Q:

Write C++ program to print sum of digits enter by user

0

Write C++ program to print sum of digits enter by user

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.

#include <iostream>
using namespace std;
 
int main()
{
    int num, total;
 
   //Reading number
    cout<<"Enter any number: "<<endl;
    cin>>num;
 
    //Adding sum of digit in total variable
    for(total = 0; num > 0; num = num/10)
        total = total + (num%10);
 
    //Printing sum of digit
    cout<<"Sum of digits: "<< total;
    return 0;
}

Result:

Enter any number: 

525

Sum of digits: 12

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write C++ program to find sum of even numbers betw... >>
<< Write C++ program to print all natural numbers in ...