Write a program to compute the sum of the digits of a given integer number.
#include <stdio.h>
#include <math.h>
void main()
{
unsigned int x,y,i=0,ans,P,n,total=0;
clrscr();
printf("Enter a number and number of digits:");
scanf("%u %u",&x,&n);
while(i<n)
{
y=pow(10,i);
P=x/y;
ans=p%10;
total=total+ans;
i++;
}
printf("%u",total);
getch();
}
#include <stdio.h>
#include <math.h>
void main()
{
unsigned int x,y,i=0,ans,P,n,total=0;
clrscr();
printf("Enter a number and number of digits:");
scanf("%u %u",&x,&n);
while(i<n)
{
y=pow(10,i);
P=x/y;
ans=p%10;
total=total+ans;
i++;
}
printf("%u",total);
getch();
}
No comments:
Post a Comment