In this tutorial you will learn about the C Program to find HCF and LCM and its application with practical example.
C Program to find HCF and LCM
In this tutorial, we will learn to create a C program that will find HCF and LCM using C programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- While loop in C programming.
- Arithmetic operations in C Programming.
Convert Number into Word
In c programming, it is possible to take numerical input from the user and find HCF and LCM with the help of a very small amount of code. The C language has many types of header libraries which has supported function in them with the help of these files the programming is easy.
With the help of this program, we can find HCF and LCM.
Here,
LCM:-Least Common Multiple.
HCF:- Highest Common Factor.
Algorithm:-
1 2 3 4 5 |
1. Declaring the variables for the program. 2. Taking the input number from the user. 3. Calculating the HCF 4. Calculating the LCM 5. Printing the result words. |
Program to find HCF and LCM:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#include <stdio.h> int main() { //declaring the local variables for program int a, b, x, y, t, hcf, lcm; //taking two numbers from user as a input printf("Enter two integers\n"); scanf("%d%d", &x, &y); //assigning the values a = x; b = y; //Finding HCF & LCM while (b != 0) { t = b; b = a % b; a = t; } hcf = a; lcm = (x*y)/hcf; //Printing output of the program printf("Highest common factor of %d and %d = %d\n", x, y, hcf); printf("Least common multiple of %d and %d = %d\n", x, y, lcm); return 0; } |
Output:-
The above program we have first initialize the required variable.
- x = it will hold the integer value1.
- y = it will hold the integer value2.
- a = it will hold the integer value of x.
- b = it will hold the integer value of y.
- t = it will hold the integer temporary value.
- hcf = it will hold the integer value of calculated hcf.
- lcm= it will hold the integer value of calculated lcm .
Input number from user.
Program Logic Code.
Printing output