Computer Science, asked by shekharsen2018, 23 days ago

Write a java program to print the 1st ten odd numbers (using for loop )

Answers

Answered by anindyaadhikari13
4

SOLUTION:

The given problem is solved using language - Java.

public class Main{

   public static void main(String args[]){

       System.out.print("First ten odd numbers are: ");

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

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

   }

}

EXPLANATION:

  • Line 1: Class declaration.
  • Line 2: Start of main() method.
  • Line 3: Prints a message on the screen.
  • Line 4: A loop is created. Since we are going to display first ten odd numbers, the control variable (i) is initialised with 1. We know odd numbers are in the form 2n-1. So tenth odd number will be 2*10 - 1. Loop iterates upto this range.
  • Line 5: The odd number is displayed on the screen. As odd numbers differ by 2, value of 'i' is incremented by 2.
  • Line 6: End of main method.
  • Line 7: End of class.

See attachment for output.

Attachments:
Answered by kamalrajatjoshi94
0

Answer:

Program:-

public class Program

{

public static void main(String args[])

{

int odd=1;

for(int i=1;i<=10;i++)

{

System.out.println(odd);

odd+=2;

}

}

}

Attachments:
Similar questions