# Golang Program To Get The Predecessor Of An Integer Number Using Library Function

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

The predecessor of an integer number is defined as the number that comes before it. For example the predecessor of 2 is 2 – 1 = 1. Similarly, predecessor of -50 is -51 and so on.

### Syntax

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

## Example 1: We Will Get The Predecessor of the integer number using Library Function

### Algorithm

• Step 1 − Import the package fmt.

• Step 2 − Start function main function().

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

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

• Step 5 − print the results on the screen.

### Example

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

### Output

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

### 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 assign the number to a variable and create a predecessor variable.

• Then create the logic to find the previous integer.

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

## Example 2: We Will get the Predecessor of the Integer Number Using Library Function and Without Any Argument and Without a Return Value

### Algorithm

• Step 1 − Import the package fmt and package.

• Step 2 − Start function main ().

• Step 3 − Calling the function predecessor().

• Step 4 − Start the predecessor() function.

• Step 5 − Declare and initialize the variable.

• Step 6 − Print the result on the console screen using fmt.Printf ().

### Example

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

### Output

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

### Description of the Code

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

• Now let us start the function main(). GO program execution starts with the function main()

• Next we call the function predecessor().

• Now we start the function predecessor(). Declare and initialize the integer variable c whose predecessor has to be found.

• In the above program, predecessor(), the function performs the calculation and no arguments are passed to this function. The return type of this function is void and hence no return.

• The final result is printed on the console screen using the built-in function fmt.Printf (). This function is defined under the fmt package and it helps to write standard output.

## Example 3: We Will Get the Predecessor 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 predecessor() function.

• Step 3 − start the main function().

• Step 4 − take the values from the user.

• Step 5 − call the predecessor() function created above and pass the values entered by user in it.

• Step 6 − print the results on the screen.

### Example

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

### Output

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

### Description Of The Code

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

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

• Then we have started the main() function.

• We declare the integer variables number and previous. Initialize the variable number with a value to find its predecessor and store the result in the variable previous.

• Call the predecessor function and store the answer.

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

## Conclusion

We have successfully compiled and executed the Go language program to find the predecessor of an integer number using library function along with examples

Updated on: 16-Nov-2022

106 Views