Exclusive Clothing Sale On Soft & Soothe

Get up to 63% off on our latest collection.

Shop Now

Finding sum of digits of a number until sum becomes single digit in C#

 

Finding sum of digits of a number until sum becomes single digit in C#


In this article we will find the single digit number from given number

for example:-

if the number is : 1589 :> 1+5+8+9 ==>  23  ==> 2 + 3 = 5


then the output should be 5


C# Code

        static void NumberSum()

        {

            int number = 1589;

            int sum = 0;

            // Loop to do sum while

            // sum is not less than

            // or equal to 9

            while (number > 0 || sum > 9)

            {

                if (number == 0)

                {

                    number = sum;

                    sum = 0;

                }

                sum += number % 10;

                number /= 10;

            }

             Console.WriteLine("Single digit number after sum is :- " +sum);

        }


Output



Share this

Related Posts

Previous
Next Post »

Exclusive Clothing Sale On Soft & Soothe

Get up to 63% off on our latest collection.

Shop Now