A Shell program To Find The GCD | Linux
The given problem asked in fico placement interview round. Find the gcd of two given numbers using shell scripting
We are given two numbers A and B and now task is to find the Greatest Common divisor (gcd) of two given number using shell scripting .
Asked in interview : FICO
Example:
Input 25 15 Output 5
// Script for finding gcd of two number // echo is for printing the message echo Enter two numbers with space in between // read for scanning read a b // Assigning the value of a to m m = $a // Condition checking if b greater than m // If yes the replace the value of m assign a new value if [ $b -lt $m ] then m = $b fi // In do while loop we are checking the gcd while [ $m -ne 0 ] dox = `expr $a % $m` y = `expr $b % $m` // If x and y both are 0 then we complete over // process and we print the gcd if [ $x -eq 0 -a $y -eq 0 ] then // Printing the greatest gcd of two given number echo gcd of $a and $b is $m breakfi m = `expr $m - 1` done |
chevron_right
filter_none
This article is contributed by Ajay Puri. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- How to find time taken by a command/program on Linux Shell?
- Introduction to Linux Shell and Shell Scripting
- Making your own Linux Shell in C
- Developing a Linux based shell
- Basic Shell Commands in Linux
- Bash shell script to find sum of digits
- C program for pipe in Linux
- Linux Kernel Module Programming: Hello World Program
- Program to show that Linux provides time sharing environment to processes
- find command in Linux with examples
- Conditional Statements | Shell Script
- String Operators | Shell Script
- Basic Operators in Shell Scripting
- Array Basics in Shell Scripting | Set 1
- Looping Statements | Shell Script



