• Home
  • About
  • Contact
  • ado.net
  • angular
  • c#.net
  • design patterns
  • linq
  • mvc
  • .net core
    • .Net Core MVC
    • Blazor Tutorials
  • sql
  • web api
  • dotnet
    • SOLID Principles
    • Entity Framework
    • C#.NET Programs and Algorithms
  • Others
    • C# Interview Questions
    • SQL Server Questions
    • ASP.NET Questions
    • MVC Questions
    • Web API Questions
    • .Net Core Questions
    • Data Structures and Algorithms

Tuesday, August 11, 2020

Sum of Digits Program in C# with Examples

 Admin     August 11, 2020     C#, Interview Question, Programming, Programs     No comments   

In this article, I am going to discuss the Sum of Digits Program in C# with some examples. Please read our previous article where we discussed the Factorial Program in C# in many different ways. This is one of the frequently asked interview questions. As part of this article, we will discuss the following things.
  1. How to find the sum of digits of a given number using a loop?
  2. How to find the sum of digits of a given number using recursion?
  3. Using LINQ to find the sum of digits of a number in C#.
Finding the sum of digits of a given number using a loop in C#:
Sum of Digits Program in C# with Examples

Let us first understand the algorithm to find the sum digits of a number.

Algorithm: Sum of Digits Program in C#

Step1: Get the number from the user
Step2: Get the modulus/remainder of that number by doing the modulus operation
Step3: Sum the remainder of the number
Step4: Divide the number by 10
Step5: Repeat step 2 while the number is greater than 0.

Let us put the above steps into a program.
using System;
namespace LogicalPrograms
{
    public class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter the Number : ");
            int number = int.Parse(Console.ReadLine());
            int sum = 0, reminder;
           
            while (number > 0)
            {
                reminder = number % 10;
                sum = sum + reminder;
                number = number / 10;
            }
            
            Console.WriteLine($"The Sum of Digits is : {sum}");
            Console.ReadKey();
        }
    }
}
Output:
Finding the sum of digits of a given number using a loop in C#

Finding the sum of digits of a number using Recursion in C#:
Let us understand how to find the sum of digits of a number using Recursion in C#. Please have a look at the following program.
using System;
namespace LogicalPrograms
{
    public class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter the Number : ");
            int number = int.Parse(Console.ReadLine());
            int sum = SumOfDigits(number);
            Console.WriteLine($"The Sum of Digits is : {sum}");
            Console.ReadKey();
        }
        static int SumOfDigits(int number)
        {
            if (number != 0)
            {
                return (number % 10 + SumOfDigits(number / 10));
            }
            else
            {
                return 0;
            }
        }
    }
}
Output:
Finding the sum of digits of a number using Recursion in C#

Using Linq to find the sum of digits of a number in C#:
In the following program, first, we convert the integer into a string. As we know a string is an array of characters, so we use the Select operator to iterate over the array. For each character digit, first, we parse it to a string and then we parse it to an int. Send the resulting enum to an int[] array. And then apply the Sum extension method on it to get the desired result.
using System;
using System.Linq;
namespace LogicalPrograms
{
    public class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter the Number : ");
            int number = int.Parse(Console.ReadLine());
            
            int sum = number.ToString().Select(digit => int.Parse(digit.ToString())).ToArray().Sum();
            
            Console.WriteLine($"The Sum of Digits is : {sum}");
            Console.ReadKey();
        }
    }
}
Output:
Using Linq to find the sum of digits of a number in C#

In the next article, I am going to discuss Decimal to Binary Conversion Program in C# with some examples. Here, in this article, I try to explain the Sum of Digits Program in C# in many different ways. I hope you enjoy this article.

Summary:
I hope this post will be helpful to write Sum of Digits Program in C#
Please share this post with your friends and colleagues.
For any queries please post a comment below.
Happy Coding 😉
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Newer Post Older Post

0 comments:

Post a Comment

If you like this website, please share with your friends on Facebook, Twitter, LinkedIn.

Join us on Telegram

Loved Our Blog Posts? Subscribe To Get Updates Directly To Your Inbox

Like us on Facebook

Popular Posts

  • What is Dependency Injection(DI)
    Hi friends! Today we are going to learn about Dependency Injection and in our last session we have come across Static classes and where it s...
  • C# Programming Examples on Sorting
    Today i am going to tell you some of the Sorting programming questions in C#. Q1- Write a C# program to perform Selection sort. Ans:  Sel...
  • Calling Web API Service in a Cross-Domain Using jQuery AJAX
    In this article, I am going to discuss Calling Web API Service in a Cross-Domain Using jQuery AJAX . Please read our previous article befor...
  • ViewBag in ASP.NET Core MVC
    In this article, I am going to discuss the use of ViewBag in ASP.NET Core MVC application with examples. Please read our previous article ...
  • Recursion And Back Tracking
    In this article, I am going to discuss Recursion And BackTracking in detail. Please read our previous article where we discussed Master Th...
  • What is Abstract Class and When we should use Abstract Class
    Hi friends! In our previous sessions we have seen  Difference Between Class and Struct . And in our last session  we learnt Usability of Sec...
  • Binary to Decimal Conversion in C# with Examples
    In this article, I am going to discuss the Binary to Decimal Conversion in C# with some examples. Please read our previous article where w...

Blog Archive

Contact Form

Name

Email *

Message *

Tags

.Net .Net Core .Net Core MVC Algorithm Angular Anonymous Types Asp.Net Asp.Net MVC Blazor C# Data Structure Database Design Patterns Entity Framework Entity Framework Core Filters Interview Question Management Studio Programming Programs SQL Server SSMS Web API

Copyright © C# Techtics | All Right Reserved.

Protected by Copyscape