Search

Sum of digits program in C || C Programs

C program to sum each digit: We can write the sum of digits program in c language by the help of loop and mathematical operation only.


Code -

#include<stdio.h> 
 int main()    
{    
int n,sum=0,m;    
printf("Enter a number:");    
scanf("%d",&n);    
while(n>0)    
{    
m=n%10;    
sum=sum+m;    
n=n/10;    
}    
printf("Sum is=%d",sum);    
return 0;  
}   

Output -

Enter a number:654
Sum is=15

Enter a number:123
Sum is=6
0 views0 comments

Related Posts

See All

Program of Random Number in C || C Programs

Code - #include <stdio.h> #include <conio.h> #include <stdlib.h> void main() { // use rand() function to generate the number printf (" The random number is: %d", rand()); pri

Getchar() function in C || C Programs

Code - #include <stdio.h> #include <conio.h> void main() { char c; printf ("\n Enter a character \n"); c = getchar(); // get a single character printf(" You have passed ")

  • 1024px-Telegram_logo.svg
  • download
  • YouTube