Question

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. It should print "The total of all the integers from 1 to 50 is …"

Input validation: when user enters a invalid number, the program should prompt user "The number cannot be less than one."

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>
using namespace std;

int main()
{
   int sum=0,n;
   cout<<"Enter a positive integer number: ";
   cin>>n;
   if(n>=1)
   {
       for(int i=1;i<=n;i++)
       {
           sum=sum+i; //sum of all number
       }
       cout<<"The total of all the integers from 1 to "<<n<<" is "<<sum;

   }
   else
    cout<<"The number cannot be less than one.";


    return 0;
}


nter a positive integer number 50 The total of all the integers from 1 to 50 is 1275 Process returned 0 〈0x0) execution time

Add a comment
Know the answer?
Add Answer to:
Hi please help me with this zybooks question asap. Thanks 4.17 Chapter 4 Program: Sum of...
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
  • C++ coding answer 5. Write a program that asks the user for a positive integer value....

    C++ coding answer 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 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. (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...

  • 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++

  • 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

  • This is a C program. please help me to write a pseudocode of the program ....

    This is a C program. please help me to write a pseudocode of the program . also, I want the program In a do...while loop and the screen output should look like in the picture. please help me. 3.1. Write a program that asks the user to continue to enter two numbers (at a time). For each pair of numbers entered, the program calculates the product of those two numbers, and then accumulate that product. For each pair of numbers...

  • Finish the following program which adds up all integers from 0 to the user's given number inclusively using a While Loop

    // Finish the following program which adds up all integers from 0 to// the user's given number inclusively using a While Loop. The total should be// assigned to the variable 'total'.#includeusing namespace std;int main() {int number;int total = 0;int counter = 0; //initialize the variable// user enters a numbercout << "Enter a positive integer to find the summation of ";cout << "all numbers from 0 to the given number up to 100." << endl;cin >> number;// check for invalid user...

  • Hi please help with this program C++ language thanks Write a recursive solution to solve the...

    Hi please help with this program C++ language thanks Write a recursive solution to solve the Partition problem. Partition: Given a set A of n integers a, a, .....a., can you find a subset A_1 of integers such that the sum of the Integers in A_1 is half of the Sum of all the n Integers (that Is, sigma_alpha 1 A_1 a_i)? Input: 1.Number of integers of set A: n 2.n Integers: a_1 a_2, a_n. Output: Print out yes if...

  • Objectives: Learn how to use the C++ while and for loops. Instructions: A perfect number is...

    Objectives: Learn how to use the C++ while and for loops. Instructions: A perfect number is a positive integer greater than 1 that is equal to the sum of the positive integer numbers, including 1 but not including itself, that evenly divide into it. First, you should prompt the user for a number. Next, you should indicate if the number is a perfect number or not. Finally, you should list the numbers that evenly divide into the number entered and...

  • Hi, I need help with this MIPS Problem. Thanks! (2 pts) Modify your program from exercise...

    Hi, I need help with this MIPS Problem. Thanks! (2 pts) Modify your program from exercise 1. Now, instead of having the variables x and y in your .data section, you should ask the user for two numbers. Each time you ask for a number, you should print: Enter number: You should add the two numbers typed at the console and print the sum. In this program, you'll need to make use of system calls 1, 4, and 5. I...

  • Lab 5-2 Nested Loops 2. Summation Of Numbers (using Nested While Loops) Part A: The program...

    Lab 5-2 Nested Loops 2. Summation Of Numbers (using Nested While Loops) Part A: The program will calculate and display the total of all numbers up to a specific number (entered by the user). Only positive numbers are allowed. Part B: User-controlled loop Part A Input Validation loop Part A: Summation loop Examples: When 5 is entered the program will calculate the total as 1+2+...+5 and display 15. When 2 is enterered the program will calculate the total as 1+2...

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