0% found this document useful (0 votes)
2 views2 pages

Java Methods for Common Algorithms

The document outlines a series of programming tasks, including methods for summing digits, finding the maximum of three numbers, checking for prime numbers, reversing an array, counting even and odd numbers, implementing a grading system, checking for palindromes, printing multiplication tables, creating a simple bank account class, finding the largest element in an array, and calculating factorials. Each task includes a brief description and an example. The tasks are designed to practice fundamental programming concepts.

Uploaded by

mo33stafa5
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Java Methods for Common Algorithms

The document outlines a series of programming tasks, including methods for summing digits, finding the maximum of three numbers, checking for prime numbers, reversing an array, counting even and odd numbers, implementing a grading system, checking for palindromes, printing multiplication tables, creating a simple bank account class, finding the largest element in an array, and calculating factorials. Each task includes a brief description and an example. The tasks are designed to practice fundamental programming concepts.

Uploaded by

mo33stafa5
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

Sum of Digits
Write a method int sumDigits(int number) that returns the sum of its digits.
Example: sumDigits(1234) returns 10

2. Maximum of Three Numbers


Write a method int max(int a, int b, int c) that returns the largest of three integers.
Example: max(3, 7, 5) returns 7

3. Prime Number Checker


Write a method boolean isPrime(int number) that returns true if the number is prime.
Example: isPrime(7) returns true, isPrime(8) returns false

4. Reverse an Array
Write a method void reverseArray(int[] array) that reverses the elements of an array in place.
Example: {1, 2, 3} becomes {3, 2, 1}

5. Count Even and Odd Numbers


Write a method that takes an integer array and prints the count of even and odd numbers.

6. Simple Grading System


Write a method that accepts a score (0–100) and prints the grade according to the scale: A (90+),
B (80–89), C (70–79), F (<70).
Use if-else statements.

7. Palindrome Number
Write a method boolean isPalindrome(int number) to check if an integer reads the same forward
and backward.
Example: 121 is a palindrome.

8. Multiplication Table
Write a method that accepts an integer and prints its multiplication table from 1 to 10.

9. Simple Bank Account Class


Create a class BankAccount with fields for balance. Add methods for deposit(double amount),
withdraw(double amount), and displayBalance().
10. Largest Element in Array
Write a method that accepts an integer array and returns the largest number.

11. Factorial Calculator


Write a method int factorial(int number) that returns the factorial of a number.
Example: factorial(5) returns 120

You might also like