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 :: c++ vector add scalar 
Cpp :: pagesNumbering C++ 
Cpp :: how initilaize deffult value to c++ class 
Cpp :: Difference Array | Range update query in O 
Cpp :: can you add a bool and an int 
Cpp :: c++ 2 dim array initialize 
Cpp :: max of 3 numbers in c++ 
Cpp :: c++ abs template 
Cpp :: c++ tuple example 
Cpp :: C++ Array With Empty Members 
Cpp :: stack using cpp 
Cpp :: sort c++ stl 
Cpp :: C++ Rectangular Form 
Cpp :: inorder to postorder converter online 
Cpp :: find number of 1s in a binary cv::mat image 
Cpp :: generate random ints and floats 
Cpp :: clean list widget qt 
Cpp :: c++ sigabrt 
Cpp :: 378. Kth Smallest Element in a Sorted Matrix using binary search 
Cpp :: type defination in C++ 
Cpp :: c++ code 
Cpp :: how to run c++ on cmd 
Cpp :: button creation in C++ GUI 
Cpp :: cpp qmenu add custom widget action 
Cpp :: Required Length 
Cpp :: kruskal algorithm in c++ 
Cpp :: remove element from vector c++ by index 
Cpp :: how to declare a function in c++ header file 
Cpp :: sort c++ array 
Cpp :: transform cpp 
ADD CONTENT
Topic
Content
Source link
Name
4+7 =