Computer Science, asked by mynuddinpatwary9, 2 months ago

Write a program (WAP) that will print following series upto Nth terms. (by for loop)

1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31 …….

sample input:5

sample output: 1, 3, 5, 7, 9

Answers

Answered by anindyaadhikari13
3

\texttt{\textsf{\large{\underline{Solution}:}}}

The given co‎‎de is written in Java.

import java.util.*;

public class Series{

   public static void main(){

       Scanner _=new Scanner(System.in);

       System.out.print("Enter n - ");

       int n=_.nextInt();

       _.close();

       for(int i=1;i<=2*n-1;i+=2)

           System.out.print(i+" ");

   }

}

\texttt{\textsf{\large{\underline{Logic}:}}}

  • Accept the limit from user, say n.
  • Repeat from i = 1 to 2 * n - 1:
  •   Display the value of i.
  •   Increment the value of i by 2.

See the attachment for output.

Attachments:
Similar questions