Obviously, this program will count the sum of all the digits of number entered by user when program is executed.Heart of this program is lines 11,12 and 13.11 is rem=n%10 means it will extract last digit from number.In 12 line number is divided by 10.13 will add that remainder to sum.For example Entered number is 2334 and when we enter this number in program then it will print 12 because 2+3+3+4=12.Now let us illustrate like program will do.Suppose we have 2334. dig=n%10 here dig will be now 4 after that program will divide number by 10 So new number will be 233 then program will add 4 to initial sum of digits and it goes till number becomes 0.
Program:
#include<stdio.h>
#include<conio.h>
main()
{
long n,sum=0,dig;
clrscr();
printf("Enter any number: ");
scanf("%ld",&n);
while(n!=0)
{
dig=n%10;
n=n/10;
sum=sum+dig;
}
printf("\nSum of all digits is %ld",sum);
getch();
}
Output:
Program:
#include<stdio.h>
#include<conio.h>
main()
{
long n,sum=0,dig;
clrscr();
printf("Enter any number: ");
scanf("%ld",&n);
while(n!=0)
{
dig=n%10;
n=n/10;
sum=sum+dig;
}
printf("\nSum of all digits is %ld",sum);
getch();
}
Output: