XII STD RECURSION WITHOUT SCANNER
the recursive function gives a stack overflow error. I want to calculate the GDC ie the greatest Integer function for two input numbers. the code of my program is as follows and the error comes in the function gcd_func in the last condition giving stack overflow null. .I do not wish to use the scanner class Is there anything i can do to improve thiscode?
import java.io.*;
public class reursion
{
public void main()throws IOException
{BufferedReader stdin= new BufferedReader (new InputStreamReader( System.in));
System.out.println("Input two nos");
int a =Integer.parseInt(stdin.readLine());
int b =Integer.parseInt(stdin.readLine());
int gcd=1;
int i=2;
int p= gcd_func(a,b,gcd,i);
System.out.println( "GCD+ +"+p);
}
int gcd_func(int a, int b, int gcd, int i)
{ if((a==1)||(b==1))
{ return(gcd);
}
else
if((a%i==0)&&(b%i==0))
{ gcd=gcd*i;
a=a/i;
b=b/i;
i++;
return( gcd_func(a,b,gcd,i));
}
else
{ i++;
return( gcd_func(a, b, gcd, i));
}
}
}
View Answers
July 28, 2011 at 10:15 AM
import java.util.*;
class FINDGCD{
public static int determineGCD(int a, int b) {
if(b==0)
return a;
else
return determineGCD(b, a % b);
}
public static void main(String[] args)throws Exception {
FINDGCD cal = new FINDGCD();
Scanner input=new Scanner(System.in);
System.out.println("Enter first number: ");
int num1=input.nextInt();
System.out.println("Enter second number: ");
int num2=input.nextInt();
int hcf = cal.determineGCD(num1, num2);
System.out.println("GCD of two numbers= "+hcf);
}
}
Ads
Related Tutorials/Questions & Answers:
XII STD RECURSION WITHOUT SCANNER
XII STD RECURSION WITHOUT SCANNER the recursive function gives a stack overflow error. I want to calculate the GDC ie the greatest Integer function... not wish to use the
scanner class Is there anything i can do to improve
Advertisements
Scanner
Scanner Hi, I am facing a problem in the following code as what should be the while condition I should use in order to get the loop keep running...= "Celsius";
do
{
Scanner temp_ip= new
Scanner(System.in
without ;
without ; can u give me an example of a program
without
without ;
without ; can u give me an example of a program
without
ModuleNotFoundError: No module named 'std'
ModuleNotFoundError: No module named '
std' Hi,
My Python program is throwing following error:
ModuleNotFoundError: No module named '
std'
How to remove the ModuleNotFoundError: No module named '
std' error
ModuleNotFoundError: No module named 'std'
ModuleNotFoundError: No module named '
std' Hi,
My Python program is throwing following error:
ModuleNotFoundError: No module named '
std'
How to remove the ModuleNotFoundError: No module named '
std' error
recursion program
recursion program Hi this is my first java class, and i have been trying for hours to do this program. it is a
recursion problem where the user will enter a continuous monthly investment, at a rate of 1% the program should say
ModuleNotFoundError: No module named 'std-domain'
ModuleNotFoundError: No module named '
std-domain' Hi,
My Python... '
std-domain'
How to remove the ModuleNotFoundError: No module named '
std... have to install padas library.
You can install
std-domain python
ModuleNotFoundError: No module named 'std-encode'
ModuleNotFoundError: No module named '
std-encode' Hi,
My Python... '
std-encode'
How to remove the ModuleNotFoundError: No module named '
std... have to install padas library.
You can install
std-encode python
ModuleNotFoundError: No module named 'recursion'
ModuleNotFoundError: No module named '
recursion' Hi,
My Python... '
recursion'
How to remove the ModuleNotFoundError: No module named '
recursion' error?
Thanks
Hi,
In your python environment you
Building a Binary Tree using std::map, std:set
Building a Binary Tree using
std::map,
std:set Hi, can someone please explain to me how I can make use of
std::map and/or
std::set to create a simple binary tree?
I do not seem to understand how these 2 containers can be used
Scanner class
Scanner class what have to do when an error occur about
Scanner class.i code
scanner sc=new
Scanner(System.in); but it shows an error regarding this.
Use
Scanner sc=new
Scanner(System.in
javascript recursion example
javascript
recursion example javascript
recursion example
<html>
<script>
function factorial (n)
{
if(n==0) return(1);
return (n * factorial (n-1) );
}
document.write(factorial(5));
<