• 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

Monday, August 10, 2020

Armstrong Number Program in C# with Examples

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

In this article, I am going to discuss the Armstrong Number Program in C# with some examples. Please read our previous article before proceeding to this article where we discussed the How to Reverse a Number and a String Program in C# with some examples. As part of this article, we are going to discuss the following pointers.
  1. What is an Armstrong Number?
  2. How to check if a given number is Armstrong or not?
  3. Program to find Armstrong number between a range of numbers.
What is an Armstrong Number?
An Armstrong Number is a number that is equal to the sum of, power of each digit by the total number of digits. For example, the numbers such as 0, 1, 153, 370, 371, and 407, 1634, 8208, 9474 are Armstrong numbers. Let us have a look at the following diagram which shows how the Armstrong number is calculated.
Armstrong Number Program in C# with Examples

Let us understand this with an example:
In the following program, first, we are finding the total number of digits in a given number, and storing each digit into an array. And then by using the power method of Math class, we are finding the power of each digit and then calculate the sum of each result. Finally, we are comparing the sum and the input number. If both are equal then it is an Armstrong number else it is not an Armstrong number.
using System;
namespace LogicalPrograms
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 0;
            int digitCount = 0;
            int[] digitArray = new int[10];
            double sum = 0;
            //Step1: Take the input
            Console.Write("Enter the Number : ");
            int number = int.Parse(Console.ReadLine());
            //Step3: Store the number in a temporary variable
            int temporaryNumber = number;
            //Step3: Find the total number of digits in number as well as
            //Store each each digit in the digit array
            while (number > 0)
            {
                digitArray[i++] = number % 10;
                number = number / 10;
                digitCount++;
            }
            //Step4: Calculate the result
            for (i = 0; i < digitCount; i++)
            {
                sum += Math.Pow(digitArray[i], digitCount);
            }
            //Step5: Check whether it is prime number or not
            if (sum == temporaryNumber)
            {
                Console.WriteLine($"The Number {temporaryNumber} is armstrong");
            }
            else
            {
                Console.WriteLine($"The Number {temporaryNumber} is not armstrong");
            }
            Console.ReadLine();
        }
    }
}
Output:
Armstrong Number in C#

Finding Armstrong number between ranges of numbers in C#:
In the following program, we are taking two input i.e. start and end numbers from the console and then finding the Armstrong numbers between these two numbers.
using System;
namespace LogicalPrograms
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter the Start Number : ");
            int StartNumber = int.Parse(Console.ReadLine());
            Console.Write("Enter the End Number : ");
            int EndNumber = int.Parse(Console.ReadLine());
            Console.WriteLine($"The Armstrong Numbers between {StartNumber} and {EndNumber} are : ");
            for (int i = StartNumber; i <= EndNumber; i++)
            {
                if (IsArmstrongNumber(i))
                    Console.WriteLine(i);
            }
            
            Console.ReadLine();
        }
        static bool IsArmstrongNumber(int number)
        {
            int sum = 0;
            int temporaryNumber = number;
            int temp = 0;
            int length = number.ToString().Length;
            while (number != 0)
            {
                temp = number % 10;
                number = number / 10;
                sum += (int)Math.Pow(temp, length);
            }
            
            if (sum == temporaryNumber)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
Output:
Finding Armstrong number between ranges of numbers in C#

In the next article, I am going to discuss the Factorial Number Program in C# with some examples. Here, in this article, I try to explain how to check whether a given number is Armstrong or not in C# with some examples.

Summary:
I hope this post will be helpful to write a Armstrong Number 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