- To print sum of 1 to N
User Input: 10
Output:
Sum of 1 to 10 : 55
#include <iostream>
using namespace std;
int main() {
int n;
cout<<"Enter last number : ";
cin>>n;
cout<<"Sum of 1 to "<<n<<" : "<<(n*(n+1))/2;
return 0;
}
Enter last number : 10 Sum of 1 to 10 : 55Program
We are using the formula of sum of natural numbers (1 to N)
(N*(N+1))/2 // N is the last number
Concept
Coming Soon !
QuickIn order to succeed you must fail, so that you know what not to do the next time.