/*C Program to print the sum of digits of a number*/
#include<stdio.h>
void main() {
int n1, sum = 0;
long int no;
printf("Enter the number: ");
scanf("%ld", &no);
while (no != 0) {
n1 = no % 10;
no = no / 10;
sum = sum + n1;
}
printf("\nSum=%d", sum);
}
To add digits of a number we have to remove one digit at a time we can use '/' division and '%' modulus operator. Number%10 will give the least significant digit of the number, we will use it to get one digit of number at a time. C program To remove last least significant digit from number we will divide number by 10.
To add digits of a number we have to remove one digit at a time we can use '/' division and '%' modulus operator. Number%10 will give the least significant digit of the number, we will use it to get one digit of number at a time. C program To remove last least significant digit from number we will divide number by 10.
ReplyDeleteSum of digits of 2534 = 2 + 5 + 3 + 4 = 14