Sum of Digits in a number in C++ - Learn Engineering

All the news and updates relevant to Engineering. You can also Write a post here

Breaking

Friday, June 12, 2020

Sum of Digits in a number in C++

C++ PROGRAM TO FIND THE SUM OF DIGITS IN A NUMBER

Sample Test Case    :    Enter a number : 52342

Sample Test Case    :    Sum = 16

CODE

#include <iostream>
using namespace std;
int main()
{
 int n,m,sum=0;
 cout<<"Enter a number :"<<endl;
 cin>>n;
 while(n>0)
 {
     m=n%10;
     sum+=m;
     n/=10;
 }
 cout<<"Sum ="<<sum<<endl;
 return 0;
}

No comments:

Post a Comment