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
#include <iostream>
int main() {
    int sum = 0, num = 12347652;
    while(num) {
        sum += num % 10;
        num /= 10;
    }
    std::cout << sum;
    return 0;
}
Comments
Leave a comment