Question

Q#1: Write a C++ program to enter a number, compute and print the following: - Sum...

Q#1:

Write a C++ program to enter a number, compute and print the following:

- Sum of even digits in the number

- Count of even digits in the number

- Count of digits below 5

For example, if the input is 3429, then

Sum of even digits in the number = 6

Count of even digits in the number = 2

Count of digits below 5 = 3

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>

using namespace std;

int main() {
    int n, d, evenCount = 0, evenSum = 0, digitCount = 0;
    cin >> n;
    if (n == 0) {
        digitCount++;
        evenCount++;
    }
    while (n > 0) {
        d = n % 10;
        if (d % 2 == 0) {
            evenCount++;
            evenSum += d;
        }
        if (d < 5) {
            digitCount++;
        }
        n /= 10;
    }
    cout << "Sum of even digits in the number = " << evenSum << endl;
    cout << "Count of even digits in the number = " << evenCount << endl;
    cout << "Count of digits below 5 = " << digitCount << endl;
    return 0;
}

Add a comment
Know the answer?
Add Answer to:
Q#1: Write a C++ program to enter a number, compute and print the following: - Sum...
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
  • (Java Please) Sum of Digits Write a program that will sum the digits of a number...

    (Java Please) Sum of Digits Write a program that will sum the digits of a number input by the user. For example, if the user enters the number 1234, the sum of the digits will be 10 (1 + 2 + 3 + 4 = 10). The user will enter a number with at least 4 digits (greater than 1000). That value will be sent to a method which will return the sum. Sample Output 1: SUM OF DIGITS -------------...

  • C programming! Write a program that reads integers until 0 and prints the sum of values...

    C programming! Write a program that reads integers until 0 and prints the sum of values on odd positions minus the sum of values on even positions. Let ?1, ?2, … , ??, 0 be the input sequence. Then the program prints the value of ?1 − ?2 + ?3 − ?4 + ⋯ ??. The input is a sequence of integers that always contains at least 0 and the output will be a single number. For example, for input...

  • Write a C program to do the following 1) request user to enter 10 integer into...

    Write a C program to do the following 1) request user to enter 10 integer into an array 2) sort the array in ascending order 3) display the sorted array 4) count the number of odd and even number in the array and print out the result 5) add the value of the odd number and even number and calculate the average of the odd and even number. display the result 6) write function addNumber() to add all the number...

  • Write a Python program that asks users to input an integer number and print the summation...

    Write a Python program that asks users to input an integer number and print the summation of its digits and total number of its digits. (i.e., for n=35, sum is 3+5=8, and total number of digits is 2).

  • To be turned in You will write a program that will do the following: Prompt the...

    To be turned in You will write a program that will do the following: Prompt the user to enter any character form the keyboard. You will compute some statistic concerning the type of characters entered. You will keep reading characters until a 'Q' is entered. The 'Q' should not be included when computing the statistics properties of the input. Example input: $8mJ0*5/I+Nx1@3qc2XQ We are interested in determining the following statistics: The number of alphabets The number of uppercase letters The...

  • To use the digits function, enter 1 To use the average function, enter 2 To use the perfect sum f...

    use matlab To use the digits function, enter 1 To use the average function, enter 2 To use the perfect sum function, enter3 To exit the program, enter 4 Please select a number = 6 Please re-select again: 2 please enter the first number 3 please enter the second number: 6 please enter the third number: 3 The average equals to: 4 Write a function, called digits function that is able to calculate the number of digits and the summation...

  • 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 C program convert.c that converts each number in an array by the sum of...

    Write a C program convert.c that converts each number in an array by the sum of that number plus 6 modulus 10. A sample input/output: Enter the length of the array: 5 Enter the elements of the array: 3 928 4 14 77 Output: 9 4 0 0 3 The program should include the following function: void convert(int *a1, int n, int *a2) The function converts every element in array a1 of length n to an output array a2. The...

  • In C programming please (b) You will write a program that will do the following: prompt...

    In C programming please (b) You will write a program that will do the following: prompt the user enter characters from the keyboard, you will read the characters until reading the letter 'O' You will compule statistics concerning the type of characters entered. In this lab we will use a while loop. We will read characters from stdin until we read the character 'Q' Example input mJ0*5/1+x1@3qcxQ The 'Q' should be included when computing the statistics properties of the input....

  • c++ please (1) Write a program that prompts the user to enter an integer value N...

    c++ please (1) Write a program that prompts the user to enter an integer value N which will rpresent the parameter to be sent to a function. Use the format below. Then write a function named CubicRoot The function will receive the parameter N and return'its cubic value. In the main program print the message: (30 Points) Enter an integer N: [. ..1 The cubic root of I.. ] is 2 update the code om part and write another function...

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