Im tryint to write a program that lists each individual digit of an integer and then take the sum of those digits. The program runs but doesnt produce anything. ANy help would be much appreciated.
#include <iostream>
#include <cmath>
usingnamespace std;
int dig (int x);
int main () {
int x;
cout << "Enter an integer\n";
cin >> x;
if (x<=0) return 0;
int dig (x);
return 0;
}
int dig ( int x ){
int tempnum, sum=0, divisor, digit, count=0;
tempnum = x;
while ( tempnum!=0){
tempnum=tempnum/10;
count++;}
tempnum = x;
do { divisor = static_cast<int>(pow(10.0, --count));
digit = tempnum/divisor;
tempnum= tempnum % divisor;
cout << " " << digit;
sum = sum + digit;}
while (count!=0);
cout << "\n Sum of the digits is = " << sum << endl;
}
30,1-8 Bot