Number Problems
Review programs in Java to solve simple numerical problems.
Finding Sign of Number
Write a program in Java that takes input from user a number and then prints its sign or zero.
Example 1
----------
Enter a number: 1430
Number is positive.
Example 2
----------
Enter a number: -4862
Number is negative.
Example 3
----------
Enter a number: 0
Number is zero.
import java.util.Scanner;
class Program {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = in.nextInt();
if (n < 0)
System.out.println("Number is negative.");
if (n > 0)
System.out.println("Number is positive.");
if (n == 0)
System.out.println("Number is zero.");
in.close();
}
}
Determining Even or Odd
Write a program in Java that takes input from user a number and then prints if the number is even or odd.
Enter a number: 16796
It is an even number.
import java.util.Scanner;
class Program {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = in.nextInt();
int remainder = n % 2;
if (remainder == 0)
System.out.println("It is an even number.");
else
System.out.println("It is an odd number.");
in.close();
}
}
Determining Divisibility
Write a program in Java that takes a number N and a factor F from user and determines if N is divisible by F.
Example 1
----------
Enter a number: 42
Enter a factor: 7
The number is divisible by 7.
Example 2
----------
Enter a number: 54
Enter a factor: 8
The number is divisible not by 8.
import java.util.Scanner;
class Program {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = in.nextInt();
System.out.print("Enter a factor: ");
int f = in.nextInt();
int remainder = n % f;
if (remainder == 0)
System.out.println("The number is divisible by " + f);
else
System.out.println("The number is not divisible by " + f);
in.close();
}
}
Finding Minimum
Write a program in Java that takes input from user two numbers in variables A, B and then prints the variable name that contains the smaller value.
Enter A: 4862
Enter B: 1430
Smaller number is: 1430
import java.util.Scanner;
class Program {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter A: ");
int a = in.nextInt();
System.out.print("Enter B: ");
int b = in.nextInt();
if (a < b)
System.out.println("Smaller number is: " + a);
else if (a > b)
System.out.println("Smaller number is: " + b);
else
System.out.println("The numbers are equal.");
in.close();
}
}
Simple Daily Problems
Solve simple real-life problems in Java that involves branching.
Determining Citizen Class
You are asked to develop a program in your complex so that people residing there can only watch the common public TV serials only based on his/her age.
Write a program in Java that takes an age A from user and decides if the person is a minor, adult or senior citizen and then prints the programs they can watch.
| Age | Class | Can Watch |
|---|
| $00-17$ | Minor | Cartoons |
| $18-59$ | Adult | TV Series |
| $60$ and above | Senior | Devotionals |
Example 1
----------
Enter an age: 85
The person is senior.
He/She can watch devotionals!
Happy Watching!
Example 2
----------
Enter an age: 27
The person is adult.
He/She can watch TV series!
Happy Watching!
Example 3
----------
Enter an age: 12
The person is minor.
He/She can watch cartoons!
Happy Watching!
Example 4
----------
Enter an age: -30
Error: Age cannot be negative.
Happy Watching!
import java.util.Scanner;
class Program {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter an age: ");
int a = in.nextInt();
if (a < 18) {
System.out.println("The person is minor.");
System.out.println("He/She can watch cartoons!.");
} else if (a < 60) {
System.out.println("The person is adult.");
System.out.println("He/She can watch TV series.");
} else if (a >= 60) {
System.out.println("The person is senior.");
System.out.println("He/She can watch devotionals.");
} else
System.out.println("Error: Age cannot be negative.");
System.out.println("Happy Watching!");
in.close();
}
}
Determine Weekday Name
Write a program in Java that takes a weekday number D from user and prints the weekday name.
\[
\begin{aligned}
1 &= \text{Sunday} \\
2 &= \text{Monday} \\
&\cdots \\
7 &= \text{Saturday} \\
\end{aligned}
\]
Example 1
----------
Enter day number: 5
Thursday.
Example 2
----------
Enter day number: 2
Monday.
Example 3
----------
Enter day number: 8
Error: A week has only seven days.
import java.util.Scanner;
class Program {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a day number: ");
int d = in.nextInt();
if (d == 1)
System.out.println("Sunday.");
else if (d == 2)
System.out.println("Monday.");
else if (d == 3)
System.out.println("Tuesday.");
else if (d == 4)
System.out.println("Wednesday.");
else if (d == 5)
System.out.println("Thursday.");
else if (d == 6)
System.out.println("Friday.");
else if (d == 7)
System.out.println("Saturday.");
else
System.out.println("Error: A week has only seven days.");
in.close();
}
}
Geometrical Problems
Solve simple geometric problems in Java that involves branching.
Determineing Angle Type
Write a program in Java that takes an angle A from user and decides if the angle is acute, obtuse or right angle.
Example 1
----------
Enter an angle: 45
It is an acute angle.
Example 2
----------
Enter an angle: 90
It is an right angle.
Example 3
----------
Enter an angle: 135
It is an obtuse angle.
Example 3
----------
Enter an angle: -270
Error: Angle cannot be negative.
import java.util.Scanner;
class Program {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter an angle: ");
int a = in.nextInt();
if (a == 90)
System.out.println("It is an right angle.");
else if (a < 90)
System.out.println("It is an acute angle.");
else if (a > 90)
System.out.println("It is an obtuse angle.");
else
System.out.println("Error: Angle cannot be negative.");
in.close();
}
}
Every time a user enters an angle, Java decides which
PRINT should it execute based on
IF instruction.
- If a user enters 90, then the first
PRINT instruction is executed. - If a user enters a value less than 90, then the second
PRINT instruction is executed. - If a user enters a value greater than 90, then the third
PRINT instruction is executed. - If neither value matches, then user entered a negative number, so fourth
PRINT instruction is executed.
[Got it. Thanks!]Determining Triangle Validity
Write a program in Java that takes three angles A, B and C from user and determines if they can form a triangle.
\[
\text{Three angles can form a triangle if and only if they sum up to exactly 180}\degree
\]
Example 1
----------
Enter angle A: 45
Enter angle B: 45
Enter angle C: 90
The angles can form a triangle.
Example 2
----------
Enter angle A: 45
Enter angle B: 60
Enter angle C: 30
The angles cannot form a triangle.
import java.util.Scanner;
class Program {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter angle A: ");
int a = in.nextInt();
System.out.print("Enter angle B: ");
int b = in.nextInt();
System.out.print("Enter angle C: ");
int c = in.nextInt();
int sum = a + b + c;
if (sum == 180)
System.out.println("The angles can form a triangle.");
else
System.out.println("The angles cannot form a triangle.");
in.close();
}
}
Determining Special Angle Pairs
Write a program in Java that takes two angles A and B from user and determines if they are special or not.
\[
\text{Two angles are complementary if they sum up to exactly 90}\degree \\
\text{Two angles are supplementary if they sum up to exactly 180}\degree
\]
Example 1
----------
Enter angle 1: 135
Enter angle 2: 45
The angles form special angle pairs.
Example 2
----------
Enter angle 1: 60
Enter angle 2: 30
The angles form special angle pairs.
Example 3
----------
Enter angle 1: 50
Enter angle 2: 85
The angles do not form special angle pairs.
import java.util.Scanner;
class Program {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter angle 1: ");
int a = in.nextInt();
System.out.print("Enter angle 2: ");
int b = in.nextInt();
int sum = a + b;
if ((sum == 90) || (sum == 180) || (sum == 360))
System.out.println("The angles form special angle pairs.");
else
System.out.println("The angles do not form special angle pairs.");
in.close();
}
}
Determining Triangle Inequality
Write a program in Java that takes three sides A, B and C from user and determines if they can form a triangle.
The sum of the lengths of any two sides of a triangle is greater than the length of the third side
\[
\text{Three sides can form a triangle if and only if the sum of two sides is greater than the third side}
\]
Example 1
----------
Enter side A: 3
Enter side B: 4
Enter side C: 5
The sides can form a triangle.
Example 2
----------
Enter angle A: 4
Enter angle B: 3
Enter angle C: 10
The sides cannot form a triangle.
import java.util.Scanner;
class Program {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter side A: ");
int a = in.nextInt();
System.out.print("Enter side B: ");
int b = in.nextInt();
System.out.print("Enter side C: ");
int c = in.nextInt();
int ab = a + b;
int bc = b + c;
int ca = c + a;
if ((ab > c) && (bc > a) && (ca > b))
System.out.println("The sides can form a triangle.");
else
System.out.println("The sides cannot form a triangle.");
in.close();
}
}
Determining Point Location
Write a program in Java that takes coordinates X and Y from user and determines the location of point in origin, x-axis, y-axis, or any of the quadrant.
Example 1
----------
Enter x coordinate: 0
Enter y coordinate: 0
Point is at origin.
Example 2
----------
Enter x coordinate: 5
Enter y coordinate: 0
Point is in x-axis.
Example 3
----------
Enter x coordinate: 0
Enter y coordinate: 5
Point is in y-axis.
Example 4
----------
Enter x coordinate: 5
Enter y coordinate: 5
Point is in one of the quadrant.
import java.util.Scanner;
class Program {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter x coordinate: ");
int x = in.nextInt();
System.out.print("Enter y coordinate: ");
int y = in.nextInt();
if (x == 0 && y == 0)
System.out.println("Point is at origin.");
else if (x == 0)
System.out.println("Point is in x-axis");
else if (y == 0)
System.out.println("Point is in y-axis");
else
System.out.println("Point is in one of the quadrant.");
in.close();
}
}