For ststement

Sum of natural numbers 1 to n in C++ language|5 ways

Sum of natural numbers 1 to n in C++ language|5 ways

In this tutorial, we will discuss the concept of C++ program to the sum of Natural number from 1 to n – (n is entered number)

In this post, we are going to learn how to find the sum of natural numbers of C++ language in different 5 ways

Addition of Natural number

Addition of natural numbers 1 to n – using for loop

Program 1

#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
    int sum=0,num,i;
    cout<<"Enter a natural number\n";
    cin>>num;
    for(i=1; i<=num; i++){
  sum+=i;   //sum=sum+i;
}
cout<<"Sum of natural numbers from 1 to "<<num<<": "<<sum;
getch();
    return 0;
}

When the above code is executed it produces the following output

Enter a natural number
25
Sum of natural numbers from 1 to 25: 325

This program allows the user to enter a maximum number. and it displays the addition of natural numbers from 1 to given number using for loop in C++ language

Addition of natural numbers 1 to n – using while loop

Program 2

#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
    int sum=0,num,i;
    cout<<"Enter a natural number\n";
    cin>>num;
    i=1;
    while(i<=num){
  sum+=i;   //sum=sum+i;
   i++;
}
cout<<"Sum of natural numbers from 1 to "<<num<<": "<<sum;
getch();
    return 0;
}

When the above code is executed it produces the following output

Enter a natural number
50
Sum of natural numbers from 1 to 50:  1275

This program allows the user to enter a maximum number. and it displays addition of natural numbers from 1 to given number using while loop in C++ language

Addition of natural numbers 1 to n – using do-while loop

Program 3

#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
    int sum=0,num,i;
    cout<<"Enter a natural number\n";
    cin>>num;
    i=1;
    do{
  sum+=i;   //sum=sum+i;
   i++;
}while(i<=num);
cout<<"Sum of natural numbers from 1 to "<<num<<": "<<sum;
getch();
    return 0;
}

When the above code is executed it produces the following output

Enter a natural number
30
Sum of natural numbers from 1 to 30:  465

This program allows the user to enter a maximum number. and it displays addition of natural numbers from 1 to given number using the do-while loop in C++ language

Addition of natural numbers 1 to n – using function

Program 4

#include <iostream>
#include <conio.h>
using namespace std;
int sumNaturalnum(int);
int main()
{
    int num,sum=0;
     cout<<"Enter a natural number\n";
    cin>>num;
    sum=sumNaturalnum(num);
    cout<<"The sum of natural numbers 1 to"<<num<<": "<<sum;
    getch();
    return 0;
}
int sumNaturalnum(int n){
   if(n==0)
   {
       return n;
   }
   else{
    return (n*(n+1)/2);
   }


}

When the above code is executed it produces the following output

Enter a natural number
40
Sum of natural numbers from 1 to 40:  820

This program allows the user to enter a maximum number. and it displays Addition of natural numbers from 1 to given number using function in C++ language

Addition of natural numbers 1 to n – using recursive function

Program 5

#include <iostream>
#include <conio.h>
using namespace std;

int sumNaturalnum(int);
int main()
{
    int num,sum=0;
    cout<<"Enter a natural number\n";
    cin>>num;
    sum=sumNaturalnum(num);
    cout<<"The sum of natural numbers 1 to "<<num<<": "<<sum;
    getch();
    return 0;
}
int sumNaturalnum(int n){
   if(n==0)
   {
       return n;
   }
   else{
    return (n+sumNaturalnum(n-1));
   }


}

When the above code is executed it produces the following output

Enter a natural number
200
Sum of natural numbers from 1 to 200:  20100

This program allows the user to enter a maximum number. and it displays addition of natural numbers from 1 to given number using recursive method  in C++ language

 

Suggested for you

for loop in C++ language

while loop in C++ language

do-while loop in C++ language

function in C++ language

 

Similar post

Similar post

Java program to Sum of natural numbers 1 to n |5 ways

C program to sum of Natural number from 1 to n

Python program to Sum of natural numbers 1 to n

 

 

 

Karmehavannan

Recent Posts

Multiply two numbers in Java using scanner| 5 different ways

Multiply two numbers in Java using scanner| 5 different ways In this article, we will…

3 months ago

5 different ways to Divide two numbers in Java using scanner

5 Different ways to Divide two numbers in Java using scanner In this article, we…

3 months ago

Learn 8 Ways to Subtract Two Numbers Using Methods in Java

Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…

4 months ago

10 ways to subtract two numbers in Java

10 ways to subtract two numbers in Java In this article, we will discuss the…

4 months ago

Java Code Examples – Multiply Two Numbers in 5 Easy Ways

Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…

4 months ago

How to Divide two numbers in Java| 5 different ways

How to Divide two numbers in Java| 5 different ways In this article, we will…

4 months ago

This website uses cookies.