Home C Programming Language / Programs / Write a program that prints the sum of digits of a number.
🚀 Programming Example

Write a program that prints the sum of digits of a number.

👁 1,142 Views
💻 Practical Program
📘 Step Learning
Write a program that prints the sum of digits of a number.

💻 Program Code

#include <stdio.h>
int main()
{
	int n, s=0, r;
	printf("\n Enter the Number: ");
	scanf("%d", &n);
	while(n>0)
	{
		r=n%10;
		s=s+r;
		n=n/10;
	}
	printf("\nSum of digits %d", s);
	
	getch();
	return 0;
}

                        

🖥 Program Output

<b>Output 1:</b>

 Enter the Number: 123

Sum of digits 6

<b>Output 2</b>

 Enter the Number: 1111

Sum of digits 4

                            

📘 Explanation

Example: Number: 123 1+2+3 = 6
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.