Write a C# program to check given number is Armstrong or not?

Write a C# program to check given number is Armstrong or not?

Last updated:11/18/2021 10:06:27 PM

1 Answers

Ashutosh Kumar Verma
Ashutosh Kumar Verma

C# program to check any number is Armstrong or not.

Program:

using System;					

public class Program
{
 public static void Main()
 {
  int i=0, num;
  Console.WriteLine('Enter any number to check for Armstrong');
  num=int.Parse(Console.ReadLine());
  int count=num;
  int number=num;
  int res=0;
  while(count!=0)
  {
    count=count/10;
   i++;
  }
  //Console.WriteLine(i);
  while(number!=0)
  {
  int mul=1;
   int rem;
    rem=number%10;
   int cnt=i;
   while(i!=0)
   {
    mul=mul*rem;
    i--;
   }
   res=res+mul;
   i=cnt;
   number=number/10;
   mul=1;
  }
  if(res==num)
  {
  Console.WriteLine(num+' is an Armstromg Number');
  }else
  {
  Console.WriteLine(num+' is not an Armstrong number');
  }
 }
}

Output:

Enter any number to check for Armstrong

340
340 is not an Armstrong number


Answer