Write a program to find LCM of two number

Write a program to find LCM of two number
using function

  1. #include <stdio.h>
  2. int gcd(int, int);

  3. int main() {
  4.   int n1, n2, lcm_gcd, lcm;

  5.   printf("Enter two numbers to find LCM : ");
  6.   scanf("%d %d", &n1, &n2);

  7.   lcm_gcd = gcd(n1, n2);
  8.   lcm = (n1*n2)/lcm_gcd;

  9.   //printf("\n%d and %d of GCD is = %d\n", n1, n2, lcm_gcd);
  10.   printf("\n%d and %d of LCM is = %d\n", n1, n2, lcm);

  11.   return 0;
  12. }

  13. int gcd(int n1, int n2) {
  14.   if (n1 == 0) {
  15.     return n2;
  16.   }

  17.   while (n2 != 0) {
  18.     if (n1 > n2) {
  19.       n1 = n1-n2;
  20.     }
  21.     else {
  22.       n2 = n2-n1;
  23.     }
  24.   }
  25.   return n1;
  26. }


Recursion recursion

  1. #include <stdio.h>
  2. int gcd(int, int);

  3. int main() {
  4.   int n1, n2, lcm_gcd, lcm;

  5.   printf("Enter two numbers to find LCM : ");
  6.   scanf("%d %d", &n1, &n2);

  7.   lcm_gcd = gcd(n1, n2);
  8.   lcm = (n1*n2)/lcm_gcd;

  9.   //printf("\n%d and %d of GCD is = %d\n", n1, n2, lcm_gcd);
  10.   printf("\n%d and %d of LCM is = %d\n", n1, n2, lcm);

  11.   return 0;
  12. }

  13. int gcd(int m, int n) {
  14.   if (n == 0) {
  15.     return m;
  16.   }
  17.   else {
  18.     return gcd(n, m%n);
  19.   }
  20. }

একটি মন্তব্য পোস্ট করুন