C++ Program to find GCD and LCM of two Given Numbers

This program will give you the gcd and lcm of two given numbers using C++ code. The logic used for this particular program is very simple and easy to understand. You need to take 5 integers values and GCD and LCM along with it. Using Conio.h Library is up to you. # include is mainly used for clearing the screen of the output. If you remove conio.h then the clrscr(); will not work in the program and it will return an error.

#include
#include
void main()
{
int x,y,a,b,t,gcd,lcm;
clrscr();
cout<<"Enter value of a and b:n";
cin>>a>>” “>>b;
x=a;
y=b;
while(b!=0)
{
t=b;
b=a%b;
a=t;
gcd=a;
lcm=(x*y)/gcd;
}
cout<<"ngcd is:"<getch();
}

This program is successfully executed in the c++ borand software. If any error is coming, then you are typing the program incorrectly. Simply copy paste the program in a notepad file and save it as you save in borland. Then open the notepad file using the software, now you can see the whole code already written in the window.

Add Comment