Write a C program to find the sum of individual digits of a positive integer

Previous
Next
#include<stdio.h>  
int main()    
{    
	int n,sum=0,r, temp;    
	printf("Enter a positive number : ");    
	scanf("%d",&n);    
	if(n>0)
	{
		temp=n;
		while(n>0)    
		{    
			r=n%10;    
			sum=sum+r;    
			n=n/10;    
		}    
		n=temp;
		printf("Sum of digits in %d is : %d\n", n, sum); 
	}
	else
	{
		printf("Invalid Input number \n");
	}
	return 0;  
}
Previous
Next

Courses Enquiry Form