C# Program to input the Number and display the sum of the Digits

Introduction

This program in C# developed using .NET Framework 4.5 and Visual Studio 2013 will demonstrate how to get the input as number and find the sum of digits of the number and display it in the console window.

C# Program to input the Number and display the sum of the Digits

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GinktageConsoleApp
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            int input;
            int result = 0, temp;
            Console.WriteLine("Enter the Number : ");
            input = int.Parse(Console.ReadLine());
            while (input != 0)
            {
                temp = input % 10;

                input = input / 10;

                result += temp;
            }

            Console.WriteLine("Sum of Digits of theInput Number is " + result);

            Console.ReadLine();
        }
    }
}

Output of the program

Enter the Number :
1234
Sum of Digits of theInput Number is 10
image

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...