Write a program in c++ to display n term or natural number and their sum
#include<iostream>using namespace std;int main(){int n,i, sum=0;cout << "Input a number of terms : ";cin>>n;cout<<"The natural number upto "<<n<<"th terms are: "<<endl;for (i=1;i<=n;i++){cout<<i<<" ";sum=sum+i;}cout<<"the sum of the natural number is :"<<sum<<endl;}
Input a number of terms : 4
The natural number upto 4th terms are:
1 2 3 4 the sum of the natural number is :10
[Program finished]
Post a Comment