Menu driven program to obtain LCM or HCF of a given number of inputs.
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
{
int i,j,n,lcm,lo,hi=1,arg['z']; //lo= loop operator
cout<<"\nEnter No. Terms of Which LCM is Desired....(2 or more):\n";
cin>>n;
cout<<"\nEnter the Nos. of Which LCM is to be Found:\n";
for(i=0;i<n;++i)
{
cin>>arg[i];//take input of all numbers
}
for(i=0;i<n;++i)
{
hi=hi*arg[i]; //find the product of all the inputs (highest possibility for LCM)
}
for(i=1,lo=arg[0];i<n;++i)
{
if(lo<arg[i])
{
lo=arg[i];//to find the lowest possibility for LCM
}
}
for(lo;lo<=hi;++lo)
{
for(j=0;j<n;++j)
{
if(lo%arg[j]!=0) //to check divisibility of each number starting form lowest possibility to highest to confirm LCM
{
break; //even a single no. is indivisible process stops and restarts
}
}
if(j==n) // complete cyle of j loop means the no. is divisible by all hence LCM.
{
cout<<"The LCM is : "<<lo;
getch();
exit(0);
}
}
}
{
int i,j,n,gcd,hi,arg['z']; //lo= loop operator
cout<<"\nEnter No. Terms of Which LCM is Desired....(2 or more):\n";
cin>>n;
cout<<"\nEnter the Nos. of Which LCM is to be Found:\n";
for(i=0;i<n;++i)
{
cin>>arg[i];//take input of all numbers
}
for(i=1,hi=arg[0];i<n;++i)
{
if(hi>arg[i])
{
hi=arg[i];//to find the highest possibility for GCD
}
}
for(i=hi;i>=hi;i--)
{
for(j=0;j<n;j++)
{
if(arg[j]%i!=0)
{
break;
}
}
if(j==n)
{
cout<<"The HCF is : "<<i;
getch();
exit(0);
}
}
}
{
int opt;
menu:
clrscr();
cout<<"Choose from one of the option:-\n1.Find LCM <Press 1>\n2.Find GCD <Press 2>\n";
cin>>opt;
switch(opt)
{
case 1: lcm();break;
case 2: gcd();break;
default: goto menu;
}
getch();
}
No comments:
Post a Comment