Inventor Pradhap

ஆதியும் அந்தமும் என் தமிழே🙏

Post Page Advertisement [Top]

How to write a C program for Sum of the digits using while loop

How to write a C program for Sum of the digits using while loop

 

Sum of the digits using while loop


#include<stdio.h>

int main()
{
	int n,sum = 0;
	printf("Enter the Value of n :	");		 /* if n = 126*/
	scanf("%d",&n);
	
	while(n>0)
	{
		sum = (sum + (n%10));				
		n= n/10;
	}
	printf("The sum of the digits are :	%d", sum);
	
	return 0;
	
}

/* Calculations
126%10 = 6;
 sum = 0+6 = 6;
 n= 126/10 = 12 ;
 again 12%10 = 2; 
 sum = 6 + 2 = 8; 
 n= 12/10 = 1; 
 again sum = 8+1 = 9 */

No comments:

Post a Comment

Bottom Ad [Post Page]