Write a C C++ program to find HCF and LCM

 

Flowchart

HCF and LCM of two numbers flowchart

 

Coding in C

#include<stdio.h>
int main()
{
int a,b,i=1,hcf,lcm;
printf(“Enter two numbers: “);
scanf(“%d%d”,&a,&b);
while(i<=a&&i<=b)
{
if(a%i==0&&b%i==0)
{
hcf=i;
}
i++;
}
lcm=(a*b)/hcf;
printf(“HCF = %d\n”,hcf);
printf(“LCM = %d\n”,lcm);
return 0;
}

 

Coding in C++

#include<iostream>
using namespace std;
int main()
{
int a,b,i=1,hcf,lcm;
cout<<“Enter two numbers: “;
cin>>a>>b;
while(i<=a&&i<=b)
{
if(a%i==0&&b%i==0)
{
hcf=i;
}
i++;
}
lcm=(a*b)/hcf;
cout<<“HCF = “<<hcf<<“\n”;
cout<<“LCM = “<<lcm<<“\n”;
return 0;
}

 

Output

HCF and LCM of given number output

 

Published by

admin

I am a computer teacher, programmer and web developer