C Programming Tutorial

 
 
 
 

Image

#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 num1
      while(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 logic
    for(i=2;i<100;i++){
         if(cntnum1[i]==0 || cntnum2[i]==0)
        continue;
         else if(cntnum1[i]<cntnum2[i])
        gcf = gcf * pow(i,cntnum1[i]);
         else if(cntnum2[i]<cntnum1[i])
        gcf = gcf * pow(i,cntnum2[i]);
         else if(cntnum1[i]>0)
        gcf = gcf * pow(i,cntnum1[i]);
         else if(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();
      }
}

 
 
Didn't find what you were looking for? Find more on Program to find least common multiple and greatest common factor of 2 numbers