We can get the sum of the digit by adding every digits of the given number forgetting the place of value of the digit.
For Example:
If we have the number: 382.
We can can calculate the digit sum as 3 + 8 + 2 = 13.
#include<stdio.h>
int main(){
int n;
printf("Enter the number : ");
scanf("%d", &n);
int sum = 0, x;
while(n!=0){
x = n % 10;
sum = sum + x;
n = n / 10;
}
printf("Sum is : %d",sum);
return 0;
}
#include<iostream>
using namespace std;
int main(){
int n;
cout<<"Enter the number : ";
cin>>n;
int sum = 0, x;
while(n!=0){
x = n % 10;
sum = sum + x;
n = n / 10;
}
cout<<"Sum is : "<<sum;
return 0;
}
Comments
Oops!