Java Programming Exercises Solutions
Java Programming Exercises Solutions
A Java program that classifies and counts numbers can use a loop in combination with the Scanner class to continuously take user input until a specific termination condition is met. Within the loop, input numbers are classified as positive, negative, or zero, and the respective counters are incremented. The implementation is shown: int positive = 0, negative = 0, zeros = 0; System.out.println("Press 1 to continue & 0 to stop"); Scanner sc = new Scanner(System.in); int input = sc.nextInt(); while(input == 1) { System.out.println("Enter your number : "); int number = sc.nextInt(); if(number > 0) { positive++; } else if(number < 0) { negative++; } else { zeros++; } System.out.println("Press 1 to continue & 0 to stop"); input = sc.nextInt(); } System.out.println("Positives : "+ positive); System.out.println("Negatives : "+ negative); System.out.println("Zeros : "+ zeros).
To calculate the circumference of a circle given its radius in Java, create a function that multiplies the radius by 2 and by the mathematical constant π (approximated as 3.14). This can be done with the formula circumference = 2 * π * radius. The Java implementation is: import java.util.*; public class Solutions { public static Double getCircumference(Double radius) { return 2 * 3.14 * radius; } public static void main(String args[]) { Scanner sc = new Scanner(System.in); Double r = sc.nextDouble(); System.out.println(getCircumference(r)); } } .
To find the GCD of two integers in Java, employ the Euclidean algorithm. This involves iteratively subtracting the smaller number from the larger number until both numbers become equal, which then represents the GCD. The Java code is as follows: import java.util.*; public class Solutions { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int n1 = sc.nextInt(); int n2 = sc.nextInt(); while(n1 != n2) { if(n1>n2) { n1 = n1 - n2; } else { n2 = n2 - n1; } } System.out.println("GCD is : "+ n2); } } .
To calculate the power of a number in Java without using built-in methods, a function can use a for-loop to repeatedly multiply the base number. This process is repeated exponent number of times. The Java implementation is: import java.util.*; public class Solutions { public static void main(String args[]) { System.out.println("Enter x"); Scanner sc = new Scanner(System.in); int x = sc.nextInt(); System.out.println("Enter n"); int n = sc.nextInt(); int result = 1; for(int i=0; i<n; i++) { result *= x; } System.out.println("x to the power n is : "+ result); } } .
To identify and sum all odd numbers up to a given number n in Java, a for-loop can be used to iterate from 1 to n, with a conditional check to verify if a number is odd using the modulo operator. If i % 2 is not equal to zero, the number is odd and added to the sum. The solution is implemented as follows: import java.util.*; public class Solutions { public static void printSum(int n) { int sum = 0; for(int i=1; i<=n; i++) { if(i % 2 != 0) { sum = sum + i; } } System.out.println(sum); } public static void main(String args[]) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); printSum(n); } } .
To write a Java function that returns the greater of two numbers, the function should use an if-else conditional statement to compare the two numbers. If the first number is greater than the second, the function should return the first number; otherwise, it should return the second. The implementation is: import java.util.*; public class Solutions { public static int getGreater(int a, int b) { if(a > b) { return a; } else { return b; } } public static void main(String args[]) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); System.out.println(getGreater(a, b)); } } .
A function can be implemented by using the Scanner class to read three integers from the user and then calculating the average by summing the numbers and dividing by three. The Java code snippet for this would be: import java.util.Scanner; public class Solutions { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int c = sc.nextInt(); int average = (a + b + c) / 3; System.out.println(average); } }.
A simple voting eligibility checker in Java can be implemented using a boolean function that checks if a given age is greater than 18, returning true if the person is eligible to vote, and false otherwise. The code is as follows: import java.util.*; public class Solutions { public static boolean isElligible(int age) { if(age > 18) { return true; } return false; } public static void main(String args[]) { Scanner sc = new Scanner(System.in); int age = sc.nextInt(); System.out.println(isElligible(age)); } } .
To generate the Fibonacci series up to n terms in Java, initialize two variables representing the first two Fibonacci numbers (0 and 1). Then utilize a for-loop to calculate subsequent numbers by summing the last two numbers in the series. This process continues until n terms are calculated. Implementation: import java.util.*; public class Solutions { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int a = 0, b = 1; System.out.print(a+" "); if(n > 1) { for(int i=2; i<=n; i++) { System.out.print(b+" "); int temp = b; b = a + b; a = temp; } System.out.println(); } } } .
An infinite loop in Java can be implemented using a do-while construct with a condition that always evaluates to true. In such a loop, the block of code will continue to execute indefinitely because the condition never becomes false. Here is an example: import java.util.*; public class Solutions { public static void main(String args[]) { do { } while(true); } } .