First C Program

(This is based on Turbo C compiler)

Q.Write a program in C to find the sum of the each digit of a given number

#include<stdio.h>

#include<conio.h> // if using Dev C compiler, then don’t need this header

void main()              //starting of the main program

{

int num,sum=0,r;       //variable declaration

clrscr();

printf(“\nEnter the number that you want,don’t put zero or negative?”);

scanf(“%d”,&num);

while(num>0)       //starting of the while loop with test condition

{

r=num%10;       //variable ‘r’ store the remainder of the number

sum=sum+r;     //remainder will be added to variable sum which was set zero initially.

num=num/10;   //number is decreased by 1o times to get next remainder in next loop

}

printf(“The sum of the digit is %d”,sum);

getch();

}

This entry was posted in Uncategorized. Bookmark the permalink.