Trending This Week:

test

Post Top Ad

Your Ad Spot

Saturday, June 11, 2022

C++ Program to Calculate Sum of Digits.


Definition: The sum of the digits of a number is called its digit sum. For example, the digit sum of 17 is 8 because 1 + 7 = 8.


 


C++ Program to Calculate Sum of Digits.


#include <iostream>

using namespace std;


int main() {

    

    int n,r,sum=0;

    cout<<"Enter Number : ";

    cin>>n;

    while(n!=0)

    {

        r=n%10;

        sum=sum+r;

        n=n/10;

    }

    cout<<"Sum is : "<<sum;

    return 0;

}


Output :



No comments:

Post a Comment

Post Top Ad

Your Ad Spot