Answer to Question #172120 in C++ for sara

Question #172120

Suppose that sum and num are int variables and the positive integer is stored in num. It is required to find the sum of the digits: Example: 234 the sum is 9 Hint to extract the last digit use (num%10) and to remove the last digit use (num/10) 1. Write C++ program that displays the results of the above statement


1
Expert's answer
2021-03-16T03:51:28-0400
#include <iostream>
int main() {
    int sum = 0, num = 12347652;
    while(num) {
        sum += num % 10;
        num /= 10;
    }
    std::cout << sum;
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS