Write a program that asks the user of a positive integer value. The program should use a loop to get the sum of all the integers from 1 up to the number entered. For example, if the user enters 50, the loop will find the sum of 1,2,3,4,…,50. using namespace std;
Program:
#include<iostream>
using namespace std;
int main()
{
int no,sum=0;
cout<<"Enter a positive number value: ";
cin>>no;
//if else to check whether the number is positive or not
if(no>0)
{
//loop to calculate sum from 1 to n
for(int i=1;i<=no;i++)
{
sum=sum+i;
}
//displaying the calculated sum
cout<<"\nThe sum of all numbers from 1 to
"<<no<<": "<<sum;
}
else
{ //error message if the entered number is not a positive
number
cout<<"The entered number is not a positive number!!";
}
return 0;
}
Output:
Get Answers For Free
Most questions answered within 1 hours.