Write a program in C++ to display the n terms of even natural number and their sum.
Flowchart of n terms of even natural number and display their sum
C++ Source Code Of program of displaying the n terms of even natural number and their sum
#include<iostream>
using namespace std;
int main()
{
int j,num,sum=0;
cout<<"Input number of terms : ";
cin>>num;
cout<<"the even numbers are : ";
j=1 ;
do
{
cout<<2*j<<endl;
sum+=2*j;
j++;
}
while(j<=num);
cout<<"The sum of numbers up to "<<num<<" terms is: "<<sum;
}
Output
Input number of terms: 5
the even numbers are:2
4
6
8
10
The sum of numbers up to 5 terms is: 30
Exercise
Find the possible mistakes in the following Shamil’s Flow Table of the program of displaying the n terms of even natural number and their sum.
Suppose user enter 5
| Loop | What lines will execute? | Actual work to do |
| 1 time
J=1 |
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 | Sum += 2*j
Sum+=2*1; Sum+=2 |
| 2 time
J=2
|
1,2,3,4,5,6,7,8,9,(10,11,12,13,14,15,16)2,17 | Sum += 2*j
Sum+=2*2; Sum+=6 |
| 3 time
J=3 |
1,2,3,4,5,6,7,8,9,(10,11,12,13,14,15,16)3,17 | 6+=6
Sum=12 |
| 4 time
J=4 |
1,2,3,4,5,6,7,8,9,(10,11,12,13,14,15,16)4,17 | 12+=8
Sum=20 |
| 5 time
J=5 |
1,2,3,4,5,6,7,8,9,(10,11,12,13,14,15,16)5,17 | 20+=10
Sum=30 |
If user enter num =0 loop still loop will execute one time
| Loop | What lines will execute? | Actual work to do |
| 1 time
J=1 |
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 | Sum=2 |

