Test 2 study sheet

Test 2 study sheet - Method to determine if a number is...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Method to determine if a number is prime import java.util.Scanner; public static boolean isPrime (int num) { boolean result=true; for (int i=2; i <= num/2; i++) { if (num%i==0) result=false; } return result; } Method to determine if a number is perfect public static boolean isPerfect (int num) { boolean result; result=false; int subtractedNum=num; for (int i=1; i <=num/2; i++) { if (num%i==0) subtractedNum -=i; } if (subtractedNum==0) result=true; return result; } Method that reverses a number public static int reverseOfNumber (int num) { int digit=0; int reversedNum= 0; for (int i=1; i > 0; i++) { digit = num%10; if (num>=10) { reversedNum = (reversedNum + digit)*10; num /=10; } else { reversedNum += digit; break; } } return reversedNum; } Method that determines if a number is palindromic (needs reverse number) public static boolean isPalindromic (int num) { boolean result; result=true; int x=num; int j=0; while (x>=10) { x /=10; j++; } while (num>=10) {
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
int firstDigit= (int) (num/Math.pow(10,j)); int lastDigit= num%10; if (firstDigit != lastDigit) { result=false; break; } num=(int)((num%Math.pow(10,j))/10); j -=2; } return result; } A method that sums the digits of a number public static int sumOfDigits(int num) { int sum = 0; for (int i=0; num>0; i++) { sum += num%10; num /= 10; } return sum; } Method that sums the digits of a number, but alternating by adding and subtracting
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/27/2008 for the course CS 303e taught by Professor Mitra during the Fall '07 term at University of Texas.

Page1 / 6

Test 2 study sheet - Method to determine if a number is...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online