Calculations

C++ program to calculate the sum of natural numbers

C++ program to calculate the sum of natural numbers

In this tutorial, we will discuss a concept of C++ program to calculate the sum of natural numbers using loops

In this article, we are going to learn  how to  find the sum of natural numbers using loops in the C++ programming language

C program to calculate the sum

C++ code to find the sum of natural numbers using loop

C++  code to find the sum of numbers using for loop

Program 1

This program takes input from the user and stores in variable num. Then, the for loop is used to calculate the sum of natural numbers up to the given numbers.

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

int main()
{
    int num,i,sum=0;  //variable declaration

    cout<<"Enter a positive integer: ";
    cin>>num;      //get input from user

    for(i=1; i<=num; i++){
        sum+=i;    //sum=sum+i

    }
    cout<<"Sum of numbers are:"<<sum;//display sum of numbers
    getch();
    return 0;
}

When the above code is executed, it produces the following results

Enter a positive integer: 150
Sum of numbers are: 11325

C++ code to find the sum of natural numbers using while loop

Program 2

This program takes input from the user and stores in variable num. Then, the while loop is used to calculate the sum of natural numbers up to the given numbers.

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

int main()
{
    int num,i,sum=0;

    cout<<"Enter a positive integer: ";
    cin>>num;
i=1;
    while( i<=num){
        sum+=i;    //sum=sum+i
 i++;
    }
    cout<<"Sum of natural numbers are:"<<sum;
    getch();
    return 0;
}

When the above code is executed, it produces the following results

Enter a positive integer: 25
Sum of natrural numbers are: 325

C++ code to find the sum of natural numbers using do-while loop

Program 3

This program takes input from the user and stores in variable num. Then, the do-while loop is used to calculate the sum of natural numbers up to the given numbers.

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

int main()
{
    int num,i,sum=0;       //1
    cout<<"Enter a positive integer: ";
    cin>>num;              //2
    i=1;

do{        //3

    sum+=i;
     i++;
}
    while(i<=num);
          //sum=sum+i

cout<<"Sum of natural numbers are: "<<sum;      //4

    getch();
    return 0;
}

When the above code is executed, it produces the following results

Enter a positive integer:75
Sum of natural numbers are: 2850

 

Method:

  1. The following variables i,num and sum=0 are declared.
  2. A positive integer is received from the user.
  3. Then, the loop is used to add a consecutive number from 1 up to the given number.
  4. Finally, the sum is received as total and displayed.

 

Similar post

Java program to find the sum of natural numbers using loops

Python program to Calculate the sum of natural numbers using loops

C program to calculate the sum of natural numbers using loops

 

Suggested for you

for loop in C++ language

while loop in C ++ language

do-while loop in C++  language

loops in C++ language

 

 

 

 

Java program to calculate the sum of natural numbers
C++ program to find the sum of natural numbers using recursion
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago