Write a program to add even numbers up to nh by using for loop in c++ language.
Write a program to add even numbers up to nh by using for loop in c++ language.
#include<iostream>
using namespace std;
int main()
{
int i , n, sum =0;
cout<<"Add even numbers up to nth term "<<endl;
cout<<"Enter the number of term (nth) = ";
cin>>n;
for(i=2;i<=n;i=i+2 )// intialization i=2;
// upgradation ,increment by 2
{ sum =sum + i ; }
cout<<"sum of "<<n<<"th term of only even number = "<<sum;
return 0;
}
- OUTPUT
Add even numbers up to nth term Enter the number of term (nth) = 15the sum of 15th term of only even number = 56
Add even numbers up to nth term
Enter the number of term (nth) = 15
the sum of 15th term of only even number = 56
Comments
Post a Comment
If you have any doubt ,let me know