Wednesday, July 20, 2011

Program to find LCM and HCF of two no using function

/*Serial No.76     [swami50.cpp]*/

#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);
    }

No comments:

Post a Comment

If you have any doubt, feel free to ask...