All Type Coding

Search Here

How to check whether the entered number is an Armstrong number or not.


using System;


class Program
{
    static void Main(string[] args)
    {
        int num, remainder, sum = 0;
        Console.Write("Enter a three digit number : ");
        num = int.Parse(Console.ReadLine());
        for (int i = num; i > 0; i = i / 10)
        {
            remainder = i % 10;
            sum = sum + remainder * remainder * remainder;
        }
        if (sum == num)
        {
            Console.Write("Entered number is an Armstrong number.");
        }
        else
            Console.Write("Entered number is not an Armstrong number.");
        Console.ReadLine();
    }
}

Output





and 4 digit armstrong number

using System;
class Program
{
    static void Main(string[] args)
    {
        int num, remainder, sum = 0;
        Console.Write("Enter a 4 digit number : ");
        num = int.Parse(Console.ReadLine());
        for (int i = num; i > 0; i = i / 10)
        {
            remainder = i % 10;
            sum = sum + remainder * remainder * remainder * remainder;
        }
        if (sum == num)
        {
            Console.Write("Entered number is an Armstrong number.");
        }
        else
            Console.Write("Entered number is not an Armstrong number.");
        Console.ReadLine();
    }
}

No comments :

Post a Comment