answersLogoWhite

0

/* Program to find LCM/HCF of 15 Nos in C++/ C (replace cin with scanf & cout with printf for c) */

/* Developed by - Kishore Kr. Banerjee - papillon_kish@Yahoo.com */

#include <iostream.h>

#include <conio.h>

main()

{

int num[3],i,j,p,q,tmp,n1,n2,LCM,rem,flag;

clrscr();

for(i=0;i<3;i=i+1)

{

cout<<"Enter No - "<<i+1<<"=";

cin>>num[i];

}

clrscr();

n1=num[0];

p=n1;

for(i=1;i<3;i=i+1)

{

n2=num[i];

/* Finding HCF */

q=n2;

if(p<q)

{

tmp=q;

q=p;

p=tmp;

}

while(p%q!=0)

{

rem=p%q;

q=p;

p=rem;

if(p<q)

{

tmp=q;

q=p;

p=tmp;

}

}

/*finding LCM */

LCM=1;

for(j=1;n1%j==0n2%j==0;j=j+1)

{

if(n1%j==0)

{

n1=n1/j;

flag=1;

}

if(n2%j==0)

{

n2=n2/j;

flag=1;

}

if(flag==1)

{

LCM=LCM*j;

}

}

LCM=LCM*n1*n2;

n1=LCM;

}

cout<<"the LCM ="<<LCM;

cout<<"the hcf ="<<q;

getch();

}

-----

int gcd (int a, int b)

{

. int tmp;

. if (a<0) a= -a;

. if (b<0) b= -b;

. if (a<b) tmp= a, a= b, b= tmp;

. while (b) {

. . tmp= a%b;

. . a= b;

. . b= tmp;

. }

. return a;

}

int gcd_n (int n, const int *vect)

{

. int i, gcdtmp;

. for (i=0, gcdtmp=0; i<n && gcdtmp!=1; ++i)

. . gcdtmp= gcd (gcdtmp, vect[i]);

. return gcdtmp;

}

int LCM (int a, int b)

{

. int d= gcd (a, b);

. if (d==0) return d;

. else return a/d*b;

}

int lcm_n (int n, const int *vect)

{

. int i, lcmtmp;

. for (i=0, lcmtmp=1; i<n; ++i)

. . lcmtmp= LCM (lcmtmp, vect[i]);

. return lcmtmp;

}

User Avatar

Wiki User

13y ago

Still curious? Ask our experts.

Chat with our AI personalities

BeauBeau
You're doing better than you think!
Chat with Beau
TaigaTaiga
Every great hero faces trials, and you—yes, YOU—are no exception!
Chat with Taiga
FranFran
I've made my fair share of mistakes, and if I can help you avoid a few, I'd sure like to try.
Chat with Fran

Add your answer:

Earn +20 pts
Q: Write a program that accepts 15 different numbers and find the LCM and HCM?
Write your answer...
Submit
Still have questions?
magnify glass
imp