Friday 3 June 2016

Write a function LCM() that receives two integer arguments and return LCM

#include<iostream.h>
#include<conio.h>
int LCM(int n1, int n2);
void main()
{
        int num1, num2, lcm;
        do
        {
                clrscr();
                cout<<"Enter two integers: ";
                cin>>num1>>num2;
        }
        while (num1 <= 0 || num2 <= 0)
        lcm= LCM(num1, num2);
        cout<<"\n LCM of  "<<num1<<" and "<<num2<<" = "<<lcm;
        getch();
}
int LCM (int n1, int n2)
{
        int prod, temp, gcd;
        prod = n1 * n2;
        while(n2 != 0)
        {
                if((n1 % n2) == 0)
                        break;
                else
                {
                        temp = n1 % n2;
                        n1 = n2;
                        n2 = temp;
                 }
        }
        gcd = n2;
        return prod / gcd;
}
Share:

0 comments:

Post a Comment

Popular Posts

Recent Posts

Unordered List

Text Widget

Pages

About Me

My photo
A group of two, with a little hope of helping beginners to learn how to code :)
Powered by Blogger.

Comments

Facebook

Ad Home

Follow Us

Random Posts

Sponsor

Recent Posts

Header Ads

Popular Posts

Copyright © Functions in C++ | Powered by Blogger

Design by ThemePacific | Blogger Theme by NewBloggerThemes.com | Distributed By Blogger Templates20