Golang Program To Get The Successor Of An Integer Number


In this article we will discuss about how to get the successor of an integer number using library function in Go language.

The successor of an integer number is defined as the number that comes after it. For example the successor of 2 is 2 + 1 = 3. Similarly, successor of 50 is 49 and so on.

Syntax

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

Example 1: We Will Get The Successor Of The Integer Number Using Library Function In A Single Function

Algorithm

  • Step 1 − Import the package fmt.

  • Step 2 − Start the function main ().

  • Step 3 − Initialize a variable of data type int and store value in it.

  • Step 4 − Implement the logic to find the successor.

  • Step 5 − print the results on the screen.

Example

// fmt package allows us to print anything on the screen. package main import "fmt" // Start the main() function func main() { fmt.Println("Golang program to find the successor of an integer number using library function") // declaring the integer variables var number, successor int // initializing the number variable number = 49 // implementing the logic successor = number + 1 // print the result fmt.Println("successor of", number, "is ", successor) }

Output

Golang program to find the successor of an integer number using library function
successor of 49 is 50

Description Of The Code

  • First, we import the package fmt that allows us to print anything.

  • Then we start the main() function.

  • After that we have to declare the two integer variables number and successor.

  • Initialize the variable number to a value whose successor number has to be found. The result of which will be stored in the integer variable successor.

  • Then create the logic to find the next integer value.

  • Print the results on the screen using fmt.Println() function.

Example 2: We Will Get The Successor Of The Integer Number Using Library Function In Two Separate Functions With Arguments And Return

Algorithm

  • Step 1 − Import the package fmt.

  • Step 2 − Create and define the successor() function.

  • Step 3 − Start the function main ().

  • Step 4 − Declare and initialize the integer variables.

  • Step 5 − Call the function successor().

  • Step 6 − Finally print the results on the screen.

Example

package main import "fmt" // create a function to find the successor of an integer func successor(number int) int { // declare the integer variable n := number + 1 return n } // calling the main() function func main() { fmt.Println("Golang program to find the successor of an integer number using library function") // declaring the integer variables // initializing the integer variable number := 66 // calling the function successor() next := successor(number) // print the result fmt.Println("successor of", number, "is ", next) }

Output

Golang program to find the successor of an integer number using library function
successor of 66 is 67

Description Of The Code

  • First, we have imported the package fmt that allows us to print anything.

  • Then create and define the successor function that will return the upcoming value of the integer number entered by the user.

  • Then we have started the main() function.

  • Declare the integer variables number and next. Initialize the number variable to a value whose successor number has to be found.

  • Call the successor() function created above and pass the value of the number variable and store the result in the integer variable next.

  • At last print the results of the successor of the integer on the screen using fmt.Println() function.

Conclusion

We have successfully compiled and executed the Go language program to find the successor of an integer number using library function in three separate examples.

Updated on: 16-Nov-2022

149 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements