Wednesday, 19 August 2015

Display the Armstrong Numbers between 100 t0 999 Using C#.Net

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

namespace ConsoleApplication1
{
    public class armstrong
    {
        static void Main(string[] args)
        {
            int n, d, x;
            int k, i, cube = 0;
            Console.WriteLine("Display the Armstrong Numbers between 100 t0 999:");
            for (k = 100; k <= 999; k++)
            {
                cube = 0;
                x = 1;
                d = 3;
                n = k;
                while (x <= d)
                {
                    i = n % 10;
                    cube = cube + (i * i * i);
                    n = n / 10;
                    x++;
                }
                if (cube == k)
                    Console.WriteLine(k);
           

            }
            Console.ReadLine();
        }

    }
}

Explanation: The program separates individual digit of a number and calculates the cubes of the each digit.If the sum of cubes of individual digits of a number is equal to that number then the number is called as Armstrong Number.

Output:
        Display the Armstrong Numbers between 100 to 999 : 
                           153 
                           370 
                           371
                           407

No comments:

Post a Comment