This is the simple C++ program for beginners. The program will prompt user to enter a positive number and after this will display the sum of all natural numbers of that specific number.
Source Code
#include <iostream>
using namespace std;
int main() {
int num, sum = 0;
cout << "Plz Enter a Positive Number ";
cin >> num;
for (int i = 1; i <= num; ++i) {
sum += i;
}
cout << "Sum = " << sum;
return 0;
}
(n*(n+1))/2
ReplyDeleteYes Stephen it also work. thanks for sharing.
Delete