codescracker


c++

C++ Program to Add Digits of Number



« Previous Program Next Program »


Add Digits of Number in C++

To add digits of any number in C++ Programming, you have to ask to the user to enter the number to add their digits and display the addition result of the digits of the entered number on the output screen as shown here in the following program.

C++ Programming Code to Add Digits of Number

Following C++ program ask to the user to enter a number to add digits of that number:

/* C++ Program - Add Digits of Number */
	
#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();
	int num, rem=0, sum=0;
	cout<<"Enter a Number :";
	cin>>num;
	int temp=num;
	while(num>0)
	{
		rem=num%10;
		sum=sum+rem;
		num=num/10;
	}
	cout<<"Sum of the digits of "<<temp<<" is "<<sum;
	getch();
}

When the above C++ program is compile and executed, it will produce the following result:

C++ program add number digits

Same Program in Other Language

You may like the same program in other programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test