Answer to Question #328642 in C++ for Nickname

Question #328642

Write a C++ program and flowchart to calculate sum of digits of a number.

1
Expert's answer
2022-04-14T06:16:19-0400
#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;

}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS