Home > Backend Development > C#.Net Tutorial > Armstrong Number in C#

Armstrong Number in C#

WBOY
Release: 2024-09-03 15:35:11
Original
431 people have browsed it

The Armstrong number is a number equal to the total of its digits in cubes. It consists of N digits and equivalent to the total of each digit lifted to the Nth power. For instance, the numbers including 0, 1, 153, 370, 371 and 407, 1634, 8208, 9474 are considered Armstrong numbers. In this topic, we are going to learn about Armstrong Number in C#.

The process of encryption and decryption uses Armstrong’s number which will be pointed to as a secret key. To make an authentication along with the protection between two intended users, the server is used. The sender and receiver will get validated with the aid of the server.

Logic

To see the logic behind Armstrong’s number, we will use one of the numbers to briefly explain about Armstrong number method. Consider the number as 407 to explain the logic.

407 = (4*4*4) + (0*0*0) + (7*7*7)

Here, multiply each number three times as shown below:

(4*4*4) = 64

(0*0*0) = 0

(7*7*7) = 343

Therefore, add the above-multiplied numbers, you will get the result as written below:

64+0+343 = 407

Examples of Armstrong Number in C#

Here are the following examples mention below.

Example #1

Code:

using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
int i = 0;
int num = 0;
int[] numArray = new int[10];
double res = 0;
//Step 1 : Provide the number in this step
Console.Write("Please enter the number : ");
int number = int.Parse(Console.ReadLine());
//Step 2 : Now store the number in a temporary variable
int tmpryNum = number;
//Step 3 : Calculate the total number of integers in number and store each integer in the digit array
while (number > 0)
{
numArray[i++] = number % 10;
number = number / 10;
num++;
}
//Step 4 : Calculate the number
for (i = 0; i < num; i++)
{
res += Math.Pow(numArray[i], num);
}
//Step 5 : Check whether number is prime or not
if (res == tmpryNum)
{
Console.WriteLine($"The number {tmpryNum} is armstrong");
}
else
{
Console.WriteLine($"The number {tmpryNum} is not armstrong");
}
Console.ReadLine();
}
}
Copy after login

Output:

Armstrong Number in C#

Example #2

Code:

using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
int mynum, remainder, res = 0;
Console.Write("Please enter an integer number: ");
mynum = int.Parse(Console.ReadLine());
for (int m = mynum; m > 0; m = m / 10)
{
remainder = m % 10;
res = res + remainder*remainder*remainder;
}
if (res == mynum)
{
Console.Write("The number entered by you is an Armstrong number...");
}
else
Console.Write("The number entered by you is not an Armstrong number...");
Console.ReadLine();
}
}
Copy after login

Output:

Armstrong Number in C#

Example #3

Code:

using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
int mynum,m, res=0,r;
Console.Write("Please enter the number :");
mynum = Convert.ToInt32(Console.ReadLine());
m = mynum;
while(m!=0)
{
r=m%10;
res =res+(r*r*r);
m = m / 10;
}
if(res==mynum)
Console.WriteLine("The number which you have entered is an Armstrong Number...");
else
Console.WriteLine("The number which you have entered is not an Armstrong Number...");
Console.ReadKey();
}
}
Copy after login

Output

Armstrong Number in C#

Example #4

Code:

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");
}
}
Copy after login

Output:

Armstrong Number in C#

Example #5

Code:

using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
Console.Write("Enter the Start Number : ");
int StartNumber = int.Parse(Console.ReadLine());
Console.Write("Enter the End Number : ");
int EndNumber = int.Parse(Console.ReadLine());
Console.WriteLine($"The Armstrong Numbers between {StartNumber} and {EndNumber} are : ");
for (int i = StartNumber; i <= EndNumber; i++)
{
if (IsArmstrongNumber(i))
Console.WriteLine(i);
}
Console.ReadLine();
}
static bool IsArmstrongNumber(int number)
{
int sum = 0;
int temporaryNumber = number;
int temp = 0;
int length = number.ToString().Length;
while (number != 0)
{
temp = number % 10;
number = number / 10;
sum += (int)Math.Pow(temp, length);
}
if (sum == temporaryNumber)
{
return true;
}
else
{
return false;
}
}
}
Copy after login

Output:

Armstrong Number in C#

Example #6

Code:

using System;
public class Program
{
// This function is used to find Nth Armstrong Number
static int ArmstrongNum(int n)
{
int cnt = 0;
// specifying the upper limit from integer
for(int m = 1; m <= int.MaxValue; m++)
{
int num = m, rem, digit = 0, res = 0;
num = m;
// Calculate the total digits in num
digit = (int) Math.Log10(num) + 1;
// Calculate sum of power of digits
while(num > 0)
{
rem = num % 10;
res = res + (int)Math.Pow(rem, digit);
num = num / 10;
}
// Check the Armstrong number
if(m == res)
cnt++;
if(cnt == n)
return m;
}
return n;
}
public static void Main()
{
int n = 15;
Console.WriteLine(ArmstrongNum(n));
}
}
Copy after login

Output:

Armstrong Number in C#

Example #7

Code:

using System;
public class Program
{
int power(int a, long b)
{
if( b == 0)
return 1;
if (b % 2 == 0)
return power(a, b / 2) * power(a, b / 2);
return a * power(a, b / 2) * power(a, b / 2);
}
int myfunc(int a)
{
int n = 0;
while (a != 0)
{
n++;
a = a / 10;
}
return n;
}
bool ArmstrongNum (int a)
{
int n = myfunc(a);
int tmp_var = a, res = 0;
while (tmp_var != 0)
{
int rs = tmp_var % 10;
res = res + power(rs, n);
tmp_var = tmp_var / 10;
}
return (res == a);
}
public static void Main()
{
Program pgm = new Program();
int a = 8208;
Console.WriteLine(pgm.ArmstrongNum(a));
a = 1423;
Console.WriteLine(pgm.ArmstrongNum(a));
}
}
Copy after login

Output:

Armstrong Number in C#

Conclusion

So far, we have studied how Armstrong’s number can be utilized in C#. In number theory, it is known as a narcissistic number. With this article, I hope the content explained above added value to your C# knowledge. Try with different types of numbers as shown in the above various methods to play with Armstrong number technique.

The above is the detailed content of Armstrong Number in C#. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template