Find sum of 1st 10 odd numbers using while loop in Java
while Loop - Question 1
In this question, we will see how to find the sum of 1st 10 odd numbers in Java programming using while loop. To know more about while loop click on the while loop lesson.
Q1) Write a program in Java to find the sum of 1st 10 odd numbers using while loop.
Program
public class Q1
{
public static void main(String args[])
{
int i=1,n=1,s=0;
while(i<=10)
{
System.out.println(n);
s=s+n;
n=n+2;
i=i+1;
}
System.out.println("Sum of 1st 10 odd numbers = " + s);
}
}
Output
1 3 5 7 9 11 13 15 17 19 Sum of 1st 10 odd numbers = 100