0% found this document useful (0 votes)
24 views

Lab1

The document contains code for two Java programs that apply different looping and branching statements to banking scenarios. The first program uses an entry-controlled while loop and an exit-controlled do-while loop to validate a user's PIN for an ATM. The second program uses nested switch statements - the outer switch evaluates the account type selected, and the inner switches set interest rates based on term for savings and fixed deposit accounts. Both programs take user input, apply the appropriate control structures, and output the results in a readable format.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Lab1

The document contains code for two Java programs that apply different looping and branching statements to banking scenarios. The first program uses an entry-controlled while loop and an exit-controlled do-while loop to validate a user's PIN for an ATM. The second program uses nested switch statements - the outer switch evaluates the account type selected, and the inner switches set interest rates based on term for savings and fixed deposit accounts. Both programs take user input, apply the appropriate control structures, and output the results in a readable format.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Q1.

Apply “different entry-controlled, exit-controlled looping statements & branching


statements” to a problem/scenario belonging to your domain (of your choice). Choose
suitable variables & instructions for your program and print the results in a good
format.
--------------------------------------------------------------------------------------------------------------------------------------

Code

import java.util.Scanner;

public class BankingPinValidator {

public static void main(String[] args) {

int validPin = 1234; // Valid PIN to compare against

Scanner scanner = new Scanner(System.in);

// Entry-controlled while loop

System.out.println("Entry-controlled while loop:");

boolean pinValidated = false;

while (!pinValidated) {

System.out.print("Enter your PIN: ");

int enteredPin = scanner.nextInt();

if (enteredPin == validPin) {

System.out.println("PIN validated successfully!");

pinValidated = true;

} else {

System.out.println("Invalid PIN! Please try again.");

// Exit-controlled do-while loop

System.out.println("\nExit-controlled do-while loop:");


boolean pinValidated2;

do {

System.out.print("Enter your PIN: ");

int enteredPin2 = scanner.nextInt();

if (enteredPin2 == validPin) {

System.out.println("PIN validated successfully!");

pinValidated2 = true;

} else {

System.out.println("Invalid PIN! Please try again.");

pinValidated2 = false;

} while (!pinValidated2);

System.out.println("Thank you for using our banking services!");

OUTPUT

PS D:\JAVA> javac BankingPinValidator.java

PS D:\JAVA> java BankingPinValidator.java

Entry-controlled while loop:

Enter your PIN: 1234

PIN validated successfully!

Exit-controlled do-while loop:

Enter your PIN: 1234

PIN validated successfully!

Thank you for using our banking services!


Q2. Apply “nesting of switch statement” to a problem/scenario belonging to your domain

(of your choice). Choose suitable variables & instructions for your program and print

the results in a good format.

--------------------------------------------------------------------------------------------------------------------------------------

CODE

import java.util.Scanner;

public class BankInterestRates {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println("Select an account type:");

System.out.println("1. Savings Account");

System.out.println("2. Checking Account");

System.out.println("3. Fixed Deposit Account");

int accountType = scanner.nextInt();

double interestRate = 0.0;

switch (accountType) {

case 1:

System.out.println("Savings Account");

int savingsTerm = 0;

System.out.println("Enter the term (in years):");

savingsTerm = scanner.nextInt();

switch (savingsTerm) {

case 1:

interestRate = 3.5;

break;

case 2:
interestRate = 4.0;

break;

case 3:

interestRate = 4.5;

break;

default:

System.out.println("Invalid term for Savings Account!");

return;

break;

case 2:

System.out.println("Checking Account");

interestRate = 1.0;

break;

case 3:

System.out.println("Fixed Deposit Account");

int depositTerm = 0;

System.out.println("Enter the term (in years):");

depositTerm = scanner.nextInt();

switch (depositTerm) {

case 1:

interestRate = 5.0;

break;

case 2:

interestRate = 5.5;

break;

case 3:

interestRate = 6.0;

break;

default:
System.out.println("Invalid term for Fixed Deposit Account!");

return;

break;

default:

System.out.println("Invalid account type!");

return;

System.out.println("Interest rate for the selected account type: " + interestRate + "%");

OUTPUT

javac BankInterestRates.java

PS D:\JAVA> java BankInterestRates.java

Select an account type:

1. Savings Account

2. Checking Account

3. Fixed Deposit Account

Savings Account

Enter the term (in years):

Interest rate for the selected account type: 4.0%

Current Account

Interest rate for the selected account type: 1.0%

Fixed Deposit Account

Enter the term (in years):

Interest rate for the selected account type: 5.5%

You might also like