In this tutorial you will learn about the C Program to Find HCF of n Numbers and its application with practical example.
C Program to Find HCF of n Numbers
In this tutorial, we will learn to create a C program that will Find HCF of n numbers 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.
- For loop in C programming.
- Arithmetic operations in C Programming.
Program to Find HCF of n numbers
In c programming, it is possible to take numerical input from the user and find HCF of n numbers 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 of n numbers
Here,
HCF:- Highest Common Factor.
Algorithm:-
1 2 3 4 5 6 7 8 9 |
1. Declaring the variables for the program. 2. Taking the input number from the user. 3. Passing to the loop. 4. Calculating the <strong>HCF</strong>. 5. Printing the result numbers. |
Program to Find HCF of n numbers:-
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 30 31 32 33 34 35 36 37 38 |
#include<stdio.h> void main() { //declaring the variables for the program int i,j,h,n,a[100]; int hcf(int,int); //taking input size of numbers printf("Enter how many numbers :"); scanf("%d",& n); for(i=0;i<n;i++) { //Taking the numbers in input printf("Enter a Number :"); scanf("%d",& a[i]); } //Calculating the by sending input to loop for(i=0;i<n-1;i++) { h=hcf(a[i],a[i+1]); a[i+1]=h; } printf("The HCF is : %d",h); return (0); } //function to calculate the hcf int hcf(int a,int b) { if(a%b==0) { return b; } else { return(hcf(b,a%b)); } } |
Output:-
In the above program, we have first initialized the required variable.
- i = it will hold the integer value.
- j = it will hold the integer value.
- h = it will hold the integer value.
- n = it will hold the integer value.
- a[100] = it will hold the integer value.
Input number from the user.
User-defined function HCF.
Input-output code in the main method body.