Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners XII STD RECURSION WITHOUT SCANNER

Have Programming Question? Ask it here!
 
 
 


Ahsial Rick
XII STD RECURSION WITHOUT SCANNER
1 Answer(s)      3 years ago
Posted in : Java Beginners


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));
    }

}

}

Advertisement
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);
        }
}


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
without scanner
without scanner  a company need a system to store the data about the employees , the number of employees is dynamic , you need to store the following... and class and didn't use the scanner .   Hi Friend, Whether
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
can we use scanner class,class , object and methods to get output without using constructor ????
can we use scanner class,class , object and methods to get output without using constructor ????  im getting error here..i hav used scanner class... am i getting error here... can we get output using scanner class , object
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
Scanner class
Scanner class  import java.util.*; class Dd { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int... Scanner class was introduced in java 5
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
scanner program
scanner program  a news paper boy purchase 100 papers every day.he purchases papers at Rs.1.50 and sells at Rs.2.00 at each.any unsold news papers... his profit..using scanner
without ;
without ;  can u give me an example of a program without
without ;
without ;  can u give me an example of a program without
recursion numbers - Java Beginners
recursion numbers  I need to use recursion to test all values from 0 to 20 and see if they are contain in a 1-D array with values: 2,4,6,8,10,12,14,16,18,20. The results of all numbers from 0-20 will be printed
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)); <
recursion method - Ajax
recursion method  Can i ask for a program that input a lines in the diamond shape recursion method   Hi friend, Code for solving the problem : public class RecursiveDiamond { public RecursiveDiamond
scanner - Java Beginners
scanner   how to ask user to enter index in arry using scanner
Recursion - Java Beginners
Recursion  HI!Can i ask for another recursion diamond,using recursive method and asking for how many lines should be inputed to create the diamond..., This is simple code of recursion code. public class RecursiveDemo { public static
Recursion - Java Beginners
Recursion  Create a method called, rangeMult, that uses recursion to multiply a range of array elements. The method takes the following arguments: an int array that contains the range of elements to be multiplied, an int
scanner problem - Java Beginners
scanner problem  the program that enters sides of the triangle using scanner and outputs the area of triangle  Hi Friend, We... main(String []args){ Scanner scanner = new Scanner(System.in
Hp scanner - Java Beginners
Hp scanner  Hi guys, i would like to access hp scanner using java programme. could you refer me some useful information to proceed
scanner Class Error - Java Beginners
scanner Class Error  Hello Sir ,When i run the program of Scanner Class ,there is Error Can not Resolve Symbol-Scanner how i can solve..., Scanner class is not provided. Check your version.It should work with java 1.5
Java file scanner
Java file scanner In this section, you will learn how to read a file using Scanner class. Description of code: J2SE5.0 provides some additional classes.... It also parses the primitive data. Scanner class gives a great deal of power
java program to check whether a number is pallindrome or not using recursion???????
java program to check whether a number is pallindrome or not using recursion???????  java program to check whether a number is pallindrome or not using recursion
java program to check whether a number is pallindrome or not using recursion???????
java program to check whether a number is pallindrome or not using recursion???????  java program to check whether a number is pallindrome or not using recursion
java program to check whether a number is pallindrome or not using recursion???????
java program to check whether a number is pallindrome or not using recursion???????  java program to check whether a number is pallindrome or not using recursion
java program to check whether a number is pallindrome or not using recursion???????
java program to check whether a number is pallindrome or not using recursion???????  java program to check whether a number is pallindrome or not using recursion
java program to check whether a number is pallindrome or not using recursion???????
java program to check whether a number is pallindrome or not using recursion???????  java program to check whether a number is pallindrome or not using recursion
java program to check whether a number is pallindrome or not using recursion???????
java program to check whether a number is pallindrome or not using recursion???????  java program to check whether a number is pallindrome or not using recursion
java program to check whether a number is pallindrome or not using recursion???????
java program to check whether a number is pallindrome or not using recursion???????  java program to check whether a number is pallindrome or not using recursion
Calculate factorial Using Recursion
Calculate factorial Using Recursion  ... by using recursion in jsp. To make a program on factorial, firstly it must be clear what is recursion. In a simple language we can define recursion
barcode scanner device usage in database application
barcode scanner device usage in database application  Am trying to design a supermarket inventory desktop database application however I can't seem to know how to use a barcode scanner device on my application can anyone put me
Multiply a range of array elements using Recursion
Multiply a range of array elements using Recursion In this section you will learn how to use recursion to multiply a range of array elements. For this we have created a method rangeMult() that takes three arguments: an int array
Pattern,Matcher,Formatter and Scanner classes
Pattern,Matcher,Formatter and Scanner classes This section Describes : 1... using the Formatter and Scanner classes and the PrintWriter.format/printf..., n) Formatted input The scanner API provides basic input
with out using scanner mul two matrices
with out using scanner mul two matrices  write a java program to multiply two matrices and print there result. note: don't use the scanner and the values is passed at run time....   import java.io.*; class
Iteration without loop
Iteration without loop  Hi, Is there any way I can have a line of code iterate a number of times set by the user without having a while loop or any...[] args) { Scanner input=new Scanner(System.in
input using scanner and add/sub/mul/div of a matrix
input using scanner and add/sub/mul/div of a matrix  hiiii...... performe matrix add/sub/mul/div using scanner
Action without a form.
Action without a form.  Can I have an Action without a form
Creating a File I/O using import scanner, io.* and text.*
Creating a File I/O using import scanner, io.* and text.*  open a file for reading and open another file for writing data. input file hours-int, hourly rate-double, name-string. output file name-string, hours-int, hourly rate
Application without Interface Builder!!
Application without Interface Builder!!  Can we make our iphone application without Interface Builder???   hello, yes , it is possible to make an iphone application without interface builder.. we can also use only
Double To Int Without TypeCasting
Double To Int Without TypeCasting  How to convert Double to Int without using typecasting?   class ConvertDoubleToInt { public static void main(String[] args) { double d=25.5; int num=new
DataGrid without DataBase
DataGrid without DataBase  I want display list of data without using database. Ex- Employee have some elements like- name, sex, place, designation. I have to display the content of these elements but without having
without using built-in functions
without using built-in functions  Hai sir... Please help me to solve this... Findout occurances of each string in every another string (built - in functions are not at all allowed) all, and, sand, falling wall and sand
Sending email without authentication
Sending email without authentication  Hi sir, Am doing a project in JSP, in that i want to send mail without any authentication of password so send me code as soon as possible.   Please visit the following links
Breaking a string into words without using StringTokenizer
Breaking a string into words without using StringTokenizer  how can we Break a string into words without using StringTokenizer ??   ... { public static void main(String[] args) { Scanner input=new
html table without border
html table without border  How to create a html table without border?   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN..." content="text/html; charset=utf-8" /> <title>Table without borders<
using tiles without struts
using tiles without struts  Hi I am trying to make an application using tiles 2.0. Description of my web.xml is as follows: tiles org.apache.tiles.web.startup.TilesServlet
how to reverse a string without changing its place
how to reverse a string without changing its place  how to reverse a string without changing its place   Hi Friend, Another way... void main(String[]args){ Scanner input=new Scanner(System.in
 

 

 

DMCA.com