‘C’ to calculate sum of digits of an integer.
C File =>
/* Write a function in ‘C’ to calculate sum of digits of an integer. Use this function in main */
#include<stdio.h>
int main()
{
int num;
// defining function
void sum_digits(int n)
{
int r,sum=0;
int num=n;
while(n>0)
{
r=n%10;
sum=sum+r;
n=n/10;
}
printf("\n Sum of digits in %d is %d",num,sum);
}
printf("\n Enter number : ");
scanf("%d",&num);
//calling function
sum_digits(num);
return 0;
}
Comments
Post a Comment
Please do not enter any spam link in message.