125k views
3 votes
Write a program that asks the user for 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. Input Validation: Do not accept a negative starting number.

1 Answer

5 votes

Answer:

Following are the program in c++ language

#include <iostream> // header file

using namespace std; // namespace

int main() // main method

{

int num, sum1 = 0; // variable declaration

do

{

cout << "Enter the positive number:" << endl;

cin >> num; // read number

// cout << "You are entered a negative number" << endl;

} while(num < 0);

for(int k= 1; k <= num; k++)

sum1 = sum1+k;

cout << "Sum is: " << sum1<< endl;

return 0;

}

Output :

Enter the positive number:5

Sum is: 15

Step-by-step explanation:

In this program we taking a integer input in the "num" variable from the user .

The do while loop is iterated and check the entered integer validation i.e it check whether the entered integer is positive or not .If we entered the negative integer then it again ask for entering the value .

Finally we calculated the sum of all the integers from the 1 up to the user Entered value.

User Webketje
by
6.9k points