All pastes #763152 Raw Edit

Java Perfect/Abundant/Deficient

public java v1 · immutable
·published 2007-11-06 06:10 UTC
rendered paste body
//ugly code by Ben aka nwkeganimport java.util.*;import java.io.*;//class housing the check methodspublic class perfect {	//method to see if number is perfect, abundant, or deficient	private static int checkPerfection( int a ) {		//vector to hold all proper divisors		Vector<Integer> b = new Vector<Integer>( 0, 1 );		//1 is always a proper divisor, so we will add it as the first index of the vector		b.add( 1 );		//saves for multiple initialization for loops		int i;		//d is the sum of the proper divisors		int d = 0;		//loop to add all proper divisors into the vector		for ( i = 2; i < a; i++ ) {			if ( a % i == 0 ) {				b.add( i );			}		}		//c is the length of the vector, this way we don't go out of bounds		int c = b.capacity();		//loop to add all proper divisors together		for ( i = 0; i < c; i++ ) {			d = d + ( b.get( i ) );		}		//I should probably use a switch statement here, but I just forgot about it. noted for optimization		if ( d == a ) {			return 1;		}		else if ( d < a ) {			return 2;		}		else {			return 3;		}	}		public static void main( String[] args ) {		//number is the number entered by the user.		int number = 0;		//io streams		InputStreamReader inputStream = null;		BufferedReader buffReader = null;		try {			inputStream = new InputStreamReader( System.in );			buffReader = new BufferedReader( inputStream );			//loop to handle user input			while ( true ) {							System.out.println( "Enter a number. Enter 0 to stop." );				number = Integer.parseInt( buffReader.readLine() );				if ( number == 0 ) {					System.exit( 1 );				}				else if ( number < 0 ) {					System.out.println( "Please enter a positive integer." );				}				if ( checkPerfection( number ) == 1 ) {					System.out.println( "\n" + number + " is perfect!" );				}					else if ( checkPerfection( number ) == 2 ) {						System.out.println( "\n" + number + " is deficient!" );				}				else {					System.out.println( "\n" + number + " is abundant!" );				}			}		}		//catches for a couple of exceptions		catch ( NumberFormatException e ) {			System.out.println( "Number format exception " + e );		}		catch ( IOException e ) {			System.out.println( "IO Exception " + e );		}	}}