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

Popular posts from this blog

A program to Add numbers divisible by 3 or 5 in C++

When only Some specific user name will be greeted in a C++ Program.

C++ Programming Language Easy Course