Exercise:
Write a C Program to find GCD of two numbers.
1. Calculate Using for loop and if Statement
Click Here to View the Solution!
#include <stdio.h>
int main() {
int num1, num2, i, gcd;
printf("Enter two integers: ");
scanf("%d %d", &num1, &num2);
for(i=1; i <= num1 && i <= num2; ++i) {
//Checks if i is factor of both integers
if(num1%i==0 && num2%i==0)
gcd = i;
}
printf("G.C.D of %d and %d is %d", num1, num2, gcd);
return 0;
}
Click Here to View the Output!
Enter two integers: 6 4
G.C.D of 6 and 4 is 2
Click Here to View the Explanation!
- This program is used to find out the GCD of two numbers using a
for loop
and an if statement. - In
main()
, the program requests the user to enter twointeger
values and stores them in the integer variablesnum1
andnum2
using thescanf()
function. - A
for loop
is initialized that will continue the iterations of the loop until the value ofi
is less than or equal tonum1
andnum2
. - Every iteration will determine whether i is exactly divisible by both the integers
num1
andnum2
(factors). - If the condition is true, the value of
i
will be stored in an integer variablegcd
. - The loop will continue until the greatest common divisor of
num1
andnum2
is stored in the variablegcd
and is displayed. - The
return 0 statement
is used to exit the program execution.
2. Calculate Using while loop and if-else
Click Here to View the Solution!
#include <stdio.h>
int main() {
int num1, num2;
printf("Enter two positive integers: ");
scanf("%d %d",&num1,&num2);
while(num1!=num2) {
if(num1 > num2)
num1 -= num2;
else
num2 -= num1;
}
printf("GCD = %d",num1);
return 0;
}
Click Here to View the Output!
Enter two positive integers: 8 4
GCD = 4
Click Here to View the Explanation!
- This program is used to find out the GCD of two numbers using the
while loop
and theif…else statement
. - In the
main method
, the program requests the user to enter two positive integers and stores them in the integer variablesnum1
andnum2
using thescanf()
function. - A
while loop
is used in this program which is a better way of finding the GCD. - In the
while loop
, a condition is set which will continue the loop untilnum1
is not equal tonum2
. - The
while loop
holds anif…else
statement which will subtract the smaller integer value from the larger integer value and store the result in the larger integer variable as per theif…else
condition. - The loop exits once
num1
becomes equal tonum2
. And the GCD is displayed on the screen. - The
return 0 statement
is used to exit the program execution.
3. Calculate for both positive and negative numbers
Click Here to View the Solution!
#include <stdio.h>
int main() {
int num1, num2;
printf("Enter two integers: ");
scanf("%d %d",&num1,&num2);
// Change negative number to positive number
num1 = ( num1 > 0) ? num1 : -num1;
num2 = ( num2 > 0) ? num2 : -num2;
while(num1!=num2) {
if(num1 > num2)
num1 -= num2;
else
num2 -= num1;
}
printf("GCD = %d",num1);
return 0;
}
Click Here to View the Output!
Enter two integers: 6 9
GCD = 3
Click Here to View the Explanation!
- This program is used to find out the GCD of two integer values by accepting only positive values from the user.
- In
main()
, the program requests the user to enter two integer values and stores them in the integer variablesnum1
andnum2
using thescanf()
function. Ternary operators
are used to check whether the values entered by the user are negative. If true, the signs of the numbers are changed to positive.- In a
while loop
, a condition is set which will continue the loop untilnum1
is not equal tonum2
. - The
while loop
holds anif…else statement
which will subtract the smaller integer value from the larger integer value and store the result in the larger integer variable as per theif…else condition
. - The loop exits once
num1
becomes equal tonum2
and the GCD is displayed on the screen. - The
return 0 statement
is used to exit the program execution.