Find sum of 1st 10 odd numbers using while loop in C++
while Loop - Question 1
In this question, we will see how to find the sum of 1st 10 odd numbers in C++ programming using while loop. To know more about while loop click on the while loop lesson.
Q1) Write a program in C++ to find the sum of 1st 10 odd numbers using while loop.
Program
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int i=1,n=1,s=0;
while(i<=10)
{
cout<<n<<endl;
s=s+n;
n=n+2;
i=i+1;
}
cout<<"Sum of 1st 10 odd numbers = "<<s;
return 0;
}
Output
1 3 5 7 9 11 13 15 17 19 Sum of 1st 10 odd numbers = 100