Write a program that computes and displays the sum of the fi
Write a program that computes and displays the sum of the first 50 odd integers starting with 1. Use a loop.
Solution
Here is program in c++:
#include <iostream>
using namespace std;
int main()
{
int count = 0;
int sum = 0;
// loops untill count reaches 50 i.e. if it finds 50 odd numbers
for(int i=1;count<=50;i++)
{
//checks if it is odd number
if(i%2==1)
{
//sum the odd
sum += i;
count++;
}
}
cout<<\"Sum of first 50 odd integers is : \"<< sum << endl;
return 0;
}
Output:
Sum of first 50 odd integers is : 2601
