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

Write a c++ program to display numbers which is divisible by 2 as well 3 from 6 to n by using For loop and if else statement .

Write the program to arrange the n queen(queen of chessboard) in n*n matrix such that they can not ATTACK TO eachother.