C++ Program to find sum of digits of a given number

 

Program:

#include <iostream>
using namespace std;

int main()
{
    int n,rem,sum=0;
    cout<<"Enter n:";
    cin>>n;
    //loop to find sum of digits
    while(n!=0)
    {
        rem=n%10;
        sum+=rem;
        n=n/10;
        
    }
    cout<<"Sum of digits is: "<<sum;
    return 0;
}

Output:

Enter n:456
Sum of digits is:15
C++ Program to find sum of digits of a given number C++ Program to find sum of digits of a given number Reviewed by Beast Coder on January 30, 2021 Rating: 5

No comments:

Powered by Blogger.