समस्या
दो पूर्णांकों का महत्तम सामान्य भाजक (GCD) और लघुत्तम सामान्य गुणक (LCM) खोजने के लिए यूक्लिड के एल्गोरिथम को लागू करें और दिए गए पूर्णांकों के साथ परिणाम आउटपुट करें।
समाधान
दो पूर्णांकों का महत्तम सामान्य भाजक (GCD) और लघुत्तम सामान्य गुणक (LCM) खोजने के लिए यूक्लिड के एल्गोरिथम को लागू करने का समाधान निम्नानुसार है:
GCD और LCM खोजने के लिए उपयोग की जाने वाली तर्क:
if(firstno * secondno != 0){
gcd = gcd_rec(firstno, secondno);
printf("\nThe GCD of %d and %d is %d\n", firstno, secondno, gcd);
printf("\nThe LCM of %d and %d is %d\n", firstno, secondno, (firstno * secondno) / gcd);
}
कॉल किया गया फंक्शन:
int gcd_rec(int x, int y){
if (y == 0)
return x;
return gcd_rec(y, x % y);
}
प्रोग्राम
निम्नलिखित C प्रोग्राम दो पूर्णांकों का GCD और LCM खोजने के लिए यूक्लिड के एल्गोरिथम को लागू करता है:
#include <stdio.h>
int gcd_rec(int, int);
void main(){
int firstno, secondno, gcd;
printf("Enter the two no.s to find GCD and LCM:");
scanf("%d%d", &firstno, &secondno);
if(firstno * secondno != 0){
gcd = gcd_rec(firstno, secondno);
printf("\nThe GCD of %d and %d is %d\n", firstno, secondno, gcd);
printf("\nThe LCM of %d and %d is %d\n", firstno, secondno, (firstno * secondno) / gcd);
}
else
printf("One of the entered no. is zero:Quitting\n");
}
/* Function for Euclid's Procedure */
int gcd_rec(int x, int y){
if (y == 0)
return x;
return gcd_rec(y, x % y);
}
आउटपुट
Enter the two no.s to find GCD and LCM: 4 8 The GCD of 4 and 8 is 4 The LCM of 4 and 8 is 8