Display the n terms of odd natural number and their sum
C++ program
Source code:
#include <iostream>// iostream provides basic
// input and output services for C++ programs
using namespace std;
int main()// Th execution of program start with the main function
{
// data type
int i, n, sum = 0;
cout << "\n\n Display n terms of odd natural number and their sum:\n";
// cout is used to display
// the same statement in the output.
cout << "---------------------------------------------------------\n";
cout << " Input number of terms: ";
cin >> n;
// Cin used to enter a value by the user.
cout << " The odd numbers are: ";
for (i = 1; i <= n; i++)
{
cout << 2 * i - 1 << " ";
sum += 2 * i - 1;
}
cout << "\n The Sum of odd Natural Numbers upto " << n << " terms";
" << sum << endl"; // endl is used to enter the next line characters
}
0 Comments