Video courses for company/skill based Preparation
(Check all courses)Get Prime Video
Purchase mock tests for company/skill building
(Check all mocks)Get Prime mock
GCD of Two Numbers in C
C program to calculate the GCD(Greatest Common Divisor) of two numbers
In this program, we are required to find the greatest common divisor of any two given numbers that are input by the user.
- The logic of the following program is to subtract the smaller number from the larger number.
- The obtained result is then assigned to the variable holding the larger of the two numbers.
- The process is repested until num1 and num2 are equal.
int num1, num2;
printf("Enter the first number: ");
printf("Enter the second number: ");
if(num1 > num2)
num1 -= num2;
num2 -= num1;
printf("The GCD of the given numbers = %d",num1);
- We take two integers as input from the user. Here, in this case we have num1 = 56 and num2 = 48.
- The result is then displayed as shown in the following image.