Search This Blog

C Program to Find Sum of Digits of a Given Number

The following C program calculates sum of digits of any given integer.

C Program

#include<stdio.h>
#include<conio.h>
void main()
{

clrscr();
int n,sum=0;
printf("Enter a number");
scanf("%d",&n);
while(n>0)
{
sum=sum+n%10; //adding last digit
n=n/10; //removing last digit
}
printf("Sum of digits of the number = %d",sum);
getch();
}

No comments: