Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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
<div class="execute"></div><div class="code-mirror language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token comment">// GOLANG PROGRAM TO FIND THE PREDECESSOR OF AN INTEGER NUMBER USING LIBRARY</span>
<span class="token comment">// fmt package allows us to print anything on the screen.</span>
<span class="token keyword">package</span> <span class="token namespace">main</span>
<span class="token keyword">import</span> <span class="token string">"fmt"</span>
<span class="token comment">// fmt package allows us to print anything on the screen.</span>
<span class="token comment">// calling the main() function</span>
func <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Golang program to find the predecessor of an integer number using library function"</span><span class="token punctuation">)</span>
<span class="token comment">// initializing the number variable to store the required number.</span>
<span class="token keyword">var</span> number<span class="token punctuation">,</span> predecessor <span class="token keyword">int</span>
<span class="token comment">// assigning value to the number variable.</span>
number <span class="token operator">=</span> <span class="token number">49</span>
<span class="token comment">// implementing the logic</span>
predecessor <span class="token operator">=</span> number <span class="token operator">-</span> <span class="token number">1</span>
<span class="token comment">// Print the result</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">" predecessor of"</span><span class="token punctuation">,</span> number<span class="token punctuation">,</span> <span class="token string">"is "</span><span class="token punctuation">,</span> predecessor <span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
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
<div class="execute"></div><div class="code-mirror language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token comment">// GOLANG PROGRAM TO FIND THE PREDECESSOR OF AN INTEGER NUMBER USING LIBRARY FUNCTION</span>
<span class="token comment">// fmt package allows us to print anything on the screen.</span>
<span class="token keyword">package</span> <span class="token namespace">main</span>
<span class="token keyword">import</span> <span class="token string">"fmt"</span>
<span class="token comment">// function prototype</span>
<span class="token comment">// GO program execution starts with the function main()</span>
func <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">// function call</span>
<span class="token function">predecessor</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
func <span class="token function">predecessor</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Golang program to find the predecessor of an integer number using library function"</span><span class="token punctuation">)</span>
<span class="token comment">// declare and initialize the variable</span>
<span class="token keyword">var</span> c <span class="token keyword">int</span>
c <span class="token operator">=</span> <span class="token number">4</span>
<span class="token comment">// print the result</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"predeccessor of the number"</span><span class="token punctuation">,</span>c<span class="token punctuation">,</span><span class="token string">"="</span><span class="token punctuation">,</span>c <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
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
<div class="execute"></div><div class="code-mirror language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token comment">// GOLANG PROGRAM TO FIND THE PREDECESSOR OF AN INTEGER NUMBER USING LIBRARY</span>
<span class="token comment">// fmt package allows us to print anything on the screen.</span>
<span class="token keyword">package</span> <span class="token namespace">main</span>
<span class="token keyword">import</span> <span class="token string">"fmt"</span>
func <span class="token function">predecessor</span><span class="token punctuation">(</span>number <span class="token keyword">int</span><span class="token punctuation">)</span> <span class="token keyword">int</span> <span class="token punctuation">{</span>
<span class="token keyword">var</span> n <span class="token keyword">int</span>
n <span class="token operator">=</span> number <span class="token operator">-</span> <span class="token number">1</span>
<span class="token keyword">return</span> n
<span class="token punctuation">}</span>
<span class="token comment">// calling the main() function</span>
func <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Golang program to find the predecessor of an integer number using library function"</span><span class="token punctuation">)</span>
<span class="token comment">// declare the variables</span>
<span class="token keyword">var</span> number<span class="token punctuation">,</span> previous <span class="token keyword">int</span>
<span class="token comment">// initialize the number variable</span>
number <span class="token operator">=</span> <span class="token number">66</span>
<span class="token comment">// implementing the logic.</span>
<span class="token comment">// function call</span>
previous <span class="token operator">=</span> <span class="token function">predecessor</span><span class="token punctuation">(</span>number<span class="token punctuation">)</span>
<span class="token comment">// print the result</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">" predecessor of"</span><span class="token punctuation">,</span> number<span class="token punctuation">,</span> <span class="token string">"is "</span><span class="token punctuation">,</span> previous <span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
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
