Please login


Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video

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: ");
printf("Enter the second number: ");
if(num1 > num2)
num1 -= num2;
num2 -= num1;
printf("The GCD of the given numbers = %d",num1);
return 0;


  • 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.
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