C++ program to find sum of digits of a number
C++ Program to Display the Sum of the Digits of a given Number
This C++ Program which gets a number from input and displays the sum of the digits in the given number. The program uses a while loop to go through each and every digit of the given number and adds them up in the loop.
Source code:
#include <iostream>
/* iostream provides basic
input and output services for C++ programs */
using namespace std;
int main()
// Th execution of program start with the main function
{
int num,sum=0,m;
//Variable Declarations specify the type followed by the name
//Initialization is the process of assign the value to a variable
cout << "\n\n Find the sum of digits of a given number:\n";
cout << " Input a number: ";
// cout is used to display the same statement in the output.
cin>>num;
// cin is used to enter the value by the user.
while(num>0)// While loop
{
m=num%10;
sum=sum+m;
num=num/10;
}
cout<<" The sum of digits is :"<<sum<<endl;
return 0;
}
0 Comments