• 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

  • Angular Interpolation
    In this article, I am going to discuss the Angular Interpolation with Examples. Please read our previous article before proceeding to this...
  • Creating and Working with tables in Sql Server
    Hi friends! In our last post we have seen How to create Database and perform Alter, Delete/ Drop operation on databases in SQL Server. Tod...
  • Filters in ASP.Net MVC
    Hi friends! Today we are going to discuss about using a very important feature of MVC i.e. “Filters“ . Filters are a unique feature of Asp...
  • Usability of SecureString object in C#
    Introduction Hi friends! In this blog we will be discussing a very interesting as well as useful topic in C# and that is Securestring objec...
  • Application Life cycle of Asp.Net MVC
    Today i am going to tell you the Application life cycle of an Asp.Net MVC Application, this post is important for those who are new to Asp.N...
  • OOPS – Difference Between Class and Struct
    Hi friends! Today we are going to discuss a very quite interesting and important topic of Object Oriented Programming(OOPs) i.e. “Classes an...
  • ASP.NET State Management
    State management is a technique or way to maintain / store the state of an Asp.Net controls, web page information, object/data, and user in ...

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