If a five-digit number is input through the keyboard, write a program to calculate the sum of its digits.
(Hint: Use the modulus operator ‘%’)
void main()
{
float number,a,b,c,d,e;
int a1,b1,c1,d1,e1,t;
printf("\nKey in a five digit number");
scanf("%f",&number);
a=number/10000.0;
a1=a/1;
b=(number-(a1*10000.0))/1000;
b1=b/1;
c=(number-(a1*10000.0)-(b1*1000))/100;
c1=c/1;
d=(number-(a1*10000.0)-(b1*1000)-(c1*100))/10;
d1=d/1;
e=(number-(a1*10000.0)-(b1*1000)-(c1*100)-(d1*10))/1;
e1=e/1;
t=a1+b1+c1+d1+e1;
printf("\nThe sum of the five digits is %d",t);
}
Subscribe to:
Post Comments (Atom)
hmmm....
ReplyDelete#include
#include
int main()
{
int a,sum=0;
printf("Enter the digit");
scanf("%d",&a);
while(a!=0) //logic to calculate sum of each digit
{
sum=sum+(a%10);
a=a/10;
}
printf("sum is %d",sum);
getch();
}
#include
ReplyDeleteint main ( )
{ int a,b,c;
int sum=0;
printf ("enter 5 digit number");
scanf ("%d/n",&a);
for (b=5;b!=0;b--)
{ c=a%10;
a=a/10;
sum=sum+c;
}
printf ("%d",sum);
return 0;
}