write a C program to calculate the sum of its digits

c program to sum of digits of a 5 digit number
c program to sum of digits of a 5 digit number

Problem Statement:

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

Answer:

/* Sum of digits of a 5 digit number*/

#include<stdio.h>
#include<conio.h>

main()
{
int num,a,n;
int sum=0; /* this has been initialised to zero otherwise it will contain a garbage value */

clrscr();
print(“\n Enter a 5 digit number less than 32767”);
scanf(“%d”, &num);

a=num % 10; /* last digit expected as remainder */
n=num/10; /*Remaining Digits */
sum=sum+a; /* sum updated with addition of extracted digit */

a=n % 10; /* 4rth digit expected as remainder */
n=n/10; /*Remaining Digits */
sum=sum+a; /* sum updated with addition of extracted digit */
a=n % 10; /* 3rd digit expected as remainder */
n=n/10; /*Remaining Digits */
sum=sum+a; /* sum updated with addition of extracted digit */
a=n % 10; /* 2nd digit expected as remainder */
n=n/10; /*Remaining Digits */
sum=sum+a; /* sum updated with addition of extracted digit */

a=n % 10; /* 1st digit expected as remainder */
sum=sum+a; /* sum updated with addition of extracted digit */

printf(“\n The sum of 5 digits of %d is %d”, num, sum);

printf(“\n\n\n\n Press any key to exit…”);
getch();

}

 

Related Posts

  • 75
    Problem Statement: If a four-digit number is input through the keyboard, write a program to obtain the sum of the first and last digit of this number. Solution: /* Sum of 1st and last digit of a four digit number */ #include<stdio.h> #include<conio.h> main() { int n,a,sum=0; clrscr(); printf("\Enter a…
    Tags: digit, sum, number, program, write, learn, example, tutorial, programming, basic
  • 65
    Write a C Program for the following :- Ramesh’s basic salary is input through the keyboard. His dearness allowance is 40% of basic salary, and house rent allowance is 20% of basic salary. Write a program to calculate his gross salary. Answer:  /* To Calculate the gross salary of Ramesh…
    Tags: basic, program, write, calculate, keyboard, input, sum, tutorial, learn, example
  • 64
    Problem Statement: Write a program to calculate the salary as per the following table: Solution: /* C program to calculate salary */ main( ) { char g ; int yos, qual, sal ; printf ( "Enter Gender, Years of Service and Qualifications ( 0 = G, 1 = PG ):" )…
    Tags: program, calculate, example, write, learn, tutorial, programming, basic
  • 60
    Problem Statement :  Two numbers are input through the keyboard into two locations C and D. Write a program to interchange the contents of C and D. Answer: /* Interchanging of contents of two variables c & d */ #include<stdio.h> #include<conio.h> main() { int c,d,e; clrscr(); printf("\n Enter the number…
    Tags: number, write, program, keyboard, input, example, basic, tutorial, learn
  • 59
    Problem Statement : If a five-digit number is input through the keyboard, write a program to reverse the number. Answer: /* To reverse the digits of 5-digit number */ #include<stdio.h> #include<conio.h> main() { int n,a,b; long int revnum=0; clrscr(); printf("\n Enter a five digit number less than 32767"); scanf("%d",&n); a=n%10;…
    Tags: digit, number, program, write, example, learn, tutorial, basic

Satya Prakash

VOIP Expert: More than 8 years of experience in Asterisk Development and Call Center operation Management. Unique Combination of Skill Set as IT, Analytics and operation management.

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: