Showing posts with label LCM. Show all posts
Showing posts with label LCM. Show all posts

Write a c program to find out L.C.M. of two numbers.

How To Write a c program to find out L.C.M. of two numbers.



#include<stdio.h>
int main(){
  int n1,n2,x,y;
  printf("\nEnter two numbers:");
  scanf("%d %d",&n1,&n2);
  x=n1,y=n2;
  while(n1!=n2){
      if(n1>n2)
           n1=n1-n2;
      else
      n2=n2-n1;
  }
  printf("L.C.M=%d",x*y/n1);
  return 0;
}

C Program To Find LCM and HCF Of Two Number Using Function - 2

How to write a C Program To Find LCM and HCF Of Two Number Using Function in C Programming Language ?


Solution For C Program :

/*C Program To Find LCM and HCF Of Two Number Using Function.*/
#include<stdio.h>
#include<conio.h>
int lcm(int a,int b);
int hcf(int a,int b);
void main()
    {
    int a,b,ch,z;
    char choice='y';
    while(choice=='y' || choice=='Y')
       {
       printf("1.LCM");
       printf("\n2.HCF");
       printf("\nEnter your choice:=");
       scanf("%d",&ch);
       if(ch==1 || ch==2)
          {
          printf("\nEnter first number:=");
          scanf("%d",&a);
          printf("\nEnter second number:=");
          scanf("%d",&b);
          }
       switch(ch)
          {
          case 1:z=hcf(a,b);
             printf("LCM:=%d",z);
             break;
          case 2:
             z=lcm(a,b);
             printf("HCF:=%d",z);
             break;
          default:
             printf("Wrong choice");
          }
          fflush(stdin);
          printf("\nDO YOU WANT TO CONTINUE Y/N:=");
          scanf("%c",&choice);
       }
    }
int lcm(int a,int b)
    {
    int lcm,c;
    while(a!=b)
       {
       if(a>b)
          {
          c=a-b;
          a=c;
          lcm=c;
          }
       if(b>a)
          {
          c=b-a;
          b=c;
          lcm=c;
          }
       }
       return(lcm);
    }
int hcf(int a,int b)
    {
    int hcf,c,a1,b1;
    a1=a;
    b1=b;
    c=lcm(a,b);
    hcf=(b1*a1)/c;
    return(hcf);
    }

You may also learn these C Program/Code :

C Program To Swap Two Numbers Without Using Third Variable

C Program to find LCM and HCF Of Two Number Using Recursion - 3

How to write a C Program to find LCM and HCF Of Two Number Using Recursion in C Programming Language ?


Solution For C Program :

/*C Program to find LCM and HCF Of Two Number Using Recursion.*/

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,old_rem,cur_rem,lcm,hcf;
int hccf(int, int);
clrscr();
printf("Enter the two no:");
scanf("%d%d",&a,&b);
if(a<b)
  {
  old_rem=b;
  cur_rem=a;
  }
  else
  {
  old_rem=a;
  cur_rem=b;
  }
hcf=hccf(old_rem,cur_rem);
lcm=(a*b)/hcf;
printf("LCM=%d\tHCF=%d",lcm,hcf);
getch();
}

hccf(int old_rem,int cur_rem)
    {
    int new_rem,hcf;
    new_rem=old_rem%cur_rem;
    old_rem=cur_rem;
    cur_rem=new_rem;
    if(new_rem==0)
      {
      return(old_rem);
      }
    else
      {
      hcf=hccf(old_rem,cur_rem);
      }
      return(hcf);
    }


You may also learn these C Program/Code :

C Program To Swap Two Numbers Without Using Third Variable

C Program To Find LCM and HCF Of Two Number -1

How to write a C Program To Find LCM and HCF Of Two Number -1 in C Programming Language ?


Solution For C Program :

/*C Program To Find LCM and HCF Of Two Number -1.*/

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,old_rem,cur_rem,new_rem,lcm,hcf;
clrscr();
printf("Enter the two no:");
scanf("%d%d",&a,&b);
if(a<b)
  {
  old_rem=b;
  cur_rem=a;
  }
  else
  {
  old_rem=a;
  cur_rem=b;
  }
do{
  new_rem=old_rem%cur_rem;
  old_rem=cur_rem;
  cur_rem=new_rem;
  }while(new_rem!=0);
hcf=old_rem;
lcm=(a*b)/hcf;
printf("LCM=%d\tHCF=%d",lcm,hcf);
getch();
}


You may also learn these C Program/Code :

C Program To Swap Two Numbers Without Using Third Variable