If a five digit number is input through the key board, write a program to calculate the sum of its digits.
[ Use the modulus operator ' % ' . The division operator ' / ' , returns the quotient on dividing one number by another, whereas the modulus operator ' % ' gives the remainder on dividing one number by another. For example {m = 14 % 2 would store 0 in m } { m = 16 % 3 would store 1 in m } ]
#include <stdio.h>
#include <conio.h>
void main ( )
{
int num, p, q, sum = 0;
clrscr ( );
printf ("\n\n\n\t Enter a five digit number (less than 32767)...:"); scanf ("%d",&num);
p = num % 10; /* last digit extracted as reminder */
q = num /10; /* remaining digits */
sum = sum + p;
p = q % 10; /* fourth digit */
q = q /10;
sum = sum + p;
p = q % 10; /* third digit */
q = q /10;
sum = sum + p;
p = q % 10; /* second digit */
q = q /10;
sum = sum + p;
p = q % 10; /* first digit */
sum = sum + p;
printf ("\n\n\t The Sum of the five digits of %d is.....:%d", num, sum);
getch ( );
}
Click here to contact us
[ Use the modulus operator ' % ' . The division operator ' / ' , returns the quotient on dividing one number by another, whereas the modulus operator ' % ' gives the remainder on dividing one number by another. For example {m = 14 % 2 would store 0 in m } { m = 16 % 3 would store 1 in m } ]
#include <stdio.h>
#include <conio.h>
void main ( )
{
int num, p, q, sum = 0;
clrscr ( );
printf ("\n\n\n\t Enter a five digit number (less than 32767)...:"); scanf ("%d",&num);
p = num % 10; /* last digit extracted as reminder */
q = num /10; /* remaining digits */
sum = sum + p;
p = q % 10; /* fourth digit */
q = q /10;
sum = sum + p;
p = q % 10; /* third digit */
q = q /10;
sum = sum + p;
p = q % 10; /* second digit */
q = q /10;
sum = sum + p;
p = q % 10; /* first digit */
sum = sum + p;
printf ("\n\n\t The Sum of the five digits of %d is.....:%d", num, sum);
getch ( );
}
Click here to contact us
No comments:
Post a Comment