Create a Rust program to find the GCD (Greatest Common Divisor) of two numbers.
Create a Rust program to find the GCD (Greatest Common Divisor) of two numbers.
Student
I am Utpal Vishwas from Uttar Pradesh. Have completed my B. Tech. course from MNNIT campus Prayagraj in 2022. I have good knowledge of computer networking.
Sure, here is a Rust program to find the GCD (Greatest Common Divisor) of two numbers:
Rust
This program first checks if one of the numbers is 0. If it is, then the program returns 0. Otherwise, the program loops while b is not 0. In each iteration of the loop, the program swaps a and b and then calculates the remainder of b divided by a. The program continues looping until b is 0. Finally, the program returns a.
To run this program, you can save it as a
.rsfile and then compile and run it using the following commands:Code snippet
This will print the following output:
Code snippet
This indicates that the GCD of 12 and 18 is 6.