C#: Program to check whether entered number is Armstrong Number or not

In this C# tutorial we "check whether the entered number is Armstrong Number or not" . Here we are taking three variables "n" for number ,"rem" for remainder and "sum" for sum of digits respectively. Now, lets get started!!

Note: A number is armstrong if the sum of cubes of individual digits of a number is equal to the number itself. For example 371 is an armstrong number as 33 + 73 + 13 = 371. Some other armstrong numbers are: 0, 1, 153, 370, 407.

using System;
​class arms
{
 public static void Main(String []args)
 {
            int n, rem, sum = 0;
            Console.Write("Enter a Number : ");
            n = int.Parse(Console.ReadLine());
            for (int i = n; i > 0; i = i / 10)
            {
                rem = i % 10;
                sum = sum + rem*rem*rem;
 
            }
            if (sum == n)
            {
                Console.Write("Entered Number is an Armstrong Number.");
            }
            else
            Console.Write("Entered Number is not an Armstrong Number.");
        
        
}
}
SHARE

Raunak Hajela

Hi. I’m CEO/Founder of Kodestat. I’m Student, Geek, Web Designer, Developer, Blog Enthusiast and Player. Inspired to make things looks better. I like playing with codes plus I am marvel fan :-P You can check my design portfolio here. For consulation fell free to drop an email at raunakhajela@gmail.com.

  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment