Sum-of-digits-of-a-three-digit-number-C-program

 

About this program:

This program accepts a three digit number and prints the sum of its individual digits.

Logic:

(i) Take a three digit number from user and assign it to a variable.
Ex: n=356

(ii) Divide n by 100 and assign the quotient to a variable.
Ex: a=n/100
a=356/100
a=3 (This is the first digit)

(iii) To get the second digit –
Do n%100 and divide the result by 10 and assign the value to another variable.
Ex: b=(n%100) => b=(356%100) => b=56
b=b/10 => b=56/10 => b=5
We can do this in one line:
b=((n%100)/10) => b=((356%100)/10) => b=5

(iv) To get the third digit –
Do n%10 and assign it to another variable.
Ex: c=(n%10) => c=(356%10) => c=6

(v) Now that we have got three individual digits. Add them to get the sum.
sum=a+b+c => sum=3+5+6 => sum=14

(vi) Print the final result (sum).

CODE:

 

OUTPUT:

Sum of individual digits of three digit number – C Program

2 thoughts on “Sum of individual digits of three digit number – C Program

Leave a Reply

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