Write a C++ program and flowchart to calculate sum of digits of a number.
#include<iostream>
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(){
int n;
cout<<"Enter int N: ";
cin>>n;
cout<<sum(n)<<endl;
}
Comments
Leave a comment