Question

Write a program that prompts the user to enter an integer number between 1 and 999....

Write a program that prompts the user to enter an integer number between 1 and 999. The program displays the sum of all digits in the integer if the input is valid; otherwise, it displays a message indicating that the integer is not between 1 and 999 and hence, is invalid.

Name the program file Q1.cpp

Example: if the user enters 12, sum of digits is 3.

If the user enters 122, sum of digits is 5.

Homework Answers

Answer #1
#include <iostream>

using namespace std;

int main() {
    int n;
    cout << "Enter a number: ";
    cin >> n;

    if (n < 1 || n > 999) {
        cout << "Input is invalid!" << endl;
    } else {
        int sum = 0;
        while (n > 0) {
            sum += n % 10;
            n /= 10;
        }
        cout << "sum of digits is " << sum << "." << endl;
    }
    return 0;
}

Know the answer?
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
Similar Questions
(SumDigits.java) Write code that asks the user to enter an integer between 100 and 999. The...
(SumDigits.java) Write code that asks the user to enter an integer between 100 and 999. The program finds and displays the three digits in the number. The program also calculates and displays the sum of the three digits. A sample run is shown below: Enter a number between 100 and 999: 528 The three digits in the number are: 5 2 8 The sum of the three digits is: 15 Note: You must use integer division and modulo division to...
Collapse Write a program that prompts the user to input a positive integer. It should then...
Collapse Write a program that prompts the user to input a positive integer. It should then output a message indicating whether the number is a prime number. (Note: An even number is prime if it is 2. An odd integer is prime if it is not divisible by any odd integer less than or equal to the square root of the number.) Turn in: Your source code for with The name of your program as a comment at the top...
[10 marks] (SumDigits.java) Write code that asks the user to enter an integer between 100 and...
[10 marks] (SumDigits.java) Write code that asks the user to enter an integer between 100 and 999. The program finds and displays the three digits in the number. The program also calculates and displays the sum of the three digits. A sample run is shown below: Enter a number between 100 and 999: 528 The three digits in the number are: 5 2 8 The sum of the three digits is: 15
Write a program in C++ coding that asks the user to input an integer between 25...
Write a program in C++ coding that asks the user to input an integer between 25 and 50, inclusive. Utilize a WHILE loop to test for INVALID input. If the input is INVALID, the loop will repeat, and ask the user to try again. The pseudocode looks like this: Prompt user to input an integer between 25 and 50 Accept the input from the user Test for invalid input (HINT: use a while loop and the OR operator with 2...
Write a program that asks the user to input an integer between 25 and 50, inclusive....
Write a program that asks the user to input an integer between 25 and 50, inclusive. Utilize a WHILE loop to test for INVALID input. If the input is INVALID, the loop will repeat, and ask the user to try again. The pseudocode looks like this: Prompt user to input an integer between 25 and 50 Accept the input from the user Test for invalid input (HINT: use a while loop and the OR operator with 2 relational expressions. You...
Write a program that prompts the user to enter time in 12-hour notation. The program then...
Write a program that prompts the user to enter time in 12-hour notation. The program then outputs the time in 24-hour notation. Your program must contain three exception classes: invalidHr, invalidMin, and invalidSec. If the user enters an invalid value for hours, then the program should throw and catch an invalidHr object. Follow similar conventions for the invalid values of minutes and seconds. This needs to be done in C++ There needs to be a separate header file for each...
(Python Programming) Write a program that prompts a user for a positive integer and then uses...
(Python Programming) Write a program that prompts a user for a positive integer and then uses a loop to calculate and display the sum of specific fractions as follows: Let's say the user enters 5, then your program will compute: 1/5 + 2/4 + 3/3 + 4/2 + 5/1 which is 8.7.
Write a program that prompts the user to enter a string and displays the number of...
Write a program that prompts the user to enter a string and displays the number of characters it contains, fourth character, and last character. Note: The string may contain blanks. For example: “C++ programming is fun”. You should write a complete program.
Write a complete program that prompts the user to enter the number of students and each...
Write a complete program that prompts the user to enter the number of students and each student’s score, then finally displays the highest score.(in C++)
Write a program that prompts the user for a measurement in meters and then converts it...
Write a program that prompts the user for a measurement in meters and then converts it to miles, feet, and inches. Again, check for valid input and exit with an error msg if you don’t get it. Testing: use some known values here and watch for integer truncation. This should be in javascript and should execute in netbeans.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT
Active Questions
  • The National Football League (NFL) records a variety of performance data for individuals and teams. To...
    asked 5 minutes ago
  • Associated Strategies obtained significant influence over Cece Corporation by buying 30% of Cece’s 50,000 outstanding shares...
    asked 7 minutes ago
  • A survey of 25 randomly selected customers found the ages shown​ (in years). The mean is...
    asked 10 minutes ago
  • (1)         For this discussion, I would like for you to identify and describe two out of...
    asked 12 minutes ago
  • Determine the open intervals on which the graph is concave upward or concave downward. (Enter your...
    asked 12 minutes ago
  • 9- What is the most widely used technique for determining the best combination of debt and...
    asked 12 minutes ago
  • Katsumoto Inc. (Katsumoto) manufactures and sells collectible swords. Katsumoto currently operates at 80% of its 15,000-unit...
    asked 14 minutes ago
  • A researcher wishes to estimate the percentage of adults who support abolishing the penny. What size...
    asked 21 minutes ago
  • Discuss why the longer-term generation of positive free cash flow is important to the providers of...
    asked 26 minutes ago
  • The three main areas for memory in the brain involve the Hippocampus, the Basal Ganglia, and...
    asked 47 minutes ago
  • Planning to Hire Human resource planning cannot be done in a vacuum. The HR department needs...
    asked 53 minutes ago
  • Find the present value of the income c (in dollars) over t1 years at the given...
    asked 54 minutes ago