Find The GCD Of Any Two Numbers

GCD

GCD stands for Greatest Common Divisor. This is a basic operation for students. I am going to show you how it's calculated by using a flow chart. This solution is based on Euclid's GCD.
 
SYSTEM REQUIRMENT
  1.  Windows operating system
  2. GCC compiler
FLOWCHART

This flow chart shows you the logic of the code.

 

CODE  IN C-PROGRAMING
  1. // finding gcd of any two Number      
  2. #include <stdio.h>                    // including INPUT AND  OUTPUT HEADER FILE    
  3.     
  4. int main()    
  5. {    
  6.     int f_Num,s_Num,gcd,tempVar;    
  7.     
  8.     printf("Enter first Number :");    
  9.     scanf("%d",&f_Num);             // takeing first number from user    
  10.     
  11.     printf("Enter Second Number :");    
  12.     scanf("%d",&s_Num);             // takeing scond number from user    
  13.     
  14.     if(f_Num < s_Num)                // echanging the number if fisrt number is greater than second number    
  15.     {    
  16.         tempVar = f_Num;    
  17.         f_Num = s_Num;    
  18.         f_Num = tempVar;    
  19.     }    
  20.     
  21.     printf("GCD(%d,%d) :",f_Num,s_Num );     
  22.     
  23.     while(!(s_Num == 0))            // logic of solveing  gcd    
  24.     {    
  25.         gcd = f_Num % s_Num;    
  26.         f_Num = s_Num;    
  27.         s_Num = gcd;    
  28.     }    
  29.     
  30.     printf("%d",f_Num);             // printing of gcd    
  31.     
  32.     return 0;    
  33. }   
OUTPUT
 
Ebook Download
View all
Learn
View all