Objective:
After performing this exercise, you will be able to solve programming problems using:
- 'if' statements
- 'if-else' statement
- Nested 'if' structure
- 'while' loop
- 'do-while' loop
- 'for' loop
Activity 1:
Write a program to input electricity unit charges and calculate total electricity bill
according to the given condition:
For first 50 units Rs. 0.50/unit
For next 100 units Rs. 0.75/unit
For next 100 units Rs. 1.20/unit
For unit above 250 Rs. 1.50/unit
An additional surcharge of 20% is added to the bill
Solution:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int unit;
float amt = 0, total_amt, sur_charge;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter Total Units Consumed: ");
unit = scanner.nextInt();
if (unit <= 50) {
amt = unit * 0.50f;
}
else if (unit <= 150) {
amt = 25 + ((unit - 50) * 0.75f);
}
else if (unit <= 250) {
amt = 100 + ((unit - 150) * 1.20f);
}
else {
amt = 220 + ((unit - 250) * 1.50f);
}
sur_charge = amt * 0.20f;
total_amt = amt + sur_charge;
System.out.println("Electricity Bill = " + total_amt);
scanner.close();
}
}
Activity 2:
Write a program that calculates and displays a person’s body mass index (BMI). The BMI is often used to determine whether a person with a sedentary lifestyle is overweight or underweight for his or her height. A person’s BMI is calculated with the following formula:
BMI = weight x 703 / height?
Where weight is measured in pounds and height is measured in inches. The program should display a message indicating whether the person has optimal weight, is underweight, or is overweight. A person’s weight is considered to be optimal if his or her BMI is between 18.5 and 25. If the BMI is less than 18.5, the person is considered to be underweight. If the BMI value is greater than 25, the person is considered to be overweight.
Solution:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
double bmi;
double weight;
double height;
double eq1;
double eq2;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter Weight in pounds");
weight = scanner.nextDouble();
System.out.println("Enter Height in inches");
height = scanner.nextDouble();
eq1 = weight * 703;
eq2 = height * height;
bmi = eq1 / eq2;
System.out.println("Your Body Mass Index = " + bmi);
if (bmi > 25.0) {
System.out.println("The Person is Overweight!");
}
else {
if (bmi <= 25.0 && bmi >= 18.5) {
System.out.println("The Person's Weight is Optimal!");
}
else if (bmi < 18.5) {
System.out.println("The Person is Underweight!");
}
}
scanner.close();
}
}
Activity 3:
Take input of age of 3 people by the user and determine the oldest and youngest among them.
Solution:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int a, b, c;
Scanner scanner = new Scanner(System.in);
System.out.println("Type the age of First Person: ");
a = scanner.nextInt();
System.out.println("Type the age of Second Person: ");
b = scanner.nextInt();
System.out.println("Type the age of Third Person: ");
c = scanner.nextInt();
if (a > b) {
if (c < b) {
System.out.println("The First Person is Oldest");
System.out.println("The Third Person is Youngest");
}
else {
System.out.println("The First Person is Oldest");
System.out.println("The Second Person is Youngest");
}
}
else if (b > a) {
if (c > a) {
System.out.println("The Second Person is Oldest");
System.out.println("The Third Person is Youngest");
}
else {
System.out.println("The Second Person is Oldest");
System.out.println("The First Person is Youngest");
}
}
else if ((c > a) && (c > b)) {
if (a > b) {
System.out.println("The Third Person is Oldest");
System.out.println("The Second Person is Youngest");
}
else {
System.out.println("The Third Person is Oldest");
System.out.println("The First Person is Youngest");
}
}
scanner.close();
}
}
Activity 4:
Write a program to find the eligibility of admission for a professional course based on the following criteria: Marks in Maths >=65 and Marks in Phy >=55 and Marks in Chem>=50 and Total in all three subject >=190 or Total in Maths and Physics >=140
Solution:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int p, c, m;
Scanner scanner = new Scanner(System.in);
System.out.println("Input the marks obtained in Physics :");
p = scanner.nextInt();
System.out.println("Input the marks obtained in Chemistry :");
c = scanner.nextInt();
System.out.println("Input the marks obtained in Mathematics :");
m = scanner.nextInt();
System.out.println("Total marks of Maths, Physics and Chemistry :\n" + (m + p + c));
System.out.println("Total marks of Maths and Physics :\n" + (m + p));
if (m >= 65) {
if (p >= 55) {
if (c >= 50) {
if ((m + p + c) >= 190 || (m + p) >= 140) {
System.out.println("The Candidate is eligible for admission.\n");
}
else
System.out.println("The Candidate is not eligible.\n");
}
else
System.out.println("The Candidate is not eligible.\n");
}
else
System.out.println("The Candidate is not eligible.\n");
}
else
System.out.println("The Candidate is not eligible.\n");
scanner.close();
}
}
Activity 5:
Print the Fibonacci sequence (0,1,1,2,3,5,8,13,21, 34,……..) till the N member. Take N from user
input. Fibonacci sequence is given by formula: sum the last two numbers to get the current.
Solution:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int n;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number upto which you want to print Fibonacci Sequence: ");
n = scanner.nextInt();
int next = 0;
int num = 0;
int num1 = 1;
System.out.print("The Series is: -" + num + "," + num1 + ",");
next = num + num1;
while (next <= n) {
System.out.print(next + ",");
num = num1;
num1 = next;
next = num + num1;
}
scanner.close();
}
}
Activity 6:
Write a program that prompts the user to enter the values. Get the input again and again until
user enter -1 so the condition to break the loop is -1. Your task is to determine the largest and smallest
value entered by the user.
Solution:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int n1, n2, large, small;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter Numbers: ");
n1 = scanner.nextInt();
large = n1;
small = n1;
boolean status = true;
while (status) {
n2 = scanner.nextInt();
if (n2 > large)
large = n2;
else if (n2 < small && n2 >= 0)
small = n2;
if (n2 == -1)
status = false;
}
System.out.println("The Largest Number is " + large);
System.out.println("The Smallest Numbers is " + small);
scanner.close();
}
}
Activity 7:
Write a Program to Calculate the Factorial of a Positive Integer. The program repeats the process until the user enters 0.
Solution:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int n, a, i = 1;
double fact;
do {
fact = 1;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a Positive integer: ");
n = scanner.nextInt();
if (n < 0) {
System.out.println("Error! Factorial of a negative number doesn't exist.");
break;
}
else {
for (a = 1; a <= n; ++a) {
fact *= a;
}
System.out.println("Factorial of " + n + " is " + fact);
}
System.out.println("If you want to Calculate another factorial press 1 otherwise press 0. ");
i = scanner.nextInt();
} while (i == 1);
scanner.close();
}
}
Activity 8:
Write a program that gets starting and ending point from the user and displays all odd
numbers in the given range using do-while loop.
Solution:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int start, end, num;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter Starting Number: ");
start = scanner.nextInt();
System.out.println("Enter Ending Number: ");
end = scanner.nextInt();
System.out.println("All Numbers between " + start + " and " + end + " are : ");
num = start;
do {
if (num % 2 != 0) {
System.out.println(num);
}
num++;
} while (num <= end);
scanner.close();
}
}
Activity 9:
Write a program that inputs table number and length of table and then displays the table
using for loop.
Solution:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int num, length;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter Table Number: ");
num = scanner.nextInt();
System.out.println("Enter Length of Table: ");
length = scanner.nextInt();
for (int i = 1; i <= length; ++i) {
System.out.println(num + " * " + i + " = " + num * i);
}
scanner.close();
}
}
Activity 10:
Write a program to print the following output using loop:
- - - - - * - - - - -
- - - - * - * - - - -
- - - * - - - * - - -
- - * - - - - - * - -
- * - - - - - - - * -
* - - - - - - - - - *
Solution:
public class Main {
public static void main(String[] args) {
for (int i = 1; i <= 6; i++) {
for (int j = 1; j <= (6 - i); j++) {
System.out.print(" ");
}
for (int k = 1; k <= i * 2 - 1; k++) {
if (k == 1 || k == i * 2 - 1) {
System.out.print("*");
}
else if (i == 6) {
System.out.print("*");
}
else {
System.out.print(" ");
}
}
System.out.print("\n");
}
}
}
Activity 11:
Write a program that displays the following using do-while loop.
4 4 4 4
3 3 3
2 2
1
Solution:
public class Main {
public static void main(String[] args) {
int row = 1, column = 1;
int i;
row = 4;
do {
column = 1;
do {
System.out.print(row);
++column;
} while (column <= row);
--row;
System.out.print("\n");
} while (row > 0);
}
}
Activity 12:
Write a program to print the following pyramid pattern using loop:
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
Solution:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int space, rows;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter number of rows: ");
rows = scanner.nextInt();
for (int i = 1, j = 0; i <= rows; ++i, j = 0) {
for (space = 1; space <= rows - i; ++space) {
System.out.print(" ");
}
while (j != 2 * i - 1) {
System.out.print("* ");
++j;
}
System.out.print("\n");
}
scanner.close();
}
}