Golang程序 使用库函数获取整数的前导数

Golang程序 使用库函数获取整数的前导数

在这篇文章中,我们将讨论如何使用Go语言中的库函数获得一个整数的前身。

一个整数的前导数被定义为它前面的数字。例如,2的前身是2-1=1。同样的,-50的前身是-51,以此类推。

语法

Syntax for Println() = fmt.Println(...interface{})

例1:我们将使用库函数获取整数的前导数

算法

  • 第1步 – 导入软件包fmt。

  • 第2步 – 启动函数main function()。

  • 第3步 – 初始化一个数据类型为int的变量并在其中存储数值。

  • 第4步– 实现寻找前身的逻辑。

  • 第5步 – 在屏幕上打印结果。

例子

// GOLANG PROGRAM TO FIND THE PREDECESSOR OF AN INTEGER NUMBER USING LIBRARY
// fmt package allows us to print anything on the screen.
package main
import "fmt"

// fmt package allows us to print anything on the screen.
// calling the main() function
func main() {
   fmt.Println("Golang program to find the predecessor of an integer number using library function")

   // initializing the number variable to store the required number.
   var number, predecessor int

   // assigning value to the number variable.
   number = 49

   // implementing the logic
   predecessor = number - 1

   // Print the result
   fmt.Println(" predecessor of", number, "is ", predecessor )
}

输出

Golang program to find the predecessor of an integer number using library function
predecessor of 49 is 48

代码的描述

  • 首先,我们导入允许我们打印任何东西的软件包fmt。

  • 然后我们开始执行 main() 函数。

  • 之后,我们必须将数字分配给一个变量,并创建一个前任变量。

  • 然后创建逻辑来寻找前一个整数。

  • 使用 fmt.Println() 函数在屏幕上打印结果。

例2:我们将使用库函数获得整数的前身,没有任何参数,也没有返回值

算法

  • 第1步– 导入软件包fmt和软件包。

  • 第2步 – 启动函数 main()。

  • 第3步 – 调用函数 predecessor()。

  • 第4步–启动 predecessor() 函数。

  • 第5步– 声明并初始化变量。

  • 第6步–使用 fmt.Printf() 在控制台屏幕上打印结果。

例子

// GOLANG PROGRAM TO FIND THE PREDECESSOR OF AN INTEGER NUMBER USING LIBRARY FUNCTION
// fmt package allows us to print anything on the screen.
package main
import "fmt"

// function prototype
// GO program execution starts with the function main()
func main() {

   // function call
   predecessor()
}
func predecessor() {
   fmt.Println("Golang program to find the predecessor of an integer number using library function")
   // declare and initialize the variable
   var c int
   c = 4

   // print the result
   fmt.Println("predeccessor of the number",c,"=",c - 1)
}

输出

Golang program to find the predecessor of an integer number using library function
predeccessor of the number 4 = 3

代码的描述

  • 首先,我们已经导入了允许我们打印任何东西的软件包fmt。

  • 现在让我们开始执行 函数main()。 GO程序的执行是从函数main()开始的。

  • 接下来我们调用函数 predecessor()。

  • 现在我们启动函数predec essor()。 声明并初始化整数变量c,其前身必须被找到。

  • 在上面的程序中, predecessor() , 函数执行计算,没有参数传递给这个函数。这个函数的返回类型是void,因此没有返回。

  • 最终结果通过内置函数fmt.Printf()打印在控制台屏幕上。这个函数是在fmt包下定义的,它有助于编写标准输出。

例3:我们将使用库函数在两个独立的函数中获取整数的前导数,并带有参数和回报

算法

  • 第1步 – 导入软件包 fmt

  • 第2步 – 创建并定义 predecessor() 函数。

  • 第3步 – 启动main函数()。

  • 第4步–从用户那里获取数值。

  • 第5步–调用上面创建的 predecessor() 函数,并将用户输入的数值传递给它。

  • 第6步– 在屏幕上打印结果。

例子

// GOLANG PROGRAM TO FIND THE PREDECESSOR OF AN INTEGER NUMBER USING LIBRARY
// fmt package allows us to print anything on the screen.
package main
import "fmt"
func predecessor(number int) int {
   var n int
   n = number - 1
   return n
}

// calling the main() function
func main() {
   fmt.Println("Golang program to find the predecessor of an integer number using library function")

   // declare the variables
   var number, previous int

   // initialize the number variable
   number = 66

   // implementing the logic.
   // function call
   previous = predecessor(number)

   // print the result
   fmt.Println(" predecessor of", number, "is ", previous )
}

输出

Golang program to find the predecessor of an integer number using library function
predecessor of 66 is 65

代码的描述

  • 首先,我们已经导入了允许我们打印任何东西的软件包fmt。

  • 然后创建并定义了predecessor函数,它将返回用户输入的整数的前一个值。

  • 然后,我们开始执行 main() 函数。

  • 我们声明整数变量number和previous。用一个值初始化变量number,以找到其前身,并将结果存储在变量previous中。

  • 调用predecessor函数并存储答案。

  • 最后使用 fmt.Println() 函数在屏幕上打印结果。

总结

我们已经成功地编译并执行了Go语言程序,使用库函数查找一个整数的前身,并附有示例

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程