Pages

This blog is under construction

Sunday, January 6, 2019

Write a c program to compute the sum of the digits of a given number.

C code:

#include<stdio.h>
void main()
{
long int Num,Temp,Sum,Dig;
printf("Enter any Number:--\n");
scanf("%ld",&Num);
Temp=Num;
Sum=0;
while(Temp!=0)
{
Dig=Temp%10;
Temp=Temp/10;
Sum=Sum+Dig;
}
printf("Sum of Number %ld is %ld\n",Num,Sum);
}
Output:

Enter any Number:--                                                                                                            
3451                                                                                                                           
Sum of Number 3451 is 13 

No comments:

Post a Comment