Java

Moderators: zibadian
Number of threads: 7836
Number of posts: 18235

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Java Program - Greatest Common Divisor Posted by kentylad on 1 May 2004 at 12:48 PM
I have been asked to create a java program which will calculate the Greatest Common Divisor of two integer arguments. I have so far got this:

import java.lang.*;
import java.math.*;

public class Gcd {

public static int gcdCalc ( int int1, int int2 ) {

		if( int1 == int2) {
			gcd = int1;
		}//if

		else {
			while ( int1 != int2) {

				if( int1 > int2 ) {
					int1 = int1 - int2;
				}//nested-if

				else {
					int2 = int2 - int1;
				}//nested-else
			}//while
		}//else

		return( gcd );

	}//gcdCalc


	public static void main( String args[] ) {

		int arg1 = Integer.parseInt( args[0] );
		int arg2 = Integer.parseInt( args[1] );

		int endGcd = gcdCalc( arg1, arg2 );

		System.out.println( "The GCD for Integers " + arg1 + " and " + arg2 + " is " + endGcd );
	}//main

}//Gcd class


The program as it stands will not compile and the errors I cannot seem to fix

Errors:

cannot resolve symbol
symbol : variable gcd
location: class Gcd
gcd = int1;
^
cannot resolve symbol
symbol : variable gcd
location: class Gcd
return( gcd );
^
[/blue]

Any suggestions?
Report
Re: Java Program - Greatest Common Divisor Posted by Manning on 1 May 2004 at 7:26 PM
: I have been asked to create a java program which will calculate the Greatest Common Divisor of two integer arguments. I have so far got this:
:
:
: import java.lang.*;
: import java.math.*;
: 
: public class Gcd {
: 
: public static int gcdCalc ( int int1, int int2 ) {
: 
: 		if( int1 == int2) {
: 			gcd = int1;
: 		}//if
: 
: 		else {
: 			while ( int1 != int2) {
: 
: 				if( int1 > int2 ) {
: 					int1 = int1 - int2;
: 				}//nested-if
: 
: 				else {
: 					int2 = int2 - int1;
: 				}//nested-else
: 			}//while
: 		}//else
: 
: 		return( gcd );
: 
: 	}//gcdCalc
: 
: 
: 	public static void main( String args[] ) {
: 
: 		int arg1 = Integer.parseInt( args[0] );
: 		int arg2 = Integer.parseInt( args[1] );
: 
: 		int endGcd = gcdCalc( arg1, arg2 );
: 
: 		System.out.println( "The GCD for Integers " + arg1 + " and " + arg2 + " is " + endGcd );
: 	}//main
: 
: }//Gcd class
: 

:
: The program as it stands will not compile and the errors I cannot seem to fix
:
: Errors:
:
: cannot resolve symbol
: symbol : variable gcd
: location: class Gcd
: gcd = int1;
: ^
: cannot resolve symbol
: symbol : variable gcd
: location: class Gcd
: return( gcd );
: ^
: [/blue]
:
: Any suggestions?

The error should be pretty clear. It says the error is cannot resolve symbol, and it says the symbol is variable gcd, so it's saying cannot resolve variable gcd. IOW, you need to declare the variable gcd before you try to use it.
Report
Re: Java Program - Greatest Common Divisor Posted by kentylad on 2 May 2004 at 3:29 AM
This message was edited by kentylad at 2004-5-2 3:29:59

: : I have been asked to create a java program which will calculate the Greatest Common Divisor of two integer arguments. I have so far got this:
: :
: :
: : import java.lang.*;
: : import java.math.*;
: : 
: : public class Gcd {
: : 
: : public static int gcdCalc ( int int1, int int2 ) {
: : 
: : 		if( int1 == int2) {
: : 			int gcd = int1;
: : 		}//if
: : 
: : 		else {
: : 			while ( int1 != int2) {
: : 
: : 				if( int1 > int2 ) {
: : 					int1 = int1 - int2;
: : 				}//nested-if
: : 
: : 				else {
: : 					int2 = int2 - int1;
: : 				}//nested-else
: : 			}//while
: : 		}//else
: : 
: : 		return( gcd );
: : 
: : 	}//gcdCalc
: : 
: : 
: : 	public static void main( String args[] ) {
: : 
: : 		int arg1 = Integer.parseInt( args[0] );
: : 		int arg2 = Integer.parseInt( args[1] );
: : 
: : 		int endGcd = gcdCalc( arg1, arg2 );
: : 
: : 		System.out.println( "The GCD for Integers " + arg1 + " and " + arg2 + " is " + endGcd );
: : 	}//main
: : 
: : }//Gcd class
: : 

: :
: : The program as it stands will not compile and the errors I cannot seem to fix
: :
: : Errors:
: :
: : cannot resolve symbol
: : symbol : variable gcd
: : location: class Gcd
: : gcd = int1;
: : ^
: : cannot resolve symbol
: : symbol : variable gcd
: : location: class Gcd
: : return( gcd );
: : ^
: : [/blue]
: :
: : Any suggestions?
:
: The error should be pretty clear. It says the error is cannot resolve symbol, and it says the symbol is variable gcd, so it's saying cannot resolve variable gcd. IOW, you need to declare the variable gcd before you try to use it.

OK so I have declared the variable gcd's type (int), however the

cannot resolve symbol
symbol : variable gcd
location: class Gcd
return( gcd );
^

error still persists




Report
Re: Java Program - Greatest Common Divisor Posted by Manning on 2 May 2004 at 6:11 AM
: This message was edited by kentylad at 2004-5-2 3:29:59

: : : I have been asked to create a java program which will calculate the Greatest Common Divisor of two integer arguments. I have so far got this:
: : :
: : :
: : : import java.lang.*;
: : : import java.math.*;
: : : 
: : : public class Gcd {
: : : 
: : : public static int gcdCalc ( int int1, int int2 ) {
: : : 
: : : 		if( int1 == int2) {
: : : 			int gcd = int1;
: : : 		}//if
: : : 
: : : 		else {
: : : 			while ( int1 != int2) {
: : : 
: : : 				if( int1 > int2 ) {
: : : 					int1 = int1 - int2;
: : : 				}//nested-if
: : : 
: : : 				else {
: : : 					int2 = int2 - int1;
: : : 				}//nested-else
: : : 			}//while
: : : 		}//else
: : : 
: : : 		return( gcd );
: : : 
: : : 	}//gcdCalc
: : : 
: : : 
: : : 	public static void main( String args[] ) {
: : : 
: : : 		int arg1 = Integer.parseInt( args[0] );
: : : 		int arg2 = Integer.parseInt( args[1] );
: : : 
: : : 		int endGcd = gcdCalc( arg1, arg2 );
: : : 
: : : 		System.out.println( "The GCD for Integers " + arg1 + " and " + arg2 + " is " + endGcd );
: : : 	}//main
: : : 
: : : }//Gcd class
: : : 

: : :
: : : The program as it stands will not compile and the errors I cannot seem to fix
: : :
: : : Errors:
: : :
: : : cannot resolve symbol
: : : symbol : variable gcd
: : : location: class Gcd
: : : gcd = int1;
: : : ^
: : : cannot resolve symbol
: : : symbol : variable gcd
: : : location: class Gcd
: : : return( gcd );
: : : ^
: : : [/blue]
: : :
: : : Any suggestions?
: :
: : The error should be pretty clear. It says the error is cannot resolve symbol, and it says the symbol is variable gcd, so it's saying cannot resolve variable gcd. IOW, you need to declare the variable gcd before you try to use it.
:
: OK so I have declared the variable gcd's type (int), however the
:
: cannot resolve symbol
: symbol : variable gcd
: location: class Gcd
: return( gcd );
: ^

: error still persists

Has to do with the "scope" of the variable. Since you declared it inside a set of { } brackets, it is only visible from within that block of code. You need to declare it at the very top of your method, and then it'll be available in all parts of it.
Report
Re: Java Program - Greatest Common Divisor Posted by kentylad on 2 May 2004 at 6:20 AM
This message was edited by kentylad at 2004-5-2 6:20:33

: : This message was edited by kentylad at 2004-5-2 3:29:59

: : : : I have been asked to create a java program which will calculate the Greatest Common Divisor of two integer arguments. I have so far got this:
: : : :
: : : :
: : : : import java.lang.*;
: : : : import java.math.*;
: : : : 
: : : : public class Gcd {

: : : : gcd = 0;

: : : : public static int gcdCalc ( int int1, int int2 ) {
: : : : 
: : : : 		if( int1 == int2) {
: : : : 		    gcd = int1;
: : : : 		}//if
: : : : 
: : : : 		else {
: : : : 			while ( int1 != int2) {
: : : : 
: : : : 				if( int1 > int2 ) {
: : : : 					int1 = int1 - int2;
                                                gcd = int1;
: : : : 				}//nested-if
: : : : 
: : : : 				else {
: : : : 					int2 = int2 - int1;
                                                gcd = int2;
: : : : 				}//nested-else
: : : : 			}//while
: : : : 		}//else
: : : : 
: : : : 		return( gcd );
: : : : 
: : : : 	}//gcdCalc
: : : : 
: : : : 
: : : : 	public static void main( String args[] ) {
: : : : 
: : : : 		int arg1 = Integer.parseInt( args[0] );
: : : : 		int arg2 = Integer.parseInt( args[1] );
: : : : 
: : : : 		int endGcd = gcdCalc( arg1, arg2 );
: : : : 
: : : : 		System.out.println( "The GCD for Integers " + arg1 + " and " + arg2 + " is " + endGcd );
: : : : 	}//main
: : : : 
: : : : }//Gcd class
: : : : 

: : : :
: : : : The program as it stands will not compile and the errors I cannot seem to fix
: : : :
: : : : Errors:
: : : :
: : : : cannot resolve symbol
: : : : symbol : variable gcd
: : : : location: class Gcd
: : : : gcd = int1;
: : : : ^
: : : : cannot resolve symbol
: : : : symbol : variable gcd
: : : : location: class Gcd
: : : : return( gcd );
: : : : ^
: : : : [/blue]
: : : :
: : : : Any suggestions?
: : :
: : : The error should be pretty clear. It says the error is cannot resolve symbol, and it says the symbol is variable gcd, so it's saying cannot resolve variable gcd. IOW, you need to declare the variable gcd before you try to use it.
: :
: : OK so I have declared the variable gcd's type (int), however the
: :
: : cannot resolve symbol
: : symbol : variable gcd
: : location: class Gcd
: : return( gcd );
: : ^

: : error still persists
:
: Has to do with the "scope" of the variable. Since you declared it inside a set of { } brackets, it is only visible from within that block of code. You need to declare it at the very top of your method, and then it'll be available in all parts of it.
:

OK changes have been made and the program will now print out the common divisor. Error checks to go



Report
Re: Java Program - Greatest Common Divisor Posted by kentylad on 2 May 2004 at 6:29 AM
: This message was edited by kentylad at 2004-5-2 6:20:33

: : : This message was edited by kentylad at 2004-5-2 3:29:59

: : : : : I have been asked to create a java program which will calculate the Greatest Common Divisor of two integer arguments. I have so far got this:
: : : : :
: : : : :
: : : : : import java.lang.*;
: : : : : import java.math.*;
: : : : : 
: : : : : public class Gcd {
: 
: : : : : gcd = 0;
: 
: : : : : public static int gcdCalc ( int int1, int int2 ) {
: : : : : 
: : : : : 		if( int1 == int2) {
: : : : : 		    gcd = int1;
: : : : : 		}//if
: : : : : 
: : : : : 		else {
: : : : : 			while ( int1 != int2) {
: : : : : 
: : : : : 				if( int1 > int2 ) {
: : : : : 					int1 = int1 - int2;
:                                                 gcd = int1;
: : : : : 				}//nested-if
: : : : : 
: : : : : 				else {
: : : : : 					int2 = int2 - int1;
:                                                 gcd = int2;
: : : : : 				}//nested-else
: : : : : 			}//while
: : : : : 		}//else
: : : : : 
: : : : : 		return( gcd );
: : : : : 
: : : : : 	}//gcdCalc
: : : : : 
: : : : : 
: : : : : 	public static void main( String args[] ) {
: : : : : 
: : : : : 		int arg1 = Integer.parseInt( args[0] );
: : : : : 		int arg2 = Integer.parseInt( args[1] );
: : : : : 
: : : : : 		int endGcd = gcdCalc( arg1, arg2 );
: : : : : 
: : : : : 		System.out.println( "The GCD for Integers " + arg1 + " and " + arg2 + " is " + endGcd );
: : : : : 	}//main
: : : : : 
: : : : : }//Gcd class
: : : : : 

: : : : :
: : : : : The program as it stands will not compile and the errors I cannot seem to fix
: : : : :
: : : : : Errors:
: : : : :
: : : : : cannot resolve symbol
: : : : : symbol : variable gcd
: : : : : location: class Gcd
: : : : : gcd = int1;
: : : : : ^
: : : : : cannot resolve symbol
: : : : : symbol : variable gcd
: : : : : location: class Gcd
: : : : : return( gcd );
: : : : : ^
: : : : : [/blue]
: : : : :
: : : : : Any suggestions?
: : : :
: : : : The error should be pretty clear. It says the error is cannot resolve symbol, and it says the symbol is variable gcd, so it's saying cannot resolve variable gcd. IOW, you need to declare the variable gcd before you try to use it.
: : :
: : : OK so I have declared the variable gcd's type (int), however the
: : :
: : : cannot resolve symbol
: : : symbol : variable gcd
: : : location: class Gcd
: : : return( gcd );
: : : ^

: : : error still persists
: :
: : Has to do with the "scope" of the variable. Since you declared it inside a set of { } brackets, it is only visible from within that block of code. You need to declare it at the very top of your method, and then it'll be available in all parts of it.
: :
:
: OK changes have been made and the program will now print out the common divisor. Error checks to go
import java.lang.*;
import java.math.*;

public class Gcd {

	public static int gcdCalc ( int int1, int int2 ) {

	int gcd = 0;

		if( int1 == int2) {
			gcd = int1;
		}//if

		else {
			while ( int1 != int2) {

				if( int1 > int2 ) {
					int1 = int1 - int2;
					gcd = int1;
				}//nested-if

				else {
					int2 = int2 - int1;
					gcd = int2;
				}//nested-else
			}//while
		}//else

		return( gcd );

	}//gcdCalc


	public static void main( String args[] ) {

		int arg1 = Integer.parseInt( args[0] );
		int arg2 = Integer.parseInt( args[1] );
		int endGcd = gcdCalc( arg1, arg2 );

		if( args.length != 2) {
			System.out.println( "Error: Incorrect Number of Arguments" );
			System.exit( 1 );
		}//if

		if ( arg1 < 0 || arg2 < 0 ) {
			System.out.println( "Error: Negative Argument Entered" );
			System.exit( 1 );
		}//if

		System.out.println( "The GCD for Integers " + arg1 + " and " + arg2 + " is " + endGcd );

	}//main

}//Gcd class


As you can see I have two error check; one to test the correct number of arguments has been entered (this works fine), and on to test for a negative integer. The negative integer checl does not work for some reason although it does compile. It seems to loop infinitly. I cant exlicitly see anything wrong with the code however
Report
Re: Java Program - Greatest Common Divisor Posted by kentylad on 3 May 2004 at 10:40 AM
: : This message was edited by kentylad at 2004-5-2 6:20:33

: : : : This message was edited by kentylad at 2004-5-2 3:29:59

: : : : : : I have been asked to create a java program which will calculate the Greatest Common Divisor of two integer arguments. I have so far got this:
: : : : : :
: : : : : :
: : : : : : import java.lang.*;
: : : : : : import java.math.*;
: : : : : : 
: : : : : : public class Gcd {
: : 
: : : : : : gcd = 0;
: : 
: : : : : : public static int gcdCalc ( int int1, int int2 ) {
: : : : : : 
: : : : : : 		if( int1 == int2) {
: : : : : : 		    gcd = int1;
: : : : : : 		}//if
: : : : : : 
: : : : : : 		else {
: : : : : : 			while ( int1 != int2) {
: : : : : : 
: : : : : : 				if( int1 > int2 ) {
: : : : : : 					int1 = int1 - int2;
: :                                                 gcd = int1;
: : : : : : 				}//nested-if
: : : : : : 
: : : : : : 				else {
: : : : : : 					int2 = int2 - int1;
: :                                                 gcd = int2;
: : : : : : 				}//nested-else
: : : : : : 			}//while
: : : : : : 		}//else
: : : : : : 
: : : : : : 		return( gcd );
: : : : : : 
: : : : : : 	}//gcdCalc
: : : : : : 
: : : : : : 
: : : : : : 	public static void main( String args[] ) {
: : : : : : 
: : : : : : 		int arg1 = Integer.parseInt( args[0] );
: : : : : : 		int arg2 = Integer.parseInt( args[1] );
: : : : : : 
: : : : : : 		int endGcd = gcdCalc( arg1, arg2 );
: : : : : : 
: : : : : : 		System.out.println( "The GCD for Integers " + arg1 + " and " + arg2 + " is " + endGcd );
: : : : : : 	}//main
: : : : : : 
: : : : : : }//Gcd class
: : : : : : 

: : : : : :
: : : : : : The program as it stands will not compile and the errors I cannot seem to fix
: : : : : :
: : : : : : Errors:
: : : : : :
: : : : : : cannot resolve symbol
: : : : : : symbol : variable gcd
: : : : : : location: class Gcd
: : : : : : gcd = int1;
: : : : : : ^
: : : : : : cannot resolve symbol
: : : : : : symbol : variable gcd
: : : : : : location: class Gcd
: : : : : : return( gcd );
: : : : : : ^
: : : : : : [/blue]
: : : : : :
: : : : : : Any suggestions?
: : : : :
: : : : : The error should be pretty clear. It says the error is cannot resolve symbol, and it says the symbol is variable gcd, so it's saying cannot resolve variable gcd. IOW, you need to declare the variable gcd before you try to use it.
: : : :
: : : : OK so I have declared the variable gcd's type (int), however the
: : : :
: : : : cannot resolve symbol
: : : : symbol : variable gcd
: : : : location: class Gcd
: : : : return( gcd );
: : : : ^

: : : : error still persists
: : :
: : : Has to do with the "scope" of the variable. Since you declared it inside a set of { } brackets, it is only visible from within that block of code. You need to declare it at the very top of your method, and then it'll be available in all parts of it.
: : :
: :
: : OK changes have been made and the program will now print out the common divisor. Error checks to go
:
: import java.lang.*;
: import java.math.*;
: 
: public class Gcd {
: 
: 	public static int gcdCalc ( int int1, int int2 ) {
: 
: 	int gcd = 0;
: 
: 		if( int1 == int2) {
: 			gcd = int1;
: 		}//if
: 
: 		else {
: 			while ( int1 != int2) {
: 
: 				if( int1 > int2 ) {
: 					int1 = int1 - int2;
: 					gcd = int1;
: 				}//nested-if
: 
: 				else {
: 					int2 = int2 - int1;
: 					gcd = int2;
: 				}//nested-else
: 			}//while
: 		}//else
: 
: 		return( gcd );
: 
: 	}//gcdCalc
: 
: 
: 	public static void main( String args[] ) {
: 
: 		int arg1 = Integer.parseInt( args[0] );
: 		int arg2 = Integer.parseInt( args[1] );
: 		int endGcd = gcdCalc( arg1, arg2 );
: 
: 		if( args.length != 2) {
: 			System.out.println( "Error: Incorrect Number of Arguments" );
: 			System.exit( 1 );
: 		}//if
: 
: 		if ( arg1 < 0 || arg2 < 0 ) {
: 			System.out.println( "Error: Negative Argument Entered" );
: 			System.exit( 1 );
: 		}//if
: 
: 		System.out.println( "The GCD for Integers " + arg1 + " and " + arg2 + " is " + endGcd );
: 
: 	}//main
: 
: }//Gcd class
: 

:
: As you can see I have two error check; one to test the correct number of arguments has been entered (this works fine), and on to test for a negative integer. The negative integer checl does not work for some reason although it does compile. It seems to loop infinitly. I cant exlicitly see anything wrong with the code however
:
The single error for checking for negative integers is still not working, and proving to be the trailing factor in my program. Is the condition correct i.e. if( arg1 < 0 || ar2 < 0 )?

Regards

Report
Re: Java Program - Greatest Common Divisor Posted by Manning on 3 May 2004 at 10:55 AM
: The single error for checking for negative integers is still not working, and proving to be the trailing factor in my program. Is the condition correct i.e. if( arg1 < 0 || ar2 < 0 )?


That looks fine to me. IIRC "boolean or" has a lower precedence than the "less than" and "greater than" operators, but just in case you could add brackets to make sure the order is what you want. So:

if ((arg1 < 0) || (arg2 < 0)) {
  // stuff here
}

Report
Re: Java Program - Greatest Common Divisor Posted by kentylad on 3 May 2004 at 1:47 PM
Unfortunatly the brakects dont seem to have worked. I'm almost 100% certain I am entering the correct arguments. For example, I pass in 10 & 100 as two arguments, and expect the GCD to be 10; which I get. However if I pass in 100 and -10; the program seems to go one infinitly.

As I said before; all other error checks work - it's just this blighter!

Report
Re: Java Program - Greatest Common Divisor Posted by Manning on 3 May 2004 at 4:23 PM
This message was edited by Manning at 2004-5-3 16:23:51

: Unfortunatly the brakects dont seem to have worked. I'm almost 100% certain I am entering the correct arguments. For example, I pass in 10 & 100 as two arguments, and expect the GCD to be 10; which I get. However if I pass in 100 and -10; the program seems to go one infinitly.
:
: As I said before; all other error checks work - it's just this blighter!


Ahh I just took a closer look at the code. You have your error checking AFTER you call your gcdCalc() function. It needs to go before of course, otherwise it's kind of useless.
Report
Re: Java Program - Greatest Common Divisor Posted by kentylad on 4 May 2004 at 12:11 AM
Good man Manning problem sorted
Report
Re: Java Program - Greatest Common Divisor Posted by rajesh_88 on 8 May 2004 at 2:36 AM
: I have been asked to create a java program which will calculate the Greatest Common Divisor of two integer arguments. I have so far got this:
:
:
: import java.lang.*;
: import java.math.*;
: 
: public class Gcd {
: 
: public static int gcdCalc ( int int1, int int2 ) {

:               int gcd;
: 		if( int1 == int2) {
: 			gcd = int1;
: 		}//if
: 
: 		else {
: 			while ( int1 != int2) {
: 
: 				if( int1 > int2 ) {
: 					int1 = int1 - int2;
: 				}//nested-if
: 
: 				else {
: 					int2 = int2 - int1;
: 				}//nested-else
: 			}//while
: 		}//else
: 
: 		return( gcd );
: 
: 	}//gcdCalc
: 
: 
: 	public static void main( String args[] ) {
: 
: 		int arg1 = Integer.parseInt( args[0] );
: 		int arg2 = Integer.parseInt( args[1] );
: 
: 		int endGcd = gcdCalc( arg1, arg2 );
: 
: 		System.out.println( "The GCD for Integers " + arg1 + " and " + arg2 + " is " + endGcd );
: 	}//main
: 
: }//Gcd class
: 

:
: The program as it stands will not compile and the errors I cannot seem to fix
:
: Errors:
:
: cannot resolve symbol
: symbol : variable gcd
: location: class Gcd
: gcd = int1;
: ^
: cannot resolve symbol
: symbol : variable gcd
: location: class Gcd
: return( gcd );
: ^
: [/blue]
:
: Any suggestions?
:

Report
Re: Java Program - Greatest Common Divisor Posted by shivee01 on 23 Nov 2012 at 1:07 PM
import java.lang.*;
import java.math.*;
import java.io.*;

public class Gcd {

public static int gcdCalc ( int int1, int int2 )
{ int gcd=0;

if( int1 == int2) {
gcd = int1;
}//if

else {
while ( int1 != int2)
{

if( int1 > int2 ) {
int1 = int1 - int2;
gcd=int1;
}//nested-if

else {
int2 = int2 - int1;
gcd=int2;
}//nested-else
}//while
}//else

return( gcd );

}//gcdCalc


public static void main( String args[] )throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the numbers ");
int arg1 = Integer.parseInt(br.readLine());
int arg2 = Integer.parseInt(br.readLine());
int endGcd = gcdCalc( arg1, arg2 );

System.out.println( "The GCD for Integers " + arg1 + " and " + arg2 + " is " + endGcd );
}//main

}//Gcd class
Report
Re: Java Program - Greatest Common Divisor Posted by shivee01 on 23 Nov 2012 at 1:09 PM
import java.lang.*;
import java.math.*;
import java.io.*;

public class Gcd {

public static int gcdCalc ( int int1, int int2 )
{ int gcd=0;

if( int1 == int2) {
gcd = int1;
}//if

else {
while ( int1 != int2)
{

if( int1 > int2 ) {
int1 = int1 - int2;
gcd=int1;
}//nested-if

else {
int2 = int2 - int1;
gcd=int2;
}//nested-else
}//while
}//else

return( gcd );

}//gcdCalc


public static void main( String args[] )throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the numbers ");
int arg1 = Integer.parseInt(br.readLine());
int arg2 = Integer.parseInt(br.readLine());
int endGcd = gcdCalc( arg1, arg2 );

System.out.println( "The GCD for Integers " + arg1 + " and " + arg2 + " is " + endGcd );
}//main

}//Gcd class
Report
Re: Java Program - Greatest Common Divisor Posted by shivee01 on 23 Nov 2012 at 1:11 PM
import java.lang.*;
import java.math.*;
import java.io.*;

public class Gcd {

public static int gcdCalc ( int int1, int int2 )
{ int gcd=0;

if( int1 == int2) {
gcd = int1;
}//if

else {
while ( int1 != int2)
{

if( int1 > int2 ) {
int1 = int1 - int2;
gcd=int1;
}//nested-if

else {
int2 = int2 - int1;
gcd=int2;
}//nested-else
}//while
}//else

return( gcd );

}//gcdCalc


public static void main( String args[] )throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the numbers ");
int arg1 = Integer.parseInt(br.readLine());
int arg2 = Integer.parseInt(br.readLine());
int endGcd = gcdCalc( arg1, arg2 );

System.out.println( "The GCD for Integers " + arg1 + " and " + arg2 + " is " + endGcd );
}//main

}//Gcd class
Report
Re: Java Program - Greatest Common Divisor Posted by CristinaAnna on 3 Jan 2013 at 9:42 AM
Helpful, thanks.
___
Architekt wnętrz poznań na pokazy.
Report
Re: Java Program - Greatest Common Divisor Posted by CristinaAnna on 3 Jan 2013 at 9:43 AM
Helpful, thanks.
___
Projektowanie wnętrz poznań na pokazy.



 

Recent Jobs