Sum of digits in C Programming
In this video I am going to show you C program to calculate the sum of digits of a number: we will use modulus operator (%) to extract individual digits of a number and keep on adding them.
For example, if the input is 98, the variable sum is 0 initially
98%10 = 8 (% is modulus operator which gives us remainder when 98 is divided by 10).
sum = sum + remainder
so sum = 8 now.
98/10 = 9 because in C language whenever we divide an integer by an another integer we get an integer.
9%10 = 9
sum = 8 (previous value) + 9
sum = 17
9/10 = 0.
So finally n = 0, the loop ends we get the required sum.
General Algorithm for sum of digits in a given number:
1)Get the number
2)Declare a variable to store the sum and set it to 0
3)Repeat the next two steps till the number is not 0
4)Get the rightmost digit of the number with help of remainder ‘%’ operator by dividing it with 10 and add it to sum.
5)Divide the number by 10 with help of ‘/’ operator
6)Print or return the sum
follow me on instagram :- https://instagram.com/techno_knowledg...
source code :- https://technoknowledge09.blogspot.com
thanks for watching #technoknowledge