Monday, June 26, 2006

Sum of digits of a Five Digit Number.

This program is a bit of a tricky one. The Program clears our concepts about using the "Modulus Operator" . Once done, there is no stopping us then. The other programs follow the same path and almost have the same logic with some minor changes.

If a five-digit number is input through the keyboard, write a program to
calculate the sum of its digits.
(Hint: Use the Modulus Operator '%')


/*If a five-digit number is input through the keyboard, write a program to
calculate the sum of its digits.
(Hint: Use the Modulus Operator '%') */





/*Is 12345 / 100 % 10 not 3?
The divide by 100 strips the 45 and the remainder of 123 / 10 would be 3.

unit digit 5 would be 12345 % 10
tens digit would be (12345 / 10) % 10
hundreds digit would be (12345 / 100) % 10 ...
*/



/* Using / ..the normal division opeator returns the quotient.
Using % ..the modulus Operator returns the Remainder. */



#include<stdio.h>
main ()
{
int number, last_digit, next_digit, total;

printf ("Enter the number whose sum of digits is to be calculated: ");
scanf ("%d", &number);

last_digit = number%10;
total = last_digit;

next_digit = (number/10) % 10;

total = total + next_digit;

next_digit = (number/100) % 10;

total = total + next_digit;

next_digit = (number/1000) %10;

total = total + next_digit;

next_digit = (number/10000) %10;

total = total + next_digit;


printf ("The sum of the digits of the entered number is: %d", total);

}





The File can be found at:
Download File

24 comments:

Anonymous said...

can u post the same program using recursion

Anonymous said...

hi. my name is rahul sahay.
from jhansi (u.p).
e-mail: r_sonnyy@rediffmail.com.
i have a solution for sum of five digit number by recursive.
here is it:

add stdio.h header file then.
int sum(int);
void main()
{
int N;
printf("\n Enter any five digit number");
scanf("%d",&N);
printf("%d",sum(N));
getch();
}
int sum(int N)
{
if(N == 0)
return 0;
else
return (N % 10 + sum(N / 10));
}

if any one can find any other method by recursive. then just e-mail. thanks.

Anonymous said...

five digit number u hav 2 use a long int

Anonymous said...

Thanks for this programme...if I am giving input to this programme is 12345 or 22222 or 11111 then it's giving right result but with the number like 34567 or 45678 it's giving -27 & -31 respectively..Could you please tell me why is it so?

ROHIT said...

it gives an error if u post a number greater than 32768 because the range of "int" is -32768 to 32768

Bing Chang said...

here is another method that allows numbers greater than 32768 to be keyed

main()
{
float number,a,b,c,d,e;
int a1,b1,c1,d1,e1,t;
printf("\nKey in a five digit
number");
scanf("%f",&number);

a=number/10000.0;
a1=a;

b=(number-(a1*10000.0))/1000;
b1=b;

c=(number-(a1*10000.0)-(b1*
1000))/100;
c1=c;

d=(number-(a1*10000.0)-(b1*1000)-
(c1*100))/10;
d1=d;

e=(number-(a1*10000.0)-(b1*1000)- (c1*100)-(d1*10))/1;
e1=e;

t=a1+b1+c1+d1+e1;
printf("\nThe sum of the five
digits is %d",t);

}

Anonymous said...

dude range of int is -32768 to + 32767

multi said...

Thanks to RAhul Sahay of jhansi(u.p) for his help.Thumbs up dude!

Mithelash said...

Hi this s mithe....dis s simple program to do calc for all digits...it works gud even for large no

consider-12345 assume the value for 1-a,2-b,c-3,d-4,e-5..and c below prog u willl get gud understanding

#include
main ()
{
int num, a, b,c,d,e, sm;

printf ("Enter the number whose sum of digits is to be calculated: ");
scanf ("%d", &num);
e = num % 10;
d = (num / 10)% 10;
c = (num / 100)% 10;
b = (num / 1000)% 10;
a = (num / 10000)% 10;
sum= a+b+c+d+e;
printf ("The sum of the digits of the entered number is: %d", sum);

}

mani said...

/*using while loop*/
int num,x=1;
printf("enter the number want to reverse:\n");
scanf("%d",&num);
while(x<=10000)
{
printf("%d",(num/x)%10);
x=x*10;
}

Anonymous said...

#include
#include
int sum(int num);
int main(int argc, char *argv[])
{
int num,a;
printf("Enter num=");
scanf("%d",&num);
a=sum(num);
printf("sum=%d",a);
system("PAUSE");
return 0;
}
int s=0;
int sum(int num)
{

if(num==0)
{
return;

}
else
{
s+=num%10;
sum(num/10);
//printf("sum=%d",s);
return s;
}
}

Sam said...
This comment has been removed by the author.
Sam said...

#include
#include
void main()
{
long num,sum=0,num1;
clrscr();
printf("Enter A Five Digit Number: ");
scanf("%ld",&num);
while(num>0)
{
num1=num%10;
sum=sum+num1;
num=num/10;
}
printf("\nSum is %ld",sum);
getch();
}
/*Visit for More C problem www.cse50.blogspot.com*/

Richie Rich said...

This program can calculate sum for any no. within integer limit, try this :

#include
#include
int sum_digit(int);
int num_digit(int);
int main()

{

int num,ne,result;
printf("Enter the Digit for which sum is to be calculate ");
scanf("%d",&num);
result=sum_digit(num);
printf("The sum of the digit is : %d",result);
getch();


}
int sum_digit(int num)
{

//calculating sum using function calling
int sum=0,i,limitdigit;
limitdigit=num_digit(num);

for(i=1;i<=limitdigit;i=(i*10))
sum=sum+(num/i)%10;
return sum;
}

int num_digit(int num)
{
//calculating No. of digits
int count=0;

if (num<0)
num=-num;

while (num>0)
{
count++;
num=num/10;

}
// calculation power
int temp=1;
for(int i=1; i<=count; i++)
temp=temp*10;

return temp;
}

Jansy said...

PLEASE,can any one help me for learning c-program and to write output??

Anonymous said...

hey buddy my teaher gave me a program
enter any number and find the sum of its odd digits and even digits seperatly please can you help me plz????

Anonymous said...

#include
#include
void main()
{
int n,oddsum=0,evensum=0;
clrscr();
printf("Enter the number upto which you want to calcualate the sum:");
scanf("%d",&n);
while(n!=0)
{
if(n%2==0)
{
evensum=evensum+n;
}
else
{
oddsum=oddsum+n;
}
n--;
}
printf("The sum of even digits is:%d\n",evensum);
printf("The sum of odd digits is:%d",oddsum);
getch();
}

Anonymous said...

i hope this program will help you...

Anonymous said...

thank you...a good way to slove this program.

Sagar Saha said...

hi i am sagar from kolkata...even i had done the same way but we get a fallacy when the input contains repetative numbers...for eg.if the input is 76767...the output that we get is 8...but it should have been 33....so, it couldn't help me out.....!!!!!!!!!!!!

Sagar Saha said...

hi i am sagar from kolkata...even i had done the same way but we get a fallacy when the input contains repetative numbers...for eg.if the input is 76767...the output that we get is 8...but it should have been 33....so, it couldn't help me out.....!!!!!!!!!!!!

Sagar Saha said...

hi i am sagar from kolkata...even i had done the same way but we get a fallacy when the input contains repetative numbers...for eg.if the input is 76767...the output that we get is 8...but it should have been 33....so, it couldn't help me out.....!!!!!!!!!!!!

Anonymous said...

anyone can post any program to find the sum of of digits of 2 digit number for JAVA
THNKS

Samuel Murmu said...

#include
void main()
{
int a,b,c,d,e;
printf("Enter five digit number ");
scanf("%d",&a);

a=a/1000;
b=a%100%10;
c=a/100%10;
d=a%100/10;
e=a/1000%10;

printf("Sum = %d",a+b+c+d+e);
}