Thursday, 5 July 2018

Ankit

C# Program to find Armstrong number

C# Program to find Armstrong Number

An Armstrong Number is an integer such that the sum of the cubes of its digits is equal to the number itself.

CODE:
1)Finding Armstrong number till 3 digits only
 using System;

namespace armstrongNumber
{
    class Program
    {
        static void Main(string[] args)
        {
            int no,r,sum=0,n=0;
            Console.WriteLine("Enter number\n");
            no = int.Parse(Console.ReadLine());
            n = no;
            while (n > 0)
            {
                r = n % 10;
                n = n / 10;
                sum += r*r*r;
            }

            if (sum == no)
            {
                Console.WriteLine(no+" is a Amstrong number" );
            }
            else
            {
                Console.WriteLine(no+" is not a Amstrong number");
            }
            Console.ReadLine();
         
        }
    }
}


2) Finding Armstrong number 10 digits


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

namespace ProjectArmstrong
{
    class Armstrong
    {
        static void Main(string[] args)
        {
            int  no, sum = 0;
            Console.WriteLine("Enter number\n");
            no = int.Parse(Console.ReadLine());
            int[] numberArray = Array.ConvertAll(no.ToString().ToArray(), x => (int)x - 48);

            foreach (int n in numberArray)
            {
                sum =sum+ (int)Math.Pow(n, numberArray.Length);
            }

            if (sum == no)
            {
                Console.WriteLine(no+" is a Amstrong number");
            }
            else
            {
                Console.WriteLine(no+" is not a Amstrong number");
            }
            Console.ReadLine();

        }
    }
}

The output will be:-


Ankit

About Ankit

Yeppz itz me 18 year old blogger who is sharing about his tricks and tips related to mobile or computer. I am very passionate about Tricks And Hacks, and mainly focust to the tweaks, tricks....

Subscribe to this Blog via Email :