Author : Rahul

Write a program to print the sum of the digits of the number


Program:

using System;

namespace ProgrammingQuestion
{
 class Program
 {
   static void Main(string[] args)
   {
     //Variable Declaration
     string checkVal;
     int checkNum, digit, digitSum = 0, tempNum = 0;
     
     //User Input
     Console.Write("Enter Number:");
     checkVal = Console.ReadLine();
     checkNum = Convert.ToInt32(checkVal);
     
     //Sum of digit Logic
     tempNum = checkNum;
     while (tempNum > 0)
     {
       digit = tempNum % 10;
       digitSum = digitSum + digit;
       tempNum = tempNum / 10;
     }
     
     //Print Result
     Console.WriteLine($"Sum of the Digits:{digitSum}");
     Console.Read();
   }
 }
}

Output:

Enter Number:125
Sum of the Digits:8