Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

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.

 

#include <stdio.h>

int main()
{
int num1, num2;

printf("Enter the first number: ");
scanf("%d",&num1);
printf("Enter the second number: ");
scanf("%d",&num2);
while(num1!=num2)
{
if(num1 > num2)
num1 -= num2;
else
num2 -= num1;
}
printf("The GCD of the given numbers = %d",num1);
return 0;
}

Output

  • 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.
gcd
Disclaimer-: The questions provided on this page are only model practice questions there is no surety that these questions have been previously asked in any company placement papers, these questions here only have the sole purpose to make you practice coding questions