Study guides

☆

Q: How do you draw a flowchart of HCF of two given numbers?

Write your answer...

Submit

Still have questions?

Continue Learning about Engineering

#define SIZE 100 #include <stdio.h> int hcf_function(int,int); int lcm_function(int,int); int main() { int array[SIZE],n,i,choice,LCM,hcf; printf("Enter No of Elements\n"); scanf("%d",&n); printf("Enter Elements\n"); for(i=0;i<n;i++) scanf("%d",&array[i]); do { printf("\n\nEnter Choice\n\n1.HCF\n2.LCM\n3.Exit\n"); scanf("%d",&choice); switch(choice) { case 1: hcf=array[0]; for(i=1;i<n;i++) hcf=hcf_function(hcf,array[i]); printf("\nHCF = %d",hcf); break; case 2: LCM=array[0]; for(i=1;i<n;i++) LCM=lcm_function(LCM,array[i]); printf("\nLCM = %d",LCM); break; case 3: break; default:printf("Wrong Choice"); break; } }while(choice!=3); } /*************************************************************** Function Name : hcf_function Purpose : to find hcf Input : two numbers Return Value : hcf Return Type : int ****************************************************************/ int hcf_function(int m,int n) { int temp,reminder; if(m<n) { temp=m; m=n; n=temp; } while(1) { reminder=m%n; if(reminder==0) return n; else m=n; n=reminder; } } /*************************************************************** Function Name : lcm_function Purpose : to find LCM Input : two numbers Return Value : LCM Return Type : int ****************************************************************/ int lcm_function(int m,int n) { int LCM; LCM=m*n/hcf_function(m,n); return LCM; }

Algorithm: to find HCF of two nos a, b 1. Find larger of two and let it l, smaller = s 2. divide l by s and find qotient (q) & remainder(r) 3. if r is 0 then s is the hcf 4. put l=s, s=r and go to step 2 int a, b; int l,s,q,r; if (a>b) {l=a;s=b;} else {l=b;s=a;} do {q= l/s; r=l%s; if (r != 0) {l=s;s=r} } while (r != 0); hcf=s; /* this variable to be output or returned as a function value as per programmer's convenience */

#include#includevoid main(){int num1 , num2 , lcm , gcd , remainder , numerator , denominator ;clrscr();printf( "Enter two numbers\n");scanf(" %d%d " , &num1 , &num2 );if (num1 > num2){numerator=num1;denominator=num2;}else{numerator = num2 ;denominator = num1 ;}remainder = num1 % num2;while ( remainder != 0){numerator = denominator;denominator = remainder;remainder = numerator % denominator;}gcd = denominator;lcm = (num1 * num2 ) / gcd;printf("GCD of %d and %d =%d\n" , num1, num2, gcd);printf(" LCM of %d and %d= %d\n" , num1, num2, lcm);getch();}

Dunno about Linux, but I've written mine in C.It prime factorises the numbers, making a note of the highest power of each prime factor as it goes (in a linked list of malloc()ed structures). Once all the numbers have been factorised, it has a list of all the primes used along with their highest power. The lcm is then the product of the primes raised to their highest power.You are also not limited to the lcm of 2 numbers - you can keep factorising numbers until you run out of them and find the lcm of them all!Whilst you're at it you can add finding their hcf very easily: this time it's the product of the common primes to their lowest power.All that is then needed is the prime factorisation of the numbers.The normal method is:try the first prime (2)If it does not divide the number: set the prime to the next primeTry again from step 2Add one to the power count of this primereplace the number by the number divided by the primeif the number is not 1 go back to step 2Found all primes, stop!Finding the primes by which to divide is not easy on the fly, so you could check 2 specifically and then all odd numbers 3, 5, 7,..., but an improvement is to specifically check 2 and 3 and then check the numbers 6n ± 1 (which may be prime and why are 6n, 6n ± 2 and 6n ± 3 definitely not prime?) which skips every third odd number - this sequence of potential primes (5, 7, 11, 13, 17, 19, ...) can be easily generated.And while you're at it, you could display the prime factorisation you've done.And using that prime factorisation you can list the factors (and factor pairs) for the numbers.Obviously you'll need to sort out how the numbers are input to the program - I decode argv[], but you could use reading from stdin if you prefer.

for two positive integers: public static int gcd(int i1, int i2) { // using Euclid's algorithm int a=i1, b=i2, temp; while (b!=0) { temp=b; b=a%temp; a=temp; } return a; }

Related questions

First you will need to pick out the two numbers. Then you can use your textbook and the instructions in order to draw out the flow chart.

sfafa

The HCF of the given two numbers is 40

The HCF of the given two numbers is 14

The HCF of the given numbers is 1

The HCF of the given numbers is 14

The HCF of the given two numbers is 28

The HCF of the given two numbers is 8

The HCF of the given three numbers is 7

The HCF of the given three numbers is 15

The HCF of the given two numbers is 6

The HCF of the given numbers is 27

People also asked