₦airaland Forum

Welcome, Guest: RegisterLoginWith GoogleTrendingRecentNew

Stats: 3,325,064 members, 8,420,113 topics. Date: Thursday, 04 June 2026 at 11:37 AM

Toggle theme

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

Nairaland ForumScience/TechnologyProgramming[problem] Write A Program In C++ That Finds The Hcf Of 2 Numbers Without Using A Recursive Function (5643 Views)

1 Reply (Go Down)

[problem] Write A Program In C++ That Finds The Hcf Of 2 Numbers Without Using A Recursive Function by lordZOUGA(op): 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(op): 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
huh 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(op): 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(op): 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(op): 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

Matlab: A Program For Mathematicians?A Program To Tell Ur Zodiac SignWithout Using Pow Function In C234

Java Internship Full TimeUpwork Vs Fiverr Which One Is Better?Which Is The Best And Budget-friendly Laptop For Coding?