Need a project done?

C++ Programming Developer

Search This Blog

GCM - Greatest Common Multiple Program in C++

Program to calculate GCM ( Greatest Commmon Multiple ) in C++ is given below:

// GCM - Greateset Common Multiple program in C++
#include "stdafx.h" //COMMENT THIS LINE IF IT CAUSES ERROR
#include <iostream>



using namespace std;

int gcm(int,int);

int main()
{
cout << "This program calculates GCM (Greatest Common Multiple) of two numbers \n\nEnter first number: "; int no1; cin >> no1;
cout << "Enter 2nd number: "; int no2; cin >> no2;

cout << "Greatest common multiple of " << no1 << " and " << no2 << " is " << gcm(no1,no2) << '.' << endl;

return 0;
}

int gcm(int no1, int no2){
int greater;
if (no1 > no2)
greater = no1;
else
greater = no2;

int gcm;

for (int i = 1; i<=greater; i++){
if ( (no1 % i == 0)  &&  (no2 % i == 0) )
gcm = i;
}

return gcm;
}

No comments:

Post a Comment

"Don't let anyone ever make you feel like you don't deserve what you want."