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.