Golang 检查Armstrong数
在本教程中,我们将学习如何在Go编程语言中检查Armstrong数。
Armstrong数,也称为自恋数,是一个数字,其各位数字的三次幂之和等于该数本身。
- 示例1:\mathrm{(1^3) :+ :(5^3) :+ :(3^3):= :153}
-
示例2:\mathrm{(3^3) :+ :(7^3) :+ :(1^3):= :371}
在下面的示例中,我们将读取一个整数,并使用Golang程序检查给定的数字是否是Armstrong数。
语法
Syntax of For loop
Initialize
for condition {
}
incrementor
初始化语句是可选的,在for循环开始之前执行。
条件语句保存一个布尔表达式,该表达式在每次循环迭代开始时进行评估。如果条件语句的值为true,则执行循环。
增量语句在for循环体之后执行。在增量语句之后,条件语句再次进行评估,如果条件语句的值为false,则循环结束。
示例1:以下是一个在主函数内部使用函数检查阿姆斯特朗数的示例
步骤
- 步骤1 - 导入fmt包。
-
步骤2 - 开始函数 main() 。
-
步骤3 - 声明并初始化变量。
-
步骤4 - 使用带有条件和增量的for循环。
-
步骤5 - 使用 fmt.Printf() 打印结果。
示例
// Golang Program to check Armstrong Number
package main
// fmt package provides the function to print anything
import "fmt"
func main() {
fmt.Println("Number = 153")
// declare the variables
var number, temp, remainder int
var result int = 0
// initialize the variables
number = 153
temp = number
// Use of For Loop
for {
remainder = temp % 10
result += remainder * remainder * remainder
temp /= 10
if temp == 0 {
break // Break Statement used to stop the loop
}
}
// If satisfies Armstrong condition
if result == number {
fmt.Printf("%d is an Armstrong number.", number)
} else {
fmt.Printf("%d is not an Armstrong number.", number)
}
// print the result
}
输出
Number = 153
153 is an Armstrong number.
代码描述
-
在上面的程序中,我们首先声明了包main。主要包用于告诉Go语言编译器该包必须被编译并生成可执行文件。
-
我们导入了fmt包,该包包含了fmt包的文件,然后我们可以使用与fmt包相关的函数。
-
现在开始 函数main () ,这个函数是可执行程序的入口点。它不接受任何参数,也不返回任何东西。
-
首先,我们声明了四个整数变量。将变量number初始化为您想要的整数值,并将变量result初始化为0。
-
使用for循环 - 条件在if语句中给出,并且通过break语句说明停止执行。
-
最后使用fmt.Printf将结果打印在屏幕上。
示例2:以下是一个使用两个独立函数检查阿姆斯特朗数的示例
步骤
-
步骤1 - 导入fmt包。
-
步骤2 - 创建 CHECKARMSTRONG() 函数。
-
步骤3 - 声明并初始化变量。
-
步骤4 - 使用带有条件和增量器的for循环。
-
步骤5 - 开始函数 main() 。
-
步骤6 - 调用函数 CHECKARMSTRONG() 。
-
步骤7 - 使用 fmt.Printf() 打印结果。
示例
package main
// fmt package provides the function to print anything
import "fmt"
// CREATE A FUNCTION TO CHECK FOR ARMSTRONG NUMBER
func CHECKARMSTRONG(number int) bool {
// declare the variables
fmt.Printf("\nEntered Number =%d\n", number)
temp := 0
remainder := 0
var result int = 0
// initialize the variables
temp = number
// Use of For Loop
// here we calculate the sum of the cube of each digit of the
// given number to check the given number is Armstrong or not
for {
remainder = temp % 10
result += remainder * remainder * remainder
temp /= 10
if temp == 0 {
break // Break Statement used to stop the loop
}
}
// If satisfies Armstrong condition
if result == number {
fmt.Printf("%d is an Armstrong number.", number)
} else {
fmt.Printf("%d is not an Armstrong number.", number)
}
return true
// print the result
}
func main() {
fmt.Println("GOLANG PROGRAM TO CHECK ARMSTRONG NUMBER")
// calling the function CHECKARMSTRONG()
CHECKARMSTRONG(153)
CHECKARMSTRONG(351)
}
输出
GOLANG PROGRAM TO CHECK ARMSTRONG NUMBER
Entered Number =153
153 is an Armstrong number.
Entered Number =351
351 is not an Armstrong number.
Program exited.
代码描述
-
以上程序中,我们首先声明了package main。主要包用于告诉Go语言编译器该包必须被编译并生成可执行文件。
-
我们导入了包含fmt包文件的fmt包,然后我们可以使用与fmt包相关的函数。
-
接下来创建 CHECKARMSTRONG() 函数来检查该数字是否是阿姆斯特朗数。
-
声明整数变量temp、remainder和result为0。
-
然后,我们使用for循环来检查数字是否为阿姆斯特朗数。在代码中,我们计算给定数字的每个数字的立方和来检查给定数字是否为阿姆斯特朗数。
-
现在开始 main() 函数,这个函数是可执行程序的入口点。它不需要任何参数,也不返回任何内容。
-
接下来,我们使用一个整数值调用 CHECKARMSTRONG() 函数,该整数值需要检查是否为阿姆斯特朗数。
-
最后,使用 fmt.Printf() 函数在屏幕上打印结果。
结论
我们已经成功编译并执行了上述两个示例中用于检查阿姆斯特朗数的Golang程序代码。