Write a program to add natural numbers up to 20 by using for loop in c++ language.



 Write a program to add natural numbers up to nth term by using for loop in c++ language. 


#include <iostream>


using namespace std;


int main() {


    int sum =0  ,n ;


    cout<<"Enter the nth term  n = ";


    cin>>n;


     for(int i =1;i<=n; i++)
    {  sum =sum +i ;}


    cout<<"sum of  "<<n<<"th term is = "<<sum

   return 0;
}


  • OUTPUT

Enter the nth term  n = 20
the sum of  20th term is = 210




Comments

Popular posts from this blog

Use of Recursion