Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,153,322 members, 7,819,101 topics. Date: Monday, 06 May 2024 at 11:20 AM

[problem] Write A Program In C++ That Finds The Hcf Of 2 Numbers Without Using A Recursive Function - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / [problem] Write A Program In C++ That Finds The Hcf Of 2 Numbers Without Using A Recursive Function (5459 Views)

Matlab: A Program For Mathematicians? / A Program To Tell Ur Zodiac Sign / Without Using Pow Function In C (2) (3) (4)

(1) (Reply) (Go Down)

[problem] Write A Program In C++ That Finds The Hcf Of 2 Numbers Without Using A Recursive Function by lordZOUGA(m): 12:19pm On Nov 26, 2011
I can write the program with a recursive function. Heard it can be done without a recursive function buh am kinda stuck wit the program. So I need help, Any input is highly valued
Re: [problem] Write A Program In C++ That Finds The Hcf Of 2 Numbers Without Using A Recursive Function by ektbear: 3:36pm On Nov 26, 2011
Re: [problem] Write A Program In C++ That Finds The Hcf Of 2 Numbers Without Using A Recursive Function by lordZOUGA(m): 7:38pm On Nov 26, 2011
tried d euclidian algorithim, Wich is why I ended up with a recursive function, So I need a non recursive program, Heard dat shud be faster in execution cos recursive functions add to stack size and all,
Re: [problem] Write A Program In C++ That Finds The Hcf Of 2 Numbers Without Using A Recursive Function by Fayimora(m): 10:33am On Nov 27, 2011
Who said so? For me, the recursive algorithm even work faster(maybe a negligible time difference). You should never use iteration over recursion or vice versa because someone said it is faster. How about you give it a go? Try it out and learn to use the appropriate thing for the job. You can write almost anything both iteratively and recursively but you must have noticed that recursive solutions are always elegant.

With that been said, am not saying you should fill up an array recursively. Now that would be stupid. You must have read the article ekt_bear posted, if you havent then go read it and if you have then you should understand the algorithm enough to implement it in ANY language either iteratively or recursively.
Re: [problem] Write A Program In C++ That Finds The Hcf Of 2 Numbers Without Using A Recursive Function by Ghenghis(m): 8:00am On Nov 28, 2011
Fayimora:

Who said so? For me, the recursive algorithm even work faster(maybe a negligible time difference).

You can write almost anything both iteratively and recursively but you must have noticed that recursive solutions are always elegant

shocked embarassed
Re: [problem] Write A Program In C++ That Finds The Hcf Of 2 Numbers Without Using A Recursive Function by Kobojunkie: 3:34am On Nov 29, 2011
lordZOUGA:

tried d euclidian algorithim, Wich is why I ended up with a recursive function, So I need a non recursive program, Heard dat shud be faster in execution cos recursive functions add to stack size and all,

If you are trying to find an iterative approach to solving this, why not code a simple division method of find the gcd? I mean it is essentially the same way you would divide in a maths class.
Re: [problem] Write A Program In C++ That Finds The Hcf Of 2 Numbers Without Using A Recursive Function by Beaf: 6:42am On Nov 29, 2011
Euclidian algorithim:
int GetHCF(int firstNum, int secondNum)
{
     while (firstNum != 0 && secondNum != 0)
     {
         if (firstNum > secondNum)
         {
            firstNum %= secondNum;
         }
         else
         {
            secondNum %= firstNum;
         }
     }

     if (firstNum == 0)
     {
         return secondNum;
     }
     else
     {
         return firstNum;
     }
}
Re: [problem] Write A Program In C++ That Finds The Hcf Of 2 Numbers Without Using A Recursive Function by dabrake(m): 4:11pm On Nov 30, 2011
Euclidean algorithm. ::
#include<iostream>
using namespace std ;
int main()
{//hcf of 2 nos
int m, n, r ;
boy : cout << "enter 2 integer numbers" ;
cin >> m >> n ;

cout << "hcf of "<< m << " and "<<  n << "is = "  ;
if ( n > m )
{
int temp = n ;
n = m ;
m = r ;
}
if (m == 0 || n == 0)
{
cout << "wrong input! Zero not needed" << endl ;
goto boy ;
}
if ( m % n == 0)
{
cout << n ;
exit(0) ;
while ( n != 0 )
{
r = m % n ;
m = n ;
n = r ;
}
cout << m << endl ;
return 0 ;

}
Re: [problem] Write A Program In C++ That Finds The Hcf Of 2 Numbers Without Using A Recursive Function by lordZOUGA(m): 7:44pm On Dec 02, 2011
dabrake:

Euclidean algorithm. ::
#include<iostream>
using namespace std ;
int main()
{//hcf of 2 nos
int num1, num2, rem ;
cin >> num1 >> num2 ;
if ( n > m )
{
int temp = n ;
n = m ;
m = r ;
}

cout << "hcf of "<< m << " and "<< n << "is = " ;
while ( n != 0 )
{
r = m % n ;
m = n ;
n = r ;
}
cout << m << endl ;
return 0 ;

}
okay, your approach is quite similar to wat I did at 1st wen I tried it out. Figured I have to create a temp value to hold the value of one of the digits but after I ran it, nothing happened. I have 2 ask u, is there a particular reason u got a value 4 d variables 'num1' and 'num2' then proceeded to use 'n' and 'm' in ur statements?.
Re: [problem] Write A Program In C++ That Finds The Hcf Of 2 Numbers Without Using A Recursive Function by lordZOUGA(m): 7:49pm On Dec 02, 2011
Beaf:

Euclidian algorithim:
int GetHCF(int firstNum, int secondNum)
{
while (firstNum != 0 && secondNum != 0)
{
if (firstNum > secondNum)
{
firstNum %= secondNum;
}
else
{
secondNum %= firstNum;
}
}

if (firstNum == 0)
{
return secondNum;
}
else
{
return firstNum;
}
}

will check urs out
Re: [problem] Write A Program In C++ That Finds The Hcf Of 2 Numbers Without Using A Recursive Function by dabrake(m): 5:30pm On Dec 04, 2011
Lordzouga, thanks for the observation. num1 should be m while num2 should be n. Lemme modify it. Thanks again.
Re: [problem] Write A Program In C++ That Finds The Hcf Of 2 Numbers Without Using A Recursive Function by lordZOUGA(m): 9:42pm On Dec 06, 2011
dabrake:

Lordzouga, thanks for the observation. num1 should be m while num2 should be n. Lemme modify it. Thanks again.
besides if u really wanted to be swapping values, you shud ave jus created function to be called anytime you wanna swap.

(1) (Reply)

My C++ CGPA Calculator Program / How Much Does It Cost To Make An App? / Matlab: A Program For Mathematicians?

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 19
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.