ccplusplus.com
Learn C, C++ Concepts
Tuesday, September 13, 2011
c program to find the lcm of numbers
/****************************************************** * File : find-lcm-of-number.c * Author : Saurabh Gupta * Desc : program to find the lcm of 2 or more * numbers * Source : http://saurabhgupta0527.blogspot.com/ * Created : AM 09:42 12 September 2011 * Note : *****************************************************/ #include <stdio.h> #define SIZE 100 /* * function declaration */ int computeHCF(int,int); int computeLCM(int,int); int main() { printf ("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"); printf ("XXXXX Interactive LCM HCF Calculator XXXXX\n"); printf ("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"); int storeUserElements[SIZE], n, i, nUserChoice, nLCM, nHCF; printf("Enter No of Elements\n"); scanf("%d",&n); printf("Enter Elements\n"); for(i = 0; i < n; i++) { scanf("%d", &storeUserElements[i]); } do { printf("\n\nEnter Choice\n\n1.HCF\n2.LCM\n3.Exit\n"); scanf("%d",&nUserChoice); switch(nUserChoice) { case 1: nHCF = storeUserElements[0]; for(i = 1; i < n; i++) { nHCF = computeHCF(nHCF,storeUserElements[i]); } printf("\nHCF = %d",nHCF); break; case 2: nLCM=storeUserElements[0]; for(i = 1; i < n; i++) { nLCM = computeLCM(nLCM,storeUserElements[i]); } printf("\nLCM = %d",nLCM); break; case 3: break; default: printf("Wrong Choice"); break; } } while(nUserChoice != 3); printf ("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"); return 0; } /*************************************************************** * Function Name : computeHCF * Purpose : calculates HCF * Input : two numbers * Return Value : hcf * Return Type : integer ****************************************************************/ int computeHCF(int m,int n) { int nTemp, nRemainder; if(m < n){ nTemp=m; m=n; n=nTemp; } while(1) { nRemainder = m%n; if(nRemainder == 0) { return n; } else { m = n; n = nRemainder; } } } /*************************************************************** * Function Name : computeLCM * Purpose : calculates LCM * Input : two numbers * Return Value : lcm * Return Type : integer ****************************************************************/ int computeLCM(int m,int n) { int nLCM; nLCM = m*n/computeHCF(m,n); return nLCM; } /* * OUTPUT * [sgupta@rhel54x64 c]$ gcc find-lcm-of-number.c -o find-lcm-of-number [sgupta@rhel54x64 c]$ ./find-lcm-of-number XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXX Interactive LCM HCF Calculator XXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX Enter No of Elements 3 Enter Elements 2 3 4 Enter Choice 1.HCF 2.LCM 3.Exit 1 HCF = 1 Enter Choice 1.HCF 2.LCM 3.Exit 2 LCM = 12 Enter Choice 1.HCF 2.LCM 3.Exit 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX [sgupta@rhel54x64 c]$ */
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment