Skip to content

Instantly share code, notes, and snippets.

@Mohamed209
Created November 14, 2016 16:43
#include <stdio.h>
#include <math.h>
int sumofdigits(int n)
{
int sum=0;
if(n>0)
{
sum=(n%10)+sumofdigits(n/10);
}
return(sum);
}
int main()
{
int number;
printf("Enter positive integer\n");
scanf("%d",&number);
if(number>0)
{
printf("sum of digits of %d = %d",number,sumofdigits(number));
}
else
{
printf("Error! you must enter a positive integer");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment