C++ program to find the sum of digits of a number.
Algorithm:-
Input=number
number1=0, sum=0;
while loop > 0
(a) number1 equal to remainder of number divide
by 10
number1=number%10;
(b) add sum and number1 to sum
sum=sum+number1;
(c) divide number by 10
number=number/10;
print sum;
exit;
Approach to solve:-
Input=259
number1=number%10=9
sum=sum+number1=9
number=number/10=25
number1=number%10=5
sum=sum+number1=14
number=number/10=2
number1=number%10=2
sum=sum+number1=16
number=number/10=0
Program:-
Output:-
if any query email me at:-sushank123456@gmail.com
Input=259
number1=number%10=9
sum=sum+number1=9
number=number/10=25
number1=number%10=5
sum=sum+number1=14
number=number/10=2
number1=number%10=2
sum=sum+number1=16
number=number/10=0
Program:-
#include<iostream>
using namespace std;
int main(){
int num,sum=0,num1=0;
cout<<"ENTER THE NUMBER:";
cin>>num;
while(num>0)
{
num1=num%10;
sum=sum+num1;
num=num/10;
}
cout<<"SUM OF DIGITS:"<<sum;
return 0;
}
Output:-
ENTER THE NUMBER:5990
SUM OF DIGITS:23
KEEP CALM AND CODE...if any query email me at:-sushank123456@gmail.com