Q:

C++ Program to find Sum of Digits of a Number using while loop

belongs to collection: C++ Number Solved Programs

0

Write a C++ Program to find Sum of Digits of a Number using while loop. Here’s simple Program to find Sum of Digits of Number using while loop in C++ Programming Language.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Sum of Digits : : Sum of digits means addition of all the digits of any number, for example we take any number like 1234. Its sum of all digits is 1+2+3+4=10. Using given code below, we can easily write c++ program .

 
 

Here is source code of the C++ Program to find Sum of Digits of a Number using while loop. The C++ program is successfully compiled and run(on Codeblocks) on a Windows system. The program output is also shown in below.


SOURCE CODE : :

/*  C++ Program to find Sum of Digits of a Number using while loop  */

#include<iostream>
using namespace std;

int main()
{
    long int a,num,no,sum=0;
    
    //----Enter any number which u want --------
    cout<<"Enter any integer :: ";
    cin>>num;

    no=num;

    while(no>0)
    {
        a=no%10;
        no=no/10;
        sum=sum+a;
    }
    
    cout<<"\nSum of Digits of a Number [ "<<num<<" ] :: "<<sum<<"\n";

    return 0;
}

Output : :


/*  C++ Program to find Sum of Digits of a Number using while loop  */

Enter any integer :: 123456

Sum of Digits of a Number [ 123456 ] :: 21

Process returned 0

Above is the source code for C++ Program to find Sum of Digits of a Number using while loop which is successfully compiled and run on Windows System.The Output of the program is shown above .

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ Number Solved Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C++ Program to Find Power of a Number using for lo... >>
<< Write a C++ Program to find Cube Root of a Number...