Pages

Friday, December 16, 2011

Program to calculate sum of 5-digit number

This program calculates sum of five digit number enterd through keyboard

If a five-digit number is input through the keyboard,write a program to calculate the sum of its digits
#include<stdio.h>
void main()
{
int num,next_num,last_num,total;
clrscr();

printf("Enter the number : ");
scanf("%d",&num);

last_num=num%10;
total=last_num;

next_num=(num/10)%10;
total=total+next_num;

next_num=(num/100)%10;
total=total+next_num;

next_num=(num/1000)%10;
total=total+next_num;

next_num=(num/10000)%10;
total=total+next_num;
printf("\nThe sum of entered 5 digit number is : %d",total);

getch();

}

No comments: