F Sum Of The Digits Using Function In C | CodeTheta

Sum Of The Digits Using Function In C

December 20, 2015

This C program uses sumdig() function to calculate sum of a digits.It takes num integer variable as a argument. Main function uses two variable called "a" and "num" and sumdig() function uses num variable and at last program will calculate and return "sum" variable.

/* Sum Of The Digits Using Function In C Program*/
/* http://native-code.blogspot.com */
#include<stdio.h>
#include<conio.h>
void main()
{
int a,num;
clrscr();
printf("/* - - - - - http://native-code.blogspot.com - - - - -*/");
printf("\n\n\t Enter a Number : ");
scanf("%d",&num);
a=sumdig(num);
printf("/* - - - - - http://native-code.blogspot.com - - - - -*/");
printf("\n\n\t Sum Of The Digits Is: %d",a);
getch();
}
sumdig(int num)
{
static int sum;
int a,b;
a=num%10;
b=(num-a)/10;
sum=sum+a;
if(b!=0)
sumdig(b);
else
return(sum);
}
/* http://native-code.blogspot.com */

IDE Used To Test This Code : Turbo C

Try this code in your computer for better understanding. Enjoy the code. If you have any Question you can contact us or mail us. We will reply you as soon as possible.

Post a Comment