How to write a C Program to Sum of digits of a Five Digit Number in C Programming Language ?
Solution:
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 '%')
/*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 ...
*/
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. */
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);
- }
Learn More :
Digit
- C Program To Find Reverse Of Any Digit Number
- C Program To Print Sum Of n Digit Number
- C Program To Add Digits Of Entered Number
- C Program Anagrams
- C Program to Sum of The First and Last Digit Of 'n' Digit Number
- C Program to Sum of First and Last Digits of a Four-Digit number
- C Program to Reversing a Five Digit Integer Number
- C Program to Sum Up Digits Of A Number
- Input Number and Calculate Sum of it's Digits C Program
Number
- Find out the perfect number using c program
- Write a c program to find out H.C.F. of two numbers.
- Check the given number is armstrong number or not using c program.
- Write a c program to find largest among three numbers using conditional operator
- FIND OUT GENERIC ROOT OF A NUMBER - C PROGRAM.
- FIND PRIME FACTORS OF A NUMBER USING C PROGRAM
- How To Write a C program that generates two random numbers ?
- Write a C program to find maximum or equal between two numbers ?
- How to Write a C program to find maximum between two numbers ?
- Write a C program to perform math operations on two input whole numbers. The operations are:
- Write a C program to find maximum between three numbers ?
- Sort Three Numbers - program reads in three Integers and displays them in ascending order.
- C Program to Enter an ODD number between 1 and 49.
- C program acquires keyboard 10 numbers for each of these numbers to determine if it is a first issue, by printing immediately message should at the end, if none of the numbers you entered was a first issue, print an appropriate message.
- C program generates a random number and uses an algorithm to generate 9 other numbers.
- C Program to Find Random Number
- C Program To Find LCM and HCF Of Two Number Using Function - 2
- C Program to find LCM and HCF Of Two Number Using Recursion - 3
- C Program To Find LCM and HCF Of Two Number -1
- C Program To Find Reverse Of Any Digit Number
- C Program To Find The Frequency Of A Number
- C Program To Print Prime Numbers Upto The Number You Want
- C Program To Print Sum Of n Digit Number
- C Program To Reverse A Number
- C Program To Search A Number Inside The Array
Sum
- Write the procedure , which is one of a sum , a product and a geometric average in the panel for the NxM elements are located on opposite diagonal and main diagonal . Remind ! Counting only odd elements !
- C Program To Find Sum and Difference Of Two Matrices
- C Program To Print Sum Of n Digit Number
- C Program Finding the sum of Squares using Recursion
- Give me an integer and I will sum it with the previous natural numbers
- C Program to Sum of The First and Last Digit Of 'n' Digit Number
- C Program to Sum of First and Last Digits of a Four-Digit number
- C Program to Sum Up Digits Of A Number
- C Program to calculate the sum of elements of upper triangle of a n*n matrix using DMA
- Calculate sum of element of upper triangle of m*n matrix by using dynamic memory allocation
- Calculate sum of non-diagonal element in m*n matrix C Program
- Calculate sum of element of lower triangle of m*n matrix by using dynamic memory allocation
- Menu Driven Program to Read Two Integers Find Sum, Difference and Product C Program
- Input Number and Calculate Sum of it's Digits C Program