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

Java Exercises Solutions

The document contains Java code solutions for various programming problems, including calculating the sum of 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, managing a simple bank account, finding the largest element in an array, and calculating factorials. Each solution includes a main method demonstrating its functionality with example outputs. These code snippets serve as practice exercises for Java fundamentals.

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 views4 pages

Java Exercises Solutions

The document contains Java code solutions for various programming problems, including calculating the sum of 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, managing a simple bank account, finding the largest element in an array, and calculating factorials. Each solution includes a main method demonstrating its functionality with example outputs. These code snippets serve as practice exercises for Java fundamentals.

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
You are on page 1/ 4

Java Fundamentals Practice - Code

Solutions
1. Sum of Digits
public class SumDigits {
public static int sumDigits(int number) {
int sum = 0;
while (number != 0) {
sum += number % 10;
number /= 10;
}
return sum;
}

public static void main(String[] args) {


System.out.println("Sum: " + sumDigits(1234)); // Output: 10
}
}

2. Find Maximum of Three Numbers


public class MaxOfThree {
public static int max(int a, int b, int c) {
int max = a;
if (b > max) max = b;
if (c > max) max = c;
return max;
}

public static void main(String[] args) {


System.out.println("Max: " + max(3, 7, 5)); // Output: 7
}
}

3. Check Prime Number


public class PrimeCheck {
public static boolean isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) return false;
}
return true;
}

public static void main(String[] args) {


System.out.println(isPrime(7)); // true
System.out.println(isPrime(8)); // false
}
}
4. Reverse an Array
import java.util.Arrays;

public class ReverseArray {


public static void reverse(int[] arr) {
int i = 0, j = arr.length - 1;
while (i < j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
}
public static void main(String[] args) {
int[] array = {1, 2, 3};
reverse(array);
System.out.println(Arrays.toString(array)); // [3, 2, 1]
}
}

5. Count Even and Odd Numbers in an Array


public class EvenOddCount {
public static void countEvenOdd(int[] arr) {
int even = 0, odd = 0;
for (int num : arr) {
if (num % 2 == 0) even++;
else odd++;
}
System.out.println("Even: " + even + ", Odd: " + odd);
}

public static void main(String[] args) {


int[] array = {1, 2, 3, 4, 5, 6};
countEvenOdd(array); // Even: 3, Odd: 3
}
}

6. Simple Grading System


import java.util.Scanner;
public class GradingSystem {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter marks: ");
int marks = sc.nextInt();
if (marks >= 90) System.out.println("Grade A");
else if (marks >= 80) System.out.println("Grade B");
else if (marks >= 70) System.out.println("Grade C");
else if (marks >= 50) System.out.println("Grade D");
else System.out.println("Fail");
}
}
7. Palindrome Number
public class Palindrome {
public static boolean isPalindrome(int n) {
int original = n, reversed = 0;
while (n != 0) {
reversed = reversed * 10 + n % 10;
n /= 10;
}
return original == reversed;
}

public static void main(String[] args) {


System.out.println(isPalindrome(121)); // true
System.out.println(isPalindrome(123)); // false
}
}

8. Print Multiplication Table


public class MultiplicationTable {
public static void printTable(int n) {
for (int i = 1; i <= 10; i++) {
System.out.println(n + " x " + i + " = " + (n * i));
}
}

public static void main(String[] args) {


printTable(5);
}
}

9. Simple Bank Account Class


class BankAccount {
double balance = 0;

void deposit(double amount) {


balance += amount;
}

void withdraw(double amount) {


if (amount <= balance) balance -= amount;
else System.out.println("Insufficient balance.");
}
void displayBalance() {
System.out.println("Balance: $" + balance);
}
public static void main(String[] args) {
BankAccount account = new BankAccount();
account.deposit(1000);
account.withdraw(200);
account.displayBalance(); // Balance: $800
}
}
10. Find Largest Element in Array
public class MaxArrayElement {
public static int findMax(int[] arr) {
int max = arr[0];
for (int num : arr) {
if (num > max) max = num;
}
return max;
}

public static void main(String[] args) {


int[] array = {4, 12, 7, 9};
System.out.println("Max: " + findMax(array)); // 12
}
}

11. Factorial Using a Method


public class Factorial {
public static long factorial(int n) {
long result = 1;
for (int i = 2; i <= n; i++) {
result *= i;
}
return result;
}

public static void main(String[] args) {


System.out.println("5! = " + factorial(5)); // 120
}
}

You might also like