GoogleTag

Google Search

[Programming] Question 27: Write a C program to find the LCM of two numbers

 

#include <stdio.h>

int main() {
	int num1, num2, max;

	printf("Enter two positive integers: ");
	scanf("%d %d", &num1, &num2);

	// Find maximum of two numbers
	max = (num1 > num2) ? num1 : num2;

	// Loop until both conditions become false
	while(1) {
		if(max % num1 == 0 && max % num2 == 0) {
			printf("LCM of %d and %d is %d\n", num1, num2, max);
			break;
		}
		++max;
	}

	return 0;
} 

Inside the main() function:

- Integer variables `num1`, `num2`, and `max` are declared to store the input numbers and the maximum of the two numbers.

- The user is prompted to enter two positive integers.

- The maximum of the two numbers is determined using a ternary operator.

- A while loop iterates indefinitely.

- Within the loop, it checks if the current value of `max` is divisible by both `num1` and `num2`.

- If it is, it prints the least common multiple (LCM) and breaks out of the loop.

- If not, it increments `max` and continues the loop.




Featured Posts

SQL Interview Questions Topics

 SQL Topics to prepare for interviews,   SQL Basics: Introduction to SQL SQL Data Types DDL (Data Definition Language): C...

Popular Posts