Sum-of-digits-of-a-two-digit-number

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).

Sum of individual digits of a three digit number.

Logic:

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.
Ex: n=28
a=28/10
a=2

(ii) Now to get another digit, do n%10 and assign it to another variable (b). b=n%10.
Ex: n=28
b=28%10
b=8

(iii) So, a=2 and b=8. Add a and b and assign it to a variable (s). s=a+b
Ex: s=a+b
s=2+8
s=10

This is the final result. Now we just have to display the value of ‘s’.

CODE:

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.

OUTPUT:

 

Sum of individual digits of a two digit number – C Program
Tagged on:

Leave a Reply

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