Sum of digits
Input
12
Output
1+2=3
#include<iostream>
#include<string>
using namespace std;
int sum(int number){
int sum = 0.0;
while(number != 0){
sum = sum+ number % 10;
number = number /10;
}
return sum;
}
//Testing code
int main(){
cout<<sum(12)<<endl;
}
Comments
Leave a comment