0% found this document useful (0 votes)
6 views15 pages

Practice Questions

The document contains a series of Java programming tasks, each with a specific problem to solve, such as finding the maximum of three numbers, calculating factorials, displaying geometric sequences, and more. Each task is accompanied by a code implementation that demonstrates how to solve the problem. The document serves as a programming exercise guide for various basic algorithms and control structures in Java.

Uploaded by

Yash Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views15 pages

Practice Questions

The document contains a series of Java programming tasks, each with a specific problem to solve, such as finding the maximum of three numbers, calculating factorials, displaying geometric sequences, and more. Each task is accompanied by a code implementation that demonstrates how to solve the problem. The document serves as a programming exercise guide for various basic algorithms and control structures in Java.

Uploaded by

Yash Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

TNP Work

Que. 1 Implement a program to find and display the maximum number out of the given three
numbers.
Code:
public class Que_1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

System.out.print("Enter num 1 = ");


int num1 = input.nextInt();
System.out.print("Enter num 2 = ");
int num2 = input.nextInt();
System.out.print("Enter num 3 = ");
int num3 = input.nextInt();

int max = num1;

if (num2>max){
max = num2;
}
if (num3>max){
max = num3;
}

System.out.println("The greatest integer is = " + max);


}

Output:
Que 2. Implement a program to calculate the factorial of a given number
Code:
import java.util.*;

// Implement a program to calculate the factorial of a given number.

public class Que_2 {


public static void main(String[] args) {
Scanner in = new Scanner(System.in);

System.out.print("Enter n = ");
int n = in.nextInt();

int a = 1;

for (int i = 1; i <=n ; i++) {


a = a*i;
}

System.out.println(a);
}
}

Output:
Que 3. Implement a program to display the geometric sequence as given below for a given value n,
where n is the number of elements in the sequence. 1, 2, 4, 8, 16, 32, 64, ......, 1024
Code:
import java.util.*;

//Implement a program to display the geometric sequence as given below for a given
//value n, where n is the number of elements in the sequence.
//1, 2, 4, 8, 16, 32, 64, ......, 1024

public class Que_3 {


public static void main(String[] args) {
Scanner in = new Scanner(System.in);

System.out.print("Enter n(number of elements) = ");


int n = in.nextInt();

int num = 1;
for (int i = 0; i < n; i++) {

System.out.print(num + " ");

num = num*2;

}
}
}

Output:
Que 4. Implement a program to display the sum of two given numbers if the numbers are same. If
the numbers are not same, display the double of the sum.
Code:
import java.util.*;

//Implement a program to display the sum of two given numbers if the


//numbers are same. If the numbers are not same, display the double of the
//sum.

public class Que_4 {


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(a+b);
}
else {
System.out.println(2*(a+b));
}
}
}

Output:
Que 5. Quadratic equation is an equation with degree 2 in the form of ax2 +bx + c = 0 where a, b
and c are the coefficients. Implement a program to solve a quadratic equation.
Find the discriminant value using the formula given below. discriminant = b^2 - 4ac
∙ If the discriminant is 0, the values of both the roots will be same. Display the value of the root.
∙ If the discriminant is greater than 0, the roots will be unequal real roots. Display the values of
both the roots.
∙ If the discriminant is less than 0, there will be no real roots. Display the message "The equation
has no real root".
Use the formula given below to find the roots of a quadratic equation. x = (-b ± discriminant)/2a
Code:
Output:

Que 6. Implement a program to calculate the product of three positive integer values. However, if
one of the integers is 7, consider only the values to the right of 7 for calculation. If 7 is the last
integer, then display -1. Note: Only one of the three values can be 7.
Code:
import java.util.*;

//Implement a program to calculate the product of three positive integer


//values. However, if one of the integers is 7, consider only the values to the
//right of 7 for calculation. If 7 is the last integer, then display -1.
//Note: Only one of the three values can be 7.
public class Que_5 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);

System.out.print("Enter number 1 = ");


int num1 = in.nextInt();
System.out.print("Enter number 2 = ");
int num2 = in.nextInt();
System.out.print("Enter number 3 = ");
int num3 = in.nextInt();

if (num1==7){
System.out.println(num2+num3);
} else if (num2==7) {
System.out.println(num3);
}
else {
System.out.println("-1");
}
}
}

Output:

Que 7. Food Corner home delivers vegetarian and non-vegetarian meals to its customers based on
the order.
Code:
import java.util.Scanner;

public class Que_6 {

public static int calculateBill(char foodType, int quantity, int distance) {


// Validate input
if ((foodType != 'V' && foodType != 'N') || quantity < 1 || distance <= 0) {
return -1;
}

// Calculate cost of food


int costPerPlate;
if (foodType == 'V') {
costPerPlate = 12;
} else {
costPerPlate = 15;
}

int foodCost = costPerPlate * quantity;

// Calculate delivery charge


int deliveryCharge;
if (distance <= 3) {
deliveryCharge = 0;
} else if (distance <= 6) {
deliveryCharge = (distance - 3) * 1;
} else {
deliveryCharge = (3 * 1) + (distance - 6) * 2;
}

// Calculate final bill


int totalCost = foodCost + deliveryCharge;
return totalCost;
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

// Input values
System.out.println("Enter food type (V for Vegetarian, N for Non-
Vegetarian):");
char foodType = sc.next().charAt(0);

System.out.println("Enter quantity (minimum 1):");


int quantity = sc.nextInt();

System.out.println("Enter distance in km (greater than 0):");


int distance = sc.nextInt();

// Calculate and display the bill


int finalBill = calculateBill(foodType, quantity, distance);
if (finalBill == -1) {
System.out.println("Invalid input, bill amount: -1");
} else {
System.out.println("Final bill amount: $" + finalBill);
}

sc.close();
}
}
Output:

Que 8. The Metro Bank provides various types of loans such as car loans, business loans and house
loans to its account holders, i.e., customers.
Code:
import java.util.*;

//The Metro Bank provides various types of loans such as car loans, business
//loans and house loans to its account holders, i.e., customers.
//Implement a program to determine the eligible loan amount and the EMI
//that the bank can provide to its customers based on their salary and the
//loan type they expect to avail.

public class Que_7 {


public static void main(String[] args) {
Scanner in = new Scanner(System.in);

System.out.println("Welcome to the Metro Bank");


String accnum;

// Validating Account Number


while (true) {
System.out.print("Enter your 4-digit Account number (starting with 1): ");
accnum = in.nextLine();

if (accnum.matches("1\\d{3}")) {
System.out.println("Account number accepted");
break;
} else {
System.out.println("Invalid account number. Please try again.");
}
}

// Input Salary
System.out.print("Enter your Salary: ");
int salary = in.nextInt();

// Check loan eligibility based on salary ranges


if (salary > 25000) {
System.out.print("Enter Account Balance: ");
int balance = in.nextInt();

if (balance > 80000) {


System.out.print("Enter the type of loan you want (car, house, business): ");
String type = in.next();
System.out.print("Enter the loan amount expected: ");
int loanamount = in.nextInt();
System.out.print("Enter EMIs expected: ");
int emiexp = in.nextInt();

// Check loan eligibility based on type


boolean eligible = false;
String loanMessage = "";

switch (type) {
case "car":
eligible = (loanamount < 500000 && emiexp < 36);
loanMessage = "Eligible loan amount = 500000\nEligible EMIs = 36";
break;
case "house":
eligible = (loanamount < 6000000 && emiexp < 60);
loanMessage = "Eligible loan amount = 6000000\nEligible EMIs = 60";
break;
case "business":
if (salary > 75000) { // Only allow business loans for high salary
eligible = (loanamount < 7500000 && emiexp < 84);
loanMessage = "Eligible loan amount = 7500000\nEligible EMIs = 84";
}
break;
default:
System.out.println("Invalid loan type. Please choose car, house, or business.");
return;
}

// Output loan eligibility


if (eligible) {
System.out.println("Congrats, You are eligible for the loan.");
System.out.println(loanMessage);
} else {
System.out.println("You are not eligible for the loan.");
}
} else {
System.out.println("You are not eligible for the loan due to insufficient account
balance.");
}
} else {
System.out.println("You are not eligible for the loan based on salary criteria.");
}
}
}

Output:

Que 9. You have x number of $5 notes and y number of $1 notes. You want to purchase an item
for amount z. The shopkeeper wants you to provide exact change. You want to pay using a
minimum number of notes. How many $5 notes and $1 notes will you use? Implement a program
to find out how many $5 notes and $1 notes will be used. If an exact change is not possible, then
display -1.
Code:
import java.util.Scanner;

//You have x number of $5 notes and y number of $1 notes. You want to


//purchase an item for amount z. The shopkeeper wants you to provide exact
//change. You want to pay using a minimum number of notes. How many $5
//notes and $1 notes will you use?
//Implement a program to find out how many $5 notes and $1 notes will be
//used. If an exact change is not possible, then display -1.

public class Que_8 {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

// Input number of $5 notes (x), $1 notes (y), and the amount z


System.out.print("Enter the number of $5 notes: ");
int x = sc.nextInt();
System.out.print("Enter the number of $1 notes: ");
int y = sc.nextInt();
System.out.print("Enter the amount to be paid: ");
int z = sc.nextInt();

// Find the maximum number of $5 notes that can be used


int maxFiveDollarNotes = Math.min(x, z / 5);

// Calculate the remaining amount after using $5 notes


int remainingAmount = z - (maxFiveDollarNotes * 5);

// Check if the remaining amount can be covered by $1 notes


if (remainingAmount <= y) {
System.out.println("Number of $5 notes used: " + maxFiveDollarNotes);
System.out.println("Number of $1 notes used: " + remainingAmount);
} else {
System.out.println(-1); // Not possible to provide exact change
}

sc.close();
}
}

Output:
Que 10. Implement a program to generate and display the next date of a given date.
Code:
import java.util.*;

//Implement a program to generate and display the next date of a given date.
//Assumption = each month has 31 days

public class Que_9 {


public static void main(String[] args) {

Scanner in = new Scanner(System.in);

System.out.print("Enter Day = ");


int day = in.nextInt();
System.out.print("Enter Month = ");
int month = in.nextInt();
System.out.print("Enter Year = ");
int year = in.nextInt();

if (day < 1 || day > 31 || month < 1 || month > 12) {


System.out.println("Enter valid date");
} else {
if (month == 12 && day == 31) {
day = 1;
month = 1;
year = year + 1;
} else if (day == 31) {
day = 1;
month = month + 1;
} else {
day = day + 1;
}

// Print the next date after incrementing


System.out.println("Next Date: " + day + "-" + month + "-" + year);
}
}
}

Output:
Que 11. Implement a program that displays a message for a given number based on
the below conditions.
● If the number is a multiple of 3, display "Zip".
● If the number is a multiple of 5, display "Zap".
● If the number is a multiple of both 3 and 5, display "Zoom",
● For all other cases, display "Invalid".
Code:
import java.util.Scanner;

public class Que_10 {


public static void main(String[] args) {
Scanner in = new Scanner(System.in);

System.out.print("Enter a number: ");


int number = in.nextInt();

if (number % 3 == 0 && number % 5 == 0) {


System.out.println("Zoom");
}
else if (number % 3 == 0) {
System.out.println("Zip");
}
else if (number % 5 == 0) {
System.out.println("Zap");
}
else {
System.out.println("Invalid");
}
}
}

Output:

You might also like