C Programming Tutorial

Advertisements

Write program to display the gcd (greatest common divisior) of a given number in C



       #include<stdio.h>
       #include<conio.h>
       void main()
       {
              int num1,num2,i,gcd=1; 
              clrscr();

              printf("\nEnter 1st number : "); 
              scanf("%d",&num1);

              printf("\nEnter 2nd number : "); 
              scanf("%d",&num2);
              
              for(i=2; i<=num1 && i<=num2; i++)
              {
                     if(num1%i == 0 && num2%i==0)
                           gcd=i;
              }
              
              printf("The gcd is : %d",gcd);

              getch();
       }

   Output : 

              Enter 1st number : 36
              Enter 2nd number : 72
              The gcd is : 36




Advertisement