Showing posts with label HCF. Show all posts
Showing posts with label HCF. Show all posts

Write a c program to find out H.C.F. of two numbers.

How To Write a c program to find out H.C.F. of two numbers in C Program.


#include<stdio.h>
int main(){
int x,y,m,i;
printf("Insert any two number: ");
scanf("%d%d",&x,&y);
m=x
for(i=m;i>=1;i--){
if(x%i==0&&y%i==0){
printf("\nHCF of two number is : %d",i) ;
break;
}
}
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