C and JAVA Program of Finding LCM and HCF of Two Numbers

C Program to find LCM and HCF of two numbers


#include <stdio.h>
int main()
{
    int n1, n2, i, gcd, lcm;

    printf("Enter two positive integers: ");
    scanf("%d %d",&n1,&n2);

    for(i=1; i <= n1 && i <= n2; ++i)
    {
        // Checks if i is factor of both integers
        if(n1%i==0 && n2%i==0)
            gcd = i;
    }
    printf("The GCD of two numbers %d and %d is %d\n",n1,n2,gcd);

    lcm = (n1*n2)/gcd;
    printf("The LCM of two numbers %d and %d is %d.", n1, n2, lcm);

    return 0;
}


Java Program to find LCM and HCF of two numbers



public class LCM {
    public static void main(String[] args) {

        int n1 = 72, n2 = 120, gcd = 1;

        for(int i = 1; i <= n1 && i <= n2; ++i)
        {
            // Checks if i is factor of both integers
            if(n1 % i == 0 && n2 % i == 0)
                gcd = i;
        }
        System.out.printf("The GCD of %d and %d is %d.", n1, n2, gcd);
        int lcm = (n1 * n2) / gcd;
        System.out.printf("The LCM of %d and %d is %d.", n1, n2, lcm);
    }
}





Comments

Popular posts from this blog

C Program to Print a trapezium pattern

C Program to Print Pattern 1*2*3*4 9*10*11*12 13*14*15*16 5*6*7*8

Glimpse Of SPYDER | Mahesh Babu | A R Murugadoss | Rakul Preet Singh | Harris Jayaraj | #SPYDER Trailer , Teaser