C PROGRAM TO FIND THE HCF OF GIVEN SET OF NUMBERS USING FUNCTION
/** program for finding the H.C.F (HIGHEST COMMON FACTOR) of given set of 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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
#include<stdio.h> #include<conio.h> #include<string.h> #define MAX 50 // defining the maximum size of an array int HCF(int, int); // HCF function prototype main() { int arr[MAX]; int num1,num2,size,result; int i,j; printf("\n>>> PROGRAM TO FIND THE H.C.F OF GIVEN NUMBERS USING FUNCTIONS <<<\n\n"); printf("\nEnter the size of an array : "); scanf("%d",&size); // reading size of an array from user printf("\nEnter the elements : n"); for(i=0;i<size;i++) //reading elements of array from user { printf("\n Element[%d]= ",i); scanf("%d", &arr[i]); } num1=arr[0]; // initiatilizing the variable for(j=0;j<size-1;j++) { num2=arr[j+1]; result=HCF(num1,num2); // calling function num1=result; } // printing the result printf("\n\n H.C.F Of The Given Numbers is : %d",result); getch(); return 0; } int HCF(int x, int y) // function body starts here { int result1; while((x%y)!=0) { result1=x%y; x=y; y=result1; } return y; // returns value } |
Output: ( using GNU GCC Compiler with code::blocks IDE)