Search

GCD of two numbers in C || C Programs

Code -

#include <stdio.h> 
#include <conio.h> 
int main()  
{  
 // declare the variables 
 int n1, n2, i, GCD_Num;  
    printf ( " Enter any two numbers: \n ");  
    scanf ( "%d %d", &n1, &n2);   
 
 // use for loop 
 for( i = 1; i <= n1 && i <= n2; ++i)  
    {  
 if (n1 % i ==0 && n2 % i == 0)  
            GCD_Num = i; /* if n1 and n2 is completely divisible by i, the divisible number will be the GCD_Num */ 
    }  
 // print the GCD of two numbers 
    printf (" GCD of two numbers %d and %d is %d.", n1, n2, GCD_Num);  
 return 0;  
}  

Output -

Enter any two numbers:
 96
36
 GCD of two numbers 96 and 36 is 12.
2 views0 comments

Related Posts

See All

Program of Random Number in C || C Programs

Code - #include <stdio.h> #include <conio.h> #include <stdlib.h> void main() { // use rand() function to generate the number printf (" The random number is: %d", rand()); pri

Getchar() function in C || C Programs

Code - #include <stdio.h> #include <conio.h> void main() { char c; printf ("\n Enter a character \n"); c = getchar(); // get a single character printf(" You have passed ")

  • 1024px-Telegram_logo.svg
  • download
  • YouTube