Search
 
SCRIPT & CODE EXAMPLE
 

CPP

C++ Find the sum of first n Natural Numbers

// C++ program to find the sum of first n natural numbers
// positive integers such as 1,2,3,...n are known as natural numbers
#include <iostream>
using namespace std;
int main() {
    int num, sum;
    sum = 0;
    cout << "Enter a positive integer: ";
    cin >> num;

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

    cout << "Sum = " << sum << endl;

    return 0;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: string to vector char c++ 
Cpp :: c++ cin operator 
Cpp :: max value of double c++ 
Cpp :: adding element in vector c++ 
Cpp :: c++ iterate map 
Cpp :: c++ nagetive to positive numbers 
Cpp :: getline cpp 
Cpp :: height of bst cpp 
Cpp :: const char to string 
Cpp :: c++ if in equivalent 
Cpp :: c++ init multidimensional vector 
Cpp :: c++ typing animation 
Cpp :: how to store pair in min heap in c++ 
Cpp :: c++ first letter of string 
Cpp :: c++ program to reverse an array 
Cpp :: destructor in c++ 
Cpp :: size of pointer array 
Cpp :: create a 2d vector in c++ 
Cpp :: delete dynamic array c++ 
Cpp :: find index of element in array c++ 
Cpp :: std::iomanip c++ 
Cpp :: c++ cast to type of variable 
Cpp :: c++ add to array 
Cpp :: conditional operator in c++ 
Cpp :: c++ input 
Cpp :: c++ int length 
Cpp :: convert characters to lowercase c++ 
Cpp :: no template named vector in namespace std 
Cpp :: login system with c++ 
Cpp :: set width qpushbutton 
ADD CONTENT
Topic
Content
Source link
Name
6+3 =