About this program:
In this program, we are going to take any two digit number from the user and calculate the sum of its individual digits. Look at the below example for better understanding.
Ex: If you give 28
The output will be 10 (2+8).
Here is the logic behind this program. Take any two digit number. Divide it with 10. The 1st digit is the quotient and the 2nd digit is the remainder.
(i) Take a number from the user(n). Suppose here n=28.
To get the first digit, divide n with 10 and assign the quotient to a variable (a). a=n/10.
(ii) Now to get another digit, do n%10 and assign it to another variable (b). b=n%10.
(iii) So, a=2 and b=8. Add a and b and assign it to a variable (s). s=a+b
This is the final result. Now we just have to display the value of ‘s’.
printf ("Enter a two digit number :");
printf("sum of digits of the number is: %d", s);
As you can see we have taken four variables. a,b,n and s. ‘n’ is for user input. ‘a’ is for quotient, which is the first digit. ‘b’ is for the remainder, which is the second digit.At last, we are adding the values of ‘a’ and ‘b’ to get the summation of the two digits.
Enter a two digit number: 36
Sum of digits of the number is: 9