Skip to main content

Least Common Denominator (LCD) in C++

I'm reposting this blog from my old blogsite which I have not been maintaining for long period of time.

A friend of mine asked me to give him code of LCD(Least Common Denominator) program in C++ via text. I Googled and I found the ff. (sorry I forgot the website) code with some modification myself:

#include <iostream>
using namespace std;

int main() {
    int a, b, d, min;

    cout << "Enter first number: ";
    cin >> a;
    cout << "Enter second number: ";
    cin >> b;

    min = a > b ? b : a;

    for(d = 2; d<min; d++) {
        if(((a%d)==0) && ((b%d)==0))
        break;
     
        if(d==min) {
            cout << "No common denominators." << endl;
            return 0;
        }
    }
   
    cout << "The lowest common denominator is " << d << "." << endl;
    return 0;


I tested this code using Dev-C++ editor with GNU C++ compiler and utilities.

Any suggestions for improvement of above codes are welcome. Thank you.

Comments

Popular posts from this blog

Guestbook in PHP and MySQL

Introduction I'm going to create an example of guestbook system using in PHP and MySQL. On the Web, guestbook is a logging system that lets visitor post their message. A visitor can post their thoughts or expressions. In general, it's not a requirement that a visitor create an account.

Phonebook Application with PHP and MySQL CRUD Operation

Definition It is a simple Web-based phonebook application which stores and displays ID, Name, Contact No, and Address of a person. It is also capable of updating/editing and deleting phonebook entries. Database CREATE TABLE `phonebook` (     `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,     `name` VARCHAR(50) NOT NULL,     `phone_no` VARCHAR(15) NOT NULL,     `address` VARCHAR(50) NOT NULL,     `date_created DATETIME` NOT NULL DEFAULT ‘0000-00-00 00:00:00’,     PRIMARY KEY(`id`) );

CRUD in Spring Boot with Spring Data JPA

T utorial about CRUD operations in Spring Boot with Spring Data JPA. Application developed in this tutorial performs adding, editing, updating and deleting contacts. Continue reading here .