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
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
Comments
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.
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);
}
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);
}
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;
}
#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;
}
}
#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*/
#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;
}
enter any number and find the sum of its odd digits and even digits seperatly please can you help me plz????
#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();
}
THNKS
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);
}