Sum of the digits using break

Write a program that reads a number and prints the sum of its digits. Use the division (/) operator and the remainder (%) operator. It is similar to the previous program and use break.

using namespace std;
int main(){
	int n,sum=0,d;
	cout<<"Enter the number : ";
		d = n%10;
		sum = sum + d;
		n = n/10;
		if(n==0) break;
	cout<<"The sum is "<<sum<<endl;
	return 0;

Here, d gets the remainder. The remainder is added to the sum. Then n is reduced by 10. This is done till n becomes 0 and it gets out of loop.

output of the above program.sum of the digits using break
output of the above program

One Reply to “Sum of the digits using break”

Leave a Reply

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