Test 2 study sheet

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

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

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) {

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

View Full Document
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; }
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern