首頁 >後端開發 >C#.Net教程 >C# 中的阿姆斯壯數

C# 中的阿姆斯壯數

WBOY
WBOY原創
2024-09-03 15:35:11426瀏覽

阿姆斯壯數是一個等於其立方數字總和的數字。它由 N 位數字組成,相當於每位數字的 N 次方之和。例如,包括 0、1、153、370、371 和 407、1634、8208、9474 在內的數字被視為阿姆斯壯數。在本主題中,我們將學習 C# 中的阿姆斯壯數。

加密和解密的過程使用阿姆斯壯號碼,該號碼將被指定為金鑰。為了在兩個目標使用者之間進行身份驗證和保護,需要使用伺服器。發送者和接收者將在伺服器的幫助下得到驗證。

邏輯

為了了解阿姆斯壯數背後的邏輯,我們將用其中一個數字來簡單解釋阿姆斯壯數法。考慮數字 407 來解釋邏輯。

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

在這裡,將每個數字乘以三,如下所示:

(4*4*4) = 64

(0*0*0) = 0

(7*7*7) = 343

因此,將上面相乘的數字相加,您將得到以下結果:

64+0+343 = 407

C# 中阿姆斯壯數的範例

以下是下面提到的以下範例。

範例#1

代碼:

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();
}
}

輸出:

C# 中的阿姆斯壯數

範例#2

代碼:

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();
}
}

輸出:

C# 中的阿姆斯壯數

範例#3

代碼:

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();
}
}

輸出

C# 中的阿姆斯壯數

範例#4

代碼:

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

輸出:

C# 中的阿姆斯壯數

範例#5

代碼:

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;
}
}
}

輸出:

C# 中的阿姆斯壯數

範例#6

代碼:

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));
}
}

輸出:

C# 中的阿姆斯壯數

範例#7

代碼:

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));
}
}

輸出:

C# 中的阿姆斯壯數

結論

到目前為止,我們已經研究瞭如何在 C# 中使用阿姆斯壯數。在數論中,它被稱為自戀數。透過本文,我希望上面解釋的內容能為您的 C# 知識增添價值。嘗試使用上述不同方法中所示的不同類型的數字來玩阿姆斯特朗數位技術。

以上是C# 中的阿姆斯壯數的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:C# 中的質數下一篇:C# 中的質數