Write a program that asks the user for a number n and prints the sum of the numbers 1 to n.
Input :
100
Output :
The sum of numbers from 1 to 100 is : 5050
Program:
#include<iostream>
using namespace std;
//Write a program that asks the user for a number n and prints the sum of the numbers 1 to n.
int main()
{
int n,sum;
cout<<"Enter the number : ";
cin>>n;
sum=(n)*(n+1)/2;
cout<<"The sum of numbers from 1 to "<<n<<" is : "<<sum;
return 0;
}
Comments
Post a Comment
If have any douts comment here.