Solution for Codechef FLOW006 | Sum of Digits

Hello Programmers,

The solution for codechef Sum of Digits problem is given below.

Problem Link:- https://www.codechef.com/problems/FLOW006

/*
*   Author:- Rahul Malhotra
*   Source:- Programming Vidya
*   Description:- Solution for Codechef Sum of Digits problem
*   Problem Link:- https://www.codechef.com/problems/FLOW006
*   Website:- www.programmingvidya.com
*/

#include<iostream>

using namespace std;

int main()
{
    // * Initializing variables
    int numberOfTestCases, n, remainder, sum;

    // * Accepting the number of test cases
    cin>>numberOfTestCases;

    // * Executing each test case one by one
    while(numberOfTestCases--)
    {
        // * Accepting the integer n
        cin>>n;

        // * Initializing remainder and sum to 0
        remainder = 0, sum = 0;

        // * Looping while n is not 0
        while(n!=0)
        {
            // * Finding the last digit of n
            remainder = n%10;

            // * Removing the last digit from n
            n = n/10;

            // * Adding it to the sum
            sum += remainder;
        }

        // * Displaying the sum of all digits
        cout<<sum<<endl;
    }
    return 0;
}

Happy Coding..!!

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s