Suppose we are considering Digit 234. We have output 2+3+4 = 9
#include<stdio.h>
#include<conio.h>
main()
{
int num, rem, sum=0;
clrscr();
printf(" Enter any number :- ");
scanf("%d"",&num);
while(num!=0)
{
rem=num%10;
sum=sum+rem;
num=num/10;
}
printf(" Sum Of Digit :- %d ",sum);
getch();
}
O/p : Enter any number :- 234
Sum of Digit :- 9
Execution :
num =234 , sum =0
while( 234!=0)
{
rem= num%10; rem= 234%10 ; rem=4
sum=sum+rem; sum=0+4; sum=4
num=num/10; num=234/10 num=23
}
while(num!=23)
{
rem= num%10; rem= 23%10 ; rem=3
sum=sum+rem; sum=4+3; sum=7
num=num/10; num=23/10 num=2
}
while(num!=2)
{
rem= num%10; rem= 2%10 ; rem=2
sum=sum+rem; sum=7; sum=9
num=num/10; num=234/10 num=0
}
while(num!=0)---> condition is false.
0 comments:
Post a Comment