Java Programming Exercises: Solutions and Code
Java Programming Exercises
Exercise 1: Calculate the Average of N Numbers
Write a program that displays the average of a sequence of N numbers entered by the user. The program should ask for the value of N at startup.
import java.util.*;
public class PromedioN {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of digits: ");
int n = input.nextInt();
int sum = 0;
for (int i = 1; i <= n; i = i + 1) {
System.out.print("Enter the " + i + "th element: ");
int x = input.nextInt();
sum = sum + x;
}
System.out.println("The average is: " + (sum / (double)n));
}
}
Exercise 2: Find the Roots of a Quadratic Equation
Show the roots of a quadratic equation. Consider only cases where coefficients are not equal to 0. The program should ask for the coefficients a, b, and c of the equation ax2 + bx + c = 0. If the equation is not quadratic (a = 0), display an appropriate message. Hint: Use the traditional formula to calculate the roots of a quadratic equation.
import java.util.*;
public class Estate {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a: ");
int a = input.nextInt();
System.out.print("Enter b: ");
int b = input.nextInt();
System.out.print("Enter c: ");
int c = input.nextInt();
if (a == 0) {
System.out.println("This is not a quadratic equation.");
} else {
int discriminant = b * b - 4 * a * c;
if (discriminant < 0) {
System.out.println("No real solutions.");
} else if (discriminant == 0) {
System.out.println("The solution is: " + (-b / (2.0 * a)));
} else {
System.out.println("Solution 1 is: " +
((-b + Math.sqrt(discriminant)) / (2 * a)));
System.out.println("Solution 2 is: " +
((-b - Math.sqrt(discriminant)) / (2 * a)));
}
}
}
}
Exercise 3: Calculate the Power of x to y
Display the result of xy, where x and y are integers entered by the user.
import java.util.*;
public class DistanciaPitagorica {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter x1: ");
int x1 = input.nextInt();
System.out.print("Enter y1: ");
int y1 = input.nextInt();
System.out.print("Enter x2: ");
int x2 = input.nextInt();
System.out.print("Enter y2: ");
int y2 = input.nextInt();
double distance = Math.sqrt(Math.pow((x1 - x2), 2) + Math.pow((y1 - y2), 2));
System.out.println("The distance is: " + distance);
}
}
Exercise 4: Repeat a Letter N Times
Show the letter ‘a’ repeated N times. N is a number entered by the user.
import java.util.*;
public class NVecesA {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number: ");
int n = input.nextInt();
for (int i = 1; i <= n; i = i + 1) {
System.out.print("a");
}
}
}
Exercise 5: Find the Largest and Smallest of a Sequence
Show the largest and smallest of a sequence of numbers (> 0) entered by the user. The sequence ends with 0.
import java.util.*;
public class MayorMenor {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int largest = -100000;
int smallest = 100000;
while (true) {
System.out.print("Enter a number: ");
int n = input.nextInt();
if (n == 0) {
break;
}
largest = Math.max(largest, n);
smallest = Math.min(smallest, n);
}
System.out.println("The largest is: " + largest);
System.out.println("The smallest is: " + smallest);
}
}
Exercise 6: Check if a Number is Prime
Ask for a number and show if it is prime or not.
import java.util.*;
public class Primo {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number: ");
int n = input.nextInt();
boolean isPrime = true;
for (int i = 2; i < n; i = i + 1) {
if (n % i == 0) {
isPrime = false;
}
}
if (isPrime) {
System.out.println(n + " is prime.");
} else {
System.out.println(n + " is not prime.");
}
}
}
Exercise 7: Calculate the Factorial of a Number
Show the factorial of a number.
import java.util.*;
public class Factorial {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = input.nextInt();
int f = 1;
for (int c = 1; c <= n; c = c + 1) {
f = f * c;
}
System.out.println("The factorial of " + n + " is " + f);
}
}
Exercise 8: Check if a Number is Perfect
Ask for a number and show if it is perfect or not. A number is perfect if the sum of its proper divisors equals the number itself. For example, 6 is perfect because 1 + 2 + 3 = 6.
import java.util.*;
public class Perfect {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = input.nextInt();
int sumOfProperDivisors = 0;
for (int i = 1; i < n; i = i + 1) {
if (n % i == 0) {
sumOfProperDivisors = sumOfProperDivisors + i;
}
}
if (sumOfProperDivisors == n) {
System.out.println(n + " is perfect.");
} else {
System.out.println(n + " is not perfect.");
}
}
}
Exercise 9: Calculate the Average of a Sequence
Show the average of a sequence of numbers entered by the user. The end of the sequence is indicated by a 0.
import java.util.*;
public class Average {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int sum = 0;
int count = 0;
while (true) {
System.out.print("Enter a number: ");
int n = input.nextInt();
if (n == 0) {
break;
}
sum = sum + n;
count = count + 1;
}
System.out.println("The average is: " + (sum / (double)count));
}
}
Exercise 10: Calculate the Power of x to y
Show the result of xy, where x and y are integers entered by the user.
import java.util.*;
public class Power {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter x: ");
int x = input.nextInt();
System.out.print("Enter y: ");
int y = input.nextInt();
int result = 1;
for (int i = 1; i <= y; ++i) {
result = result * x;
}
System.out.println("Result: " + result);
}
}
Exercise 11: Count the Digits of a Number
Show the number of digits in a number entered by the user.
import java.util.*;
public class CantidadDigitos {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = input.nextInt();
int count = 0;
while (n > 0) {
n = n / 10;
count = count + 1;
}
System.out.println("The number has " + count + " digits.");
}
}
Exercise 12: Get the ith Digit of a Number
Show the ith digit of a number (from right to left). For example, if the number is 3456 and you want the third digit, the answer is 4.
import java.util.*;
public class Digit {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = input.nextInt();
System.out.print("Which digit do you want? ");
int pos = input.nextInt();
int count = 0;
int digit = -1;
while (count < pos) {
digit = n % 10;
n = n / 10;
count = count + 1;
}
System.out.println("Digit: " + digit);
}
}
Exercise 13: Find Perfect Numbers Less Than N
Ask for a number and display all perfect numbers less than that number.
import java.util.*;
public class NPerfectos {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = input.nextInt();
for (int number = 2; number <= n; number = number + 1) {
int sumOfProperDivisors = 0;
for (int j = 1; j < number; j = j + 1) {
if (number % j == 0) {
sumOfProperDivisors = sumOfProperDivisors + j;
}
}
if (sumOfProperDivisors == number) {
System.out.println(number);
}
}
}
}
Exercise 14: Show All Primes Less Than or Equal to N
Show all prime numbers less than or equal to N, where N is entered by the user.
import java.util.*;
public class PrimosHastaN {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number: ");
int n = input.nextInt();
System.out.println("List of prime numbers: ");
for (int i = 2; i < n; i = i + 1) {
boolean isPrime = true;
for (int j = 2; j < i; j = j + 1) {
if (i % j == 0) {
isPrime = false;
}
}
if (isPrime) {
System.out.println(i);
}
}
}
}
Exercise 15: Display the First N Prime Numbers
Display the first N prime numbers, where N is entered by the user.
import java.util.*;
public class NPrimos {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number: ");
int n = input.nextInt();
System.out.println("List of the first " + n + " prime numbers: ");
int count = 1;
for (int i = 2; count <= n; i = i + 1) {
boolean isPrime = true;
for (int j = 2; j < i; j = j + 1) {
if (i % j == 0) {
isPrime = false;
}
}
if (isPrime) {
System.out.println(i);
count = count + 1;
}
}
}
}
Exercise 16: Find the Largest and Second Largest
Show the largest and second largest of a sequence of N numbers entered by the user. The sequence ends with 0.
import java.util.*;
public class DosMayores {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int firstLargest = -100000;
int secondLargest = -100000;
while (true) {
System.out.print("Enter a number: ");
int n = input.nextInt();
if (n == 0) {
break;
}
if (n > firstLargest) {
secondLargest = firstLargest;
firstLargest = n;
} else if (n > secondLargest) {
secondLargest = n;
}
}
System.out.println("The largest is: " + firstLargest);
System.out.println("The second largest is: " + secondLargest);
}
}
Exercise 17: Find Largest and Second Largest of N
Show the largest and second largest of a sequence of N numbers entered by the user, where N is entered by the user.
import java.util.*;
public class DosMayoresN {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("How many numbers do you wish to enter? ");
int firstLargest = 0, secondLargest = 0;
int n = input.nextInt();
int number;
int i;
for (i = 0; i < n; i = i + 1) {
System.out.print("Enter a number: ");
number = input.nextInt();
if (i == 0) {
firstLargest = number;
secondLargest = number;
}
if (number > firstLargest) {
secondLargest = firstLargest;
firstLargest = number;
}
if (number > secondLargest && number < firstLargest) {
secondLargest = number;
}
}
System.out.println("The largest is: " + firstLargest);
System.out.println("The second largest is: " + secondLargest);
}
}
Exercise 18: Find the Three Largest Numbers
Show the largest, second largest, and third largest of a sequence of numbers (> 0) entered by the user. The sequence ends with 0.
import java.util.*;
public class TresMayores {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int firstLargest = -100000;
int secondLargest = -100000;
int thirdLargest = -100000;
while (true) {
System.out.print("Enter a number: ");
int n = input.nextInt();
if (n == 0) {
break;
}
if (n > firstLargest) {
thirdLargest = secondLargest;
secondLargest = firstLargest;
firstLargest = n;
} else if (n > secondLargest) {
thirdLargest = secondLargest;
secondLargest = n;
} else if (n > thirdLargest) {
thirdLargest = n;
}
}
System.out.println("The largest is: " + firstLargest);
System.out.println("The second largest is: " + secondLargest);
System.out.println("The third largest is: " + thirdLargest);
}
}
Exercise 19: Check if Two Numbers are Amicable
Show if two numbers entered by the user are amicable or not. Two numbers are amicable if the proper divisors of the first number add up to the second, and the proper divisors of the second add up to the first. For example, 220 and 284 are amicable because 1 + 2 + 4 + 5 + 10 + 11 + 20 + 22 + 44 + 55 + 110 = 284 and 1 + 2 + 4 + 71 + 142 = 220.
import java.util.*;
public class Friends {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the first number: ");
int a = input.nextInt();
System.out.print("Enter the second number: ");
int b = input.nextInt();
int sumOfProperDivisorsA = 0;
for (int i = 1; i < a; i = i + 1) {
if (a % i == 0) {
sumOfProperDivisorsA = sumOfProperDivisorsA + i;
}
}
int sumOfProperDivisorsB = 0;
for (int i = 1; i < b; i = i + 1) {
if (b % i == 0) {
sumOfProperDivisorsB = sumOfProperDivisorsB + i;
}
}
if (a == sumOfProperDivisorsB && b == sumOfProperDivisorsA) {
System.out.println(a + " and " + b + " are amicable.");
} else {
System.out.println(a + " and " + b + " are not amicable.");
}
}
}
Exercise 20: Prime Factorization of a Number
Display the prime factorization of a number. The prime factorization of a number consists of all the prime numbers that, when multiplied together, result in the original number. For example, 63 = 3 * 3 * 7.
import java.util.Scanner;
public class DescomposicionPrimal {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = input.nextInt();
for (int i = 2; i < n; ++i) {
if (n % i == 0) {
System.out.print(i + " * ");
n = n / i;
i = i - 1;
}
}
System.out.println(n);
}
}