Blog Moved

This website completely moved to new domain. For latest content, visit www.programmingposts.com

Search This Blog

28 Sept 2012

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**/
#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)



1 comment:

  1. nice article .thank you for sharing useful info.
    web programming tutorial
    welookups

    ReplyDelete