Beta Coder

Learn code like Beta Coder.

Breaking

Thursday, September 3, 2020

C Program to Calculate sum of all digits of a given number

CALCULATE SUM OF ALL DIGITS OF A GIVEN NUMBER

CALCULATE SUM OF ALL DIGITS OF A GIVEN NUMBER
In this program we learn how to calculate sum of all digits of a given number provided by user using C.
C program to calculate sum of all digits of a given number
// Calculate sum of all digits of a given number
#include<stdio.h>
#include<conio.h>
void main()
{
	int n,s=0,r;
	clrscr();
	printf("\n Enter a number : ");
	scanf("%d",&n);
	printf("\n The sum of %d = ",n);
	do
	{
		r = n % 10;
		printf(" %d +",r);
		s = s + r;
		n = n / 10;
	}while(n != 0);
	printf(" = %d",s);
	printf("\n The sum = %d",s);
	getch();
}
OUTPUT:
   Enter a number : 175
   The sum of 175 = 5 + 7 + 1 = 13
   The sum = 13

No comments:

Post a Comment