Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » Homework HelpRSS Feeds

Program to find least common multiple and greatest common factor of 2 numbers

Posted By: Easy Tutor     Category: C Programming     Views: 12366

Write a program that finds least common multiple and greatest common factor of 2 numbers.

Code for Program to find least common multiple and greatest common factor of 2 numbers in C Programming

#include <stdio.h>
#include <conio.h>
#include <math.h>

int num1,num2,choice,temp1,temp2;

void lcm(){
      int i,lcm=1;
      num1 = temp1;
      num2 = temp2;
      printf("\n\nL.C.M OF NUMBER\n");
      while(num1>1 || num2>1){
        for(i=2;i<100;i++){
        if(num1%i==0 || num2%i==0){
        printf("\n%4d | %4d ,  %4d",i,num1,num2);
        if(num1%i==0){
            num1     = num1/i;
         }
        if(num2%i==0){
            num2     = num2/i;
        }
        lcm = lcm * i;
        }
          }
    }
    printf("\nÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ");
    printf("\n%4d | %4d ,  %4d",lcm,1,1);
    printf("\n\nLCM IS %d",lcm);
}

void gcf(){
      int i,gcfnum1=1,gcfnum2=1,cntnum1[100]={0},cntnum2[100]={0},gcf=1;
      num1 = temp1;
      num2 = temp2;
      printf("G.C.F. OF NUMBER\n");
      //For num1while(num1>1){
        for(i=2;i<100;i++){
        if(num1%i==0){
        printf("\n%4d | %4d ",i,num1);
        num1     = num1/i;
        gcfnum1 = gcfnum1 * i;
        cntnum1[i]++;
        }
          }
    }
    printf("\nÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ");
    printf("\n%4d | %4d ",gcfnum1,1);
    printf("\nGCF IS %d",gcfnum1);
    printf("\n%-4d  =  ",temp1);
    for(i=2;i<100;i++){
        if(cntnum1[i]>0)
           printf(" (%3d^%-3d) ",i,cntnum1[i]);
    }


    //For num2
    printf("\n\n");
    while(num2>1){
        for(i=2;i<100;i++){
        if(num2%i==0){
        printf("\n%4d | %4d ",i,num2);
        num2     = num2/i;
        gcfnum2 = gcfnum2 * i;
        cntnum2[i]++;
        }
          }
    }
    printf("\nÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ");
    printf("\n%4d | %4d ",gcfnum2,1);
    printf("\n\nGCF IS %3d",gcfnum2);
    printf("\n%-4d  =  ",temp2);
    for(i=2;i<100;i++){
        if(cntnum2[i]>0)
           printf(" (%3d^%-3d) ",i,cntnum2[i]);
    }

    //GCF logicfor(i=2;i<100;i++){
         if(cntnum1[i]==0 || cntnum2[i]==0)
        continue;
         elseif(cntnum1[i]<cntnum2[i])
        gcf = gcf * pow(i,cntnum1[i]);
         elseif(cntnum2[i]<cntnum1[i])
        gcf = gcf * pow(i,cntnum2[i]);
         elseif(cntnum1[i]>0)
        gcf = gcf * pow(i,cntnum1[i]);
         elseif(cntnum2[i]>0)
        gcf = gcf * pow(i,cntnum2[i]);
    }

    printf("\n\nGCF of  %d  and  %d  is  %d",temp1,temp2,gcf);
}

void main(){
    void lcm(void);
    void gcf(void);
    clrscr();
    printf("*****LCM And GCF of Given Numbers*****");
    up:
    printf("\n\nEnter num1 : ");
    scanf("%d",&num1);
    printf("Enter num2 : ");
    scanf("%d",&num2);
    temp1 = num1;
    temp2 = num2;
    while(1){
    clrscr();
    printf("\n\nChoose your Choice\n");
    printf("1)  L.C.M\n");
    printf("2)  G.C.F\n");
    printf("3)  Scan New Numbers\n");
    printf("4)  Exit\n");
    printf("Enter your choice : ");
    scanf("%d",&choice);
    switch(choice){
        case 1 : clrscr();
             lcm();
             break;
        case 2 : clrscr();
             gcf();
             break;
        case 3 : goto up;
        case 4 :
             exit(1);
        default: printf("\n\nINVALID CHOICE\n");
    }
       getch();
      }
}
  
Share: 



Easy Tutor
Easy Tutor author of Program to find least common multiple and greatest common factor of 2 numbers is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!