Pages

Sunday, 28 January 2018

If a five-digit number is input through the keyboard, write a program to calculate the sum of its digits.

/* This program takes a five-digit integer from user, seprates its digits, adds them
and then shows results.*/
#include<stdio.h>
#include<conio.h>
int main ()
{

/*Declaring and initializing variables*/
    int value,dig1,dig2,dig3,dig4,dig5,sum;

/*prompt to take the input*/
    printf("\n   Please enter a five-digit number: ");

/*Taking input*/  
    scanf("%d",&value);

/*Calculations*/
 
    dig1 = value % 10;
 
    /* e.g. if value is 12345 then 12345 % 10 gives 5*/
 
    value = value / 10;
 
    /* '12345/10' gives '1234.5' but this is integer division so digit/s after '.' will
    be dropped and we get '1234'*/
 
    dig2 = value % 10;
 
    /*now '1234 % 10' gives '4' and so on*/
    value = value / 10;
    dig3 = value % 10;
    value = value / 10;
    dig4 = value % 10;
    value = value / 10;
    dig5 = value % 10;
    sum = dig1+dig2+dig3+dig4+dig5 ;

/*showing Results*/  
    printf("\n   Solution: ");
    printf("\n   __________");
    printf("\n\n   %d + %d + %d + %d + %d = %d",dig5, dig4, dig3, dig2, dig1, sum );
    getch();
    return 0;

}