How to write a c# program to check a number is Armstrong number or not. In the below example We will see what is an Armstrong number and how to find the Armstrong number in C#.
What is Armstrong Number
An Armstrong number is an n-digit number that is equal to the sum of the cubes of its each digits is equal to the number itself. In other words Armstrong number is a number that is equal to the sum of cubes of its digits. For example – 0, 1, 153, 370, 371 and 407 are the Armstrong numbers.
Example: let’s see why 371 is a Armstrong number?
371 = (3*3*3)+(7*7*7)+(1*1*1)
where:
(3*3*3)=27
(7*7*7)=343
(1*1*1)=1
So:
27+343+1=371
Algorithm for checking Armstrong Number:
First of all, we define four integer variable n for storing the number, sum for storing the sum, r for storing the reminder each time and temp as temporary variable then assign zero (0) to the sum.
Then get user input by Console.ReadLine() and pass this input to the variable n and converting it to an integer with the help of int.parse method.
Now assign user input number to our temp variable for future use.
Then we iterate while loop til the number became zero(0).
In loop:
First, we find the Remainder of the number by finding its module by 10.
Then we get the power of this remainder by simply multiply it three times and add it in our sum variable.
Then assign the result of sum and power of remainder to the sum variable again.
Last, divide the number by 10.
After a loop, check if the temp variable and sum variable are equal. If both are equal, then print it’s an Armstrong number, otherwise print it’s not a Armstrong number.
Then we use the Console.ReadKey() method to read any key from the console. By entering this line of code, the program will wait and not exit immediately. It will wait for the user to enter any key before finally exiting, If we don’t include this statement in the code, the program will exit as soon as it run.
Simple program to check whether a number is Armstrong or not in C#
using System; namespace CSharpProgram { class Program { static void Main(string[] args) { //Define variables int n, r, sum = 0, temp; //Print message to enter the number Console.Write("Enter the Number= "); //Read a number from console and convert it in the int n = int.Parse(Console.ReadLine()); temp = n; //Execute loop while (n > 0) { r = n % 10; sum = sum + (r * r * r); n = n / 10; } if (temp == sum) Console.Write(n+ " is an armstrong Number"); else Console.Write(n+ " is not an Armstrong Number."); Console.ReadLine(); } } }
Output screen:
C# program to print all the Armstrong Number between 1 to 1000
Working of the code:Here what we have to do is for printing all the Armstrong numbers in between 1 to 1000 first we have to compute the division of the value of ‘i’ variable by 100.
Then multiply the value of ‘a’ variable by 100 and compute the difference of the resulted value with the value of ‘i’ variable and divide the resulted value by 10. In last compute the sum of the cube of ‘a’, ‘b’ and ‘c’ variables.
Here If condition statement is used to check the value of ‘i’ variable is equal to ‘d’ or not, if the condition is true then it will execute the statement and print the Armstrong numbers.
using System; class Program { static void Main() { int a, b, c, d; for (int i = 1; i <= 1000; i++) { a = i / 100; b = (i - a * 100) / 10; c = (i - a * 100 - b * 10); d = a * a * a + b * b * b + c * c * c; if (i == d) { System.Console.WriteLine("{0}", i); } } Console.Read(); } }
Output:
1 153 370 371 407 1000
To print Armstrong numbers in between any given range in C#
using System; using System.Collections.Generic; using System.Linq; public class Program { public static void Main() { int mynum,val,res,temp_var; int start_no,end_no; Console.Write("\n\n"); Console.Write("Searching for the Armstrong number in a given range of range of numbers:\n"); Console.Write("--------------------------------------------------------"); Console.Write("\n\n"); Console.Write("Enter the starting number... "); start_no= Convert.ToInt32(Console.ReadLine()); Console.Write("Enter the ending number... "); end_no= Convert.ToInt32(Console.ReadLine()); Console.Write("The list of Armstrong numbers in given above range are: "); for(mynum=start_no;mynum<=end_no;mynum++){ temp_var=mynum; res = 0; while(temp_var!=0){ val=temp_var % 10; temp_var=temp_var/10; res=res+(val*val*val); } if(res==mynum) Console.Write("{0} ",mynum); } Console.Write("\n"); } }
So I hope you have now understand how to check whether a number is Armstrong or not in C# with these many types of programs example.