Need solution C++ (a small program )

Given an integer, find the sum of all the digits in the number until the sum becomes a single digit. E.g. sum of digits of 9264 = 21. Then sum of 21 = 3.
Last edited on
1
2
3
4
5
6
int x;
std::cin >> x;
x %= 9;
if (x == 0)
    x = 9;
std::cout << x;
Works for all natural numbers
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

using namespace std;

int main(){
int t=0;
int q;
int num;
cout<<"enter no";
cin>>num;

while(num>0){
q=num%10;
num=num/10;
t=t+q;
}

cout<<t;


return 0;
}
Topic archived. No new replies allowed.