Monday, 9 July 2012

Sum of the digits of 5 digit number in C


Code - 



/* Program to find the sum of the digits of a 5 digit number */

#include<stdio.h>

#include<conio.h>

#include<stdlib.h> //____ Header file included for system("cls") _____

int check_five(int number);

int main()
{
int number = 0, sum = 0, remainder = 0, temp = 0, check = 0;

while(check != 1)
{
system("cls");

printf("\n    __ Program to calculate the sum of the digits of 5 digit number ___");

printf("\n\n\n  Enter the Number - ");

scanf("%d",&number);

check = check_five(number);

if (check == 0)
{
//__ Error Message____

printf("\n\n\t\t____ 5 digit number is required ____");

printf("\n\n\n  Press Enter to Continue....");

while(getch() != 13)
{
}
}
}

temp = number;

while(temp != 0)
{
remainder = temp % 10;

temp = temp / 10;

sum = sum + remainder;

//___ finding the sum of the digits ____

}

printf("\n\n  The sum of the digits of number %d is %d", number, sum);

getch();

return 0;

}

int check_five(int number)
{
int remainder = 0, counter = 0;

while(number != 0)
{
remainder = number % 10;

number = number / 10;

counter++;
}

if (counter == 5)

/*___ Change 5 to make this program for any digit number, but do check the value of

 integer before doing that */
{
return 1;
}
else
{
return 0;
}
}

Follow Us on Facebook  - Assignment Hub

No comments:

Post a Comment