Ask Question
12 February, 10:32

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.

+3
Answers (1)
  1. 12 February, 10:50
    0
    Following are the program in c+ + language

    #include / / 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

    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.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “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 ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers