Question

5. 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 i C++ coding answer
0 0
Add a comment Improve this question Transcribed image text
Answer #1

The code for the above problem is:-

#include<iostream>
#include<bits/stdc++.h>
using namespace std;
main(){
int num,sum=0; //variable for storing input and result
cout<<"Please enter a positive integer \n"; //prompting the user for input
cin>>num;
while(num<=0) //checking if the input is negative or zero
{
   cin.clear(); //clearing the input stream
   cout<<"Invalid input . Please enter a positive integer \n"; //prompting the user to check his input
   cin>>num;   
   if(num>=1) //if the number entered is positive then stop checking the number
   break;
}

for(int i=1;i<=num;i++)
{
   sum=sum+i; //calculating the sum upto that number

}
cout<<"The sum upto "<<num<<" is "<<sum; // Displaying Result
}

The Screenshot of the code is provided below for proper indentation and positioning of comments:-

i 2 4 5 6 | 7 8 9 //variable for storing input and result //prompting the user for input #include<iostream> #include<bits/std

OUTPUT:-

Please enter a positive integer -5 Invalid input . Please enter a positive integer -4 Invalid input . Please enter a positive

Add a comment
Know the answer?
Add Answer to:
C++ coding answer 5. Write a program that asks the user for a positive integer value....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. (sumFrom1.cpp) Write a program that will ask the user for a positive integer value. The...

    1. (sumFrom1.cpp) Write a program that will ask the user for a positive integer value. The program should use the for 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. If the user enters a zero or negative number, a message should be given and the program should not continue (see Sample Run...

  • Hi please help me with this zybooks question asap. Thanks 4.17 Chapter 4 Program: Sum of...

    Hi please help me with this zybooks question asap. Thanks 4.17 Chapter 4 Program: Sum of Numbers/C++ program Write a C ++ program that asks the user for a positive integer value by prompting "Enter a positive integer number: ", read in that number, then 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....

  • C++ Write a program that asks user to input three positive integers one at a time,...

    C++ Write a program that asks user to input three positive integers one at a time, then compute and output the largest entered number. Use if-else statements for three integer comparison and use for loops for a user validation. Example output: Enter an integer: -7 Invalid! Number must be positive. Enter an integer: -2 Invalid! Number must be positive. Enter an integer: 5 Enter an integer: 7 Enter an integer: 1 Largest number: 7

  • using while loop

    Sum of Numbers: 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.C++

  • In Python, write a program that asks the user for a positive integer number. If the...

    In Python, write a program that asks the user for a positive integer number. If the user enters anything else, the program would ask the user to re-enter a number or enter -1 to quit. If it is a positive number, the program then prints out a multiplication table that goes up to that number. For example, if the user enters 10, the output would look something like this. https://www.vectorstock.com/royalty-free-vector/multiplication-table-on-white-background-vector-2721686

  • Write a C++ program that asks the user to enter a single number from 1 to...

    Write a C++ program that asks the user to enter a single number from 1 to 50 and a choice of "even" or "odd". Using a recursive function, output the sum of all the even or odd integers from 1 up to (and including) that number. Your program does not need to loop and accept more input (only one input of the number then even/odd).

  • Write a java program that asks the user to enter an integer. The program should then...

    Write a java program that asks the user to enter an integer. The program should then print all squares less than the number entered by the user. For example, if the user enters 120, the program should display 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, and 100.

  • Write a program using M.I.P.S that asks user “how many positive number that is devisable by...

    Write a program using M.I.P.S that asks user “how many positive number that is devisable by 6 you want to add?” .Then your loopcounter would be the user input. If the user enters a positive number between 1 and 100 that is devisable by 6, you increment your loop counter and add it to the sum.. You need to decide if the positive number entered by the user is divisible by 6 or not. Your program should print an error...

  • P.2. Division Write a C program to input an integer n. Then the program asks users...

    P.2. Division Write a C program to input an integer n. Then the program asks users to input n integers. Calculate the sum of all non negative integers and the division of this with every negative integer in the sequence. p3 Write a C program to print all 3-digit abc numbers, satisfying: (a+b+c)=(a.b.c). Example: 3-digit 123 number fulfills requirement as (1+2+3)=1.2.3

  • Write a program that prompts the user for positive integers, only stopping when a negative integer...

    Write a program that prompts the user for positive integers, only stopping when a negative integer or zero is given. The program should then print out how many of the positive integers were odd. Example: Enter a positive integer (0 or negative to stop): 9 Enter a positive integer (0 or negative to stop): 4 Enter a positive integer (0 or negative to stop): 7 Enter a positive integer (0 or negative to stop): -3 You entered 2 odd integers....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT