Lowest common multiple

    0

    0

    Giovanny Gongora

    Codiga's C Recipes

    Calculate the LCM (Lowest common multiple) of two numbers entered by the user.

    The LCM of two integers n1 and n2 is the smallest positive integer that is perfectly divisible by both n1 and n2 (without a remainder). For example, the LCM of 72 and 120 is 360.

    #include <stdio.h>
    
    int lcm() {
      int n1, n2, max;
      printf("Enter two positive integers: ");
      scanf("%d %d", &n1, &n2);
      // maximum number between n1 and n2 is stored in max
      max = (n1 > n2) ? n1 : n2;
      while (1) {
        if (max % n1 == 0 && max % n2 == 0) {
          printf("The LCM of %d and %d is %d.", n1, n2, max);
          break;
        }
        ++max;
      }
      return 0;
    }
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Explore
    • Cookbooks
    • Playground
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.