Q:

Define Armstrong numbers and write program to check given number is Armstrong or not, in C#

belongs to collection: C# Basic Programs | Looping programs

0

An Armstrong number is a number which is the sum of, power of each digit by total number of digits.

For example:

153 is an Armstrong number: 13 + 53 +33 = 153

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Consider the program:

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

namespace ConsoleApplication1
{
    
    class Program
    {
        static void Main(string[] args)
        {
            int i         = 0;
            int digiCount = 0;
            int number    = 0;
            int tNumber    = 0;
            int []digiArray = new int[10];
            double sum = 0;

            Console.Write("Enter Number : ");
            tNumber = number = int.Parse(Console.ReadLine());

            //To find out total number of digits in number
            while (number > 0)
            {
                digiArray[i++] = number %10; 
                number = number / 10;
                digiCount++;
            }

            for(i=0;i<digiCount;i++)
            {
                sum += Math.Pow(digiArray[i], digiCount);
            }

            if (sum == tNumber)
                Console.WriteLine("Given Number is armstrong");
            else
                Console.WriteLine("Given Number is not armstrong");
        }
    }
}

Output

Enter Number : 153
Given Number is armstrong

In above program, first of all we are finding total number of digits in given number, and store each digit into an array then using power method of Math class, find power then calculate sum of each result, then comparing sum and number, if it is equal then it is Armstrong otherwise it is not Armstrong.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# program to check whether a given number is Pali... >>
<< C# program to print Even and Odd numbers from 1 to...