WAP in C to store any ten numbers in an array to print HCF,LCM

The Program in c to store any ten numbers in an array and print the LCM and HCF of all the numbers is given below:

``````#include <stdio.h>

int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}

int lcm(int a, int b) {
return (a * b) / gcd(a, b);
}

int main() {
int arr[10], i;
int n = sizeof(arr) / sizeof(arr[0]);
int hcf, lcm_value;

printf("Enter 10 numbers: \n");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

hcf = arr[0];
for (i = 1; i < n; i++) {
hcf = gcd(arr[i], hcf);
}

lcm_value = arr[0];
for (i = 1; i < n; i++) {
lcm_value = lcm(arr[i], lcm_value);
}

printf("HCF of all the numbers: %d\n", hcf);
printf("LCM of all the numbers: %d\n", lcm_value);

return 0;
}
``````

Output:

Enter 10 numbers:
10
23
45
13
45
77
34
13
99
56
HCF of all the numbers: 1
LCM of all the numbers: 32633272

Pro-Tips💡

This program first prompts the user to enter ten numbers and stores them in an array.

Then it uses a pair of nested for loop to find the HCF and LCM of all the number in the array.

It uses the gcd function to find the HCF of all the numbers in the array and lcm function to find the LCM of all the numbers.

The hcf variable stores the HCF of all the numbers and lcm_value variable stores the LCM of all the numbers

and finally it prints the value of the hcf and lcm_value variable.

Learn C-Sharp ↗

C-sharp covers every topic to learn about C-Sharp thoroughly.

Learn C Programming ↗

C-Programming covers every topic to learn about C-Sharp thoroughly.

Learn C++ Programming↗

C++ covers every topic to learn about C-Sharp thoroughly.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

C# Program to Delete Element at Desired position From Array

The Program in C# Program to Delete Element at Desired position From Array is given below: Output: Hello Codeauri Family, enter the number of elements in the array:4Enter…

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X