Q1 A bank announces new rates for Term Deposit Schemes for their customers and Senior Citizens as given below: Term Rate of Interest (General) Rate of Interest (Senior Citizen) Up to 1 year 7.5% 8.0% Up to 2 years 8.5% 9.0% Up to 3 years 9.5% 10.0% More than 3 years 10.0% 11.0% The 'senior citizen' rates are applicable to the customers whose age is 60 years or more. Write a program to accept the sum (p) in term deposit scheme, age of the customer and the term. A1. import java.util.*; public class Bank_Deposit { public static void main() { Scanner in = new Scanner(System.in); System.out.print("Enter sum: "); double sum = in.nextDouble(); System.out.print("Enter age: "); int age = in.nextInt(); System.out.print("Enter term: "); double term = in.nextDouble(); double si = 0.0; if (term <= 1 && age < 60) 1 si = (sum * 7.5 * term) / 100; else if (term <= 1 && age >= 60) si = (sum * 8.0 * term) / 100; if (term <= 2 && age < 60) si = (sum * 8.5 * term) / 100; else if (term <= 2 && age >= 60) si = (sum * 9.0 * term) / 100; if (term <= 3 && age < 60) si = (sum * 9.5 * term) / 100; else if (term <= 3 && age >= 60) si = (sum * 10.0 * term) / 100; if (term > 3 && age < 60) si = (sum * 10.0 * term) / 100; else if (term > 3 && age >= 60) si = (sum * 11.0 * term) / 100; double amt = sum + si; System.out.println("Amount Deposited: " + sum); System.out.println("Term: " + term); System.out.println("Age: " + age); System.out.println("Interest Earned: " + si); System.out.println("Amount Paid: " + amt); } } 2 Variable Description Variable Data Type Description sum age term si double int double double amt double Input and store the sum deposited Input and store the age of the customer input and store the term of the deposited sum Compute and display the interest earned on the sum Compute and display the final amount after completion of term Output Enter sum: 10000 Enter age: 15 Enter term: 3 Amount Deposited: 10000.0 Term: 3.0 Age: 15 Interest Earned: 2850.0 Amount Paid: 12850.0 Enter sum: 10000 Enter age: 65 Enter term: 3 Amount Deposited: 10000.0 Term: 3.0 Age: 65 Interest Earned: 3000.0 Amount Paid: 13000.0 3 Q2. Write a program using switch case to find the volume of a cube, a sphere and a cuboid. For an incorrect choice, an appropriate error message should be displayed. 1. Volume of a cube = s * s *s 2. Volume of a sphere = (4/3) * π * r * r * r (π = (22/7)) 3. Volume of a cuboid = l*b*h A2. import java.util.*; public class Volume { public static void main() { Scanner in = new Scanner(System.in); System.out.println("1. Volume of Cube"); System.out.println("2. Volume of Sphere"); System.out.println("3. Volume of Cuboid"); System.out.println("Enter your choice: "); int choice = in.nextInt(); switch(choice) { case 1: System.out.print("Enter side of cube: "); int s = in.nextInt (); System.out.println("Volume of cube = " + Math.pow(s, 3)); break; case 2: System.out.print("Enter radius of sphere: "); int r = in.nextInt (); System.out.println("Volume of sphere = " + (4 / 3.0) * (22 / 7.0) * dfhgbhd Math.pow(r, 3)); break; 4 case 3: System.out.print("Enter length of cuboid: "); int l = in.nextInt (); System.out.print("Enter breadth of cuboid: "); int b = in.nextInt (); System.out.print("Enter height of cuboid: "); int h = in.nextInt (); System.out.println("Volume of cuboid = " + l * b * h); break; default: System.out.println("Wrong choice!"); }}} 5 Variable Description Variable Data Type Description choice s r l b h int int int int int int Input and store choice of user Input and store side of cube Input and store radius of sphere Input and store length of cuboid Input and store breadth of cuboid Input and store height of cuboid Output 1. Volume of Cube 2. Volume of Sphere 3. Volume of Cuboid Enter your choice: 1 Enter side of cube: 4 Volume of cube = 64.0 1. Volume of Cube 2. Volume of Sphere 3. Volume of Cuboid Enter your choice: 2 Enter radius of sphere: 6 Volume of sphere = 905.142857142857 1. Volume of Cube 2. Volume of Sphere 3. Volume of Cuboid Enter your choice: 5 Wrong choice! 6 Q3. A special two-digit number is such that when the sum of its digits is added to the product of its digits, the result is equal to the original twodigit number. Example: Consider the number 59. Sum of digits = 5 + 9 = 14 Product of digits = 5 * 9 = 45 Sum of the sum of digits and product of digits = 14 + 45 = 59 Write a program to accept a two-digit number. Add the sum of its digits to the product of its digits. If the value is equal to the number input, then display the message "Special two-digit number" otherwise, display the message "Not a special two-digit number". A3. import java.util.*; public class Special_Num { public static void main() { Scanner in = new Scanner(System.in); System.out.print("Enter a 2 digit number: "); int og_num = in.nextInt(); int num = og_num; int count = 0, digit_sum = 0, digit_product = 1; while (num != 0) { int digit = num % 10; num /= 10; digit_sum += digit; digit_product *= digit; count++; } if (count != 2) 7 System.out.println("Input not valid. Please enter a 2-digit number."); else if ((digit_sum + digit_product) == og_num) System.out.println("Special 2-digit number"); else System.out.println("Not a special 2-digit number"); }} Variable Description Variable Data Type Description og_num num int int count int digit int digit_sum int Input and store number given by user Store the same value as og_num, then compute if og_num is a special number Compute if the number given by user is a 2 digit number Used in computation of values of sum and product of the digits in the number Compute the value of sum of the digits in the number Compute the value of product of the digits in the number digit_product int Output Enter a 2 digit number: 59 Special 2-digit number Enter a 2 digit number: 76 Not a special 2-digit number Enter a 2 digit number: 444 Input not valid. Please enter a 2-digit number. 8 Q4. Write a menu driven program to perform the following tasks: (a) Tribonacci numbers are a sequence of numbers similar to Fibonacci numbers, except that a number is formed by adding the three previous numbers. Write a program to display the first twenty Tribonacci numbers. For example; 1, 1, 2, 4, 7, 13, ...................... (b) Write a program to display all Sunny numbers in the range from 1 to n. [Hint: A number is said to be sunny number if square root of (n+1) is an integer.] For example, 3, 8, 15, 24, 35,......................are sunny numbers. For an incorrect option, an appropriate error message should be displayed. A4. import java.util.*; public class Series { public static void main() { Scanner in = new Scanner(System.in); System.out.println("1. Tribonacci numbers"); System.out.println("2. Sunny numbers"); System.out.print("Enter your choice: "); int ch = in.nextInt(); switch (ch) { case 1: int a = 1, b = 1, c = 2; System.out.print(a + " " + b + " " + c); for (int i = 4; i <= 20; i++) { 9 int term = a + b + c; System.out.print(" " + term); a = b; b = c; c = term; } break; case 2: System.out.print("Enter n: "); int n = in.nextInt(); double sq_root; for (int i = 3; i <= n; i++) { sq_root = Math.sqrt(i + 1); double diff = sq_root - Math.floor(sq_root); if (diff == 0) System.out.print(i + " "); } break; default: System.out.println("Incorrect choice"); break; }}} 10 Variable Description Variable Data Type Description ch a int int b int c int i term int int n sq_root int double diff double Input and store choice of user Used to help compute next value in tribonacci series Used to help compute next value in tribonacci series Used to help compute next value in tribonacci series Counter Compute and display next value in tribonacci series Input and store number of terms from user Compute and store the square root of natural numbers less than n Compute whether the square root of the number is an integer or not Output 1. Tribonacci numbers 2. Sunny numbers Enter your choice: 1 1 1 2 4 7 13 24 44 81 149 274 504 927 1705 3136 5768 10609 19513 35890 660121. Tribonacci numbers 2. Sunny numbers Enter your choice: 2 Enter n: 50 3 8 15 24 35 48 11 Q5. Write the programs to display the following patterns a) 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 Ans) public class Pattern_a { public static void main() { int a = 15; for (int i = 5; i > 0; i--) { for (int j = 1; j <= i; j++) { System.out.print(a-- + " "); } System.out.println(); }}} b) 1 2 3 4 5 22345 33345 44445 55555 Ans) public class Pattern_b { public static void main() { for (int i = 1; i <= 5; i++) { for (int j = 1; j < i; j++) System.out.print(i + " "); for (int k = i; k <= 5; k++) System.out.print(k + " "); 12 System.out.println(); }}} c) * *# *#* *#*# *#*#* Ans) public class Pattern_c { public static void main() { for (int i = 1; i <= 5; i++) { for (int j = 1; j <= i; j++) { if (j % 2 == 0) System.out.print("# "); else System.out.print("* "); } System.out.println(); }}} 13 Q6. Write a program to accept a list of 20 integers. Sort the first 10 numbers in ascending order and next the 10 numbers in descending order by using 'Bubble Sort' technique. Finally, print the complete list of integers. A6. import java.util.*; public class Bubble_Sort { public static void main() { Scanner in = new Scanner(System.in); int a[] = new int[20]; System.out.println("Enter 20 numbers:"); for (int i = 0; i < 20; i++) { a[i] = in.nextInt(); } for (int i = 0; i < 9; i++) { for (int j = 0; j < 9 - i; j++) { if (a[j] > a[j + 1]) { int t = a[j + 1]; a[j + 1] = a[j]; a[j] = t; } } } System.out.print(" "); for (int i = 0; i < 9; i++) { for (int j = 10; j < 19 - i; j++) { if (a[j] < a[j + 1]) { 14 int t = a[j + 1]; a[j + 1] = a[j]; a[j] = t; }}} System.out.println("Sorted Array:"); for (int i = 0; i < 20; i++) { System.out.print(a[i] + " "); }}} Variable Description Variable Data Type Description a[] int i j t int int int Input and store given array, and compute and display sorted array Counter Counter To interchange values within the array to arrange in ascending or descending order Output Enter 20 numbers: 1 9 6 4 7 3 9 45 746 34 7 435 15 78 23 765 34 76 2 8 94 Sorted Array: 1 3 4 6 7 9 9 34 45 746 765 435 94 78 76 34 23 8 7 2 16 Q7. If arrays M and M + N are as shown below, write a program in Java to find the array N. M = -1 0 2 M + N = -6 9 4 -3 -1 6 4 5 0 4 3 -1 1 -2 -3 A7. public class Find_N { public static void main() { int M[][] = {{-1, 0, 2}, {-3, -1, 6}, {4, 3, -1}}; int Sum[][] = {{-6, 9, 4}, {4, 5, 0}, {1, -2, -3}}; int N[][] = new int[3][3]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { N[i][j] = Sum[i][j] - M[i][j]; } } System.out.println("Array N:"); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { System.out.print(N[i][j]); System.out.print(' '); } 17 System.out.println(); }}} Variable Description Variable Data Type Description M[][] Sum[][] N[][] i j int int int int int Store the given array M Store the given sum of arrays M and N Compute and display array N Counter Counter Output Array N: -5 9 2 7 6 -6 -3 -5 -2 18 Q8. Write a program to input a sentence. Find and display the following: (i) Number of words present in the sentence (ii) Number of letters present in the sentence Assume that the sentence has neither include any digit nor a special character. A8. import java.util.*; public class Word_Letter { public static void main() { Scanner in = new Scanner(System.in); System.out.println("Enter a sentence:"); String str = in.nextLine(); int wCount = 0, lCount = 0; int len = str.length(); for (int i = 0; i < len; i++) { char ch = str.charAt(i); if (ch == ' ') wCount++; else lCount++; } wCount++; System.out.println("No. of words = " + wCount); System.out.println("No. of letters = " + lCount); }} 19 Variable Description Variable Data Type Description str wCount String int lCount int len int ch char Input and store sentence given by user Computes and displays number of words in sentence Computes and displays number of letters in sentence Computes and stores the total number of characters in sentence Used to help check if a letter is present in a character position. Output Enter a sentence: I am listening to music No. of words = 5 No. of letters = 19 20 Q9. Write a program in Java to store 10 words in a Single Dimensional Array. Display only those words which are Palindrome. Sample Input: MADAM, TEACHER, SCHOOL, ABBA, ......... Sample Output: MADAM ABBA .......... .......... A9. import java.util.*; public class Palindrome { public static void main() { Scanner in = new Scanner(System.in); String words[] = new String[10]; System.out.println("Enter 10 words:"); for (int i = 0; i < 10; i++) { words[i] = in.nextLine(); } System.out.println("Palindrome Words:"); for (int i = 0; i < 10; i++) { String str = words[i].toUpperCase(); int strLen = str.length(); boolean isPalin = true; for (int j = 0; j < strLen / 2; j++) { if (str.charAt(j) != str.charAt(strLen - 1 - j)) { isPalin = false; break; } } 21 if (isPalin) System.out.println(words[i]); }}} Variable Description Variable Data Type Description words[] i j str String int int String strLen int isPalin boolean Input and store words given by user Counter Counter Store word in capital letters and helps to check whether word is a palindrome Compute and store the length of each word in each iteration Store if word is palindrome Output Enter 10 words: abba abccba ccc clock level madam civic computer headphone music Palindrome Words: abba abccba ccc level civic 22 Q10. Write a program to input a three digit number. Use a method int Armstrong(int n) to accept the number. The method returns 1, if the number is Armstrong, otherwise zero(0). Sample Input: 153 Sample Output: 153 ⇒ 13 + 53 + 33 = 153 It is an Armstrong Number. A10. import java.util.*; public class Armstrong_Num { int armstrong(int n) { int cube_sum = 0, int x = n; while (n > 0) { int digit = n % 10; cube_sum += Math.pow(digit,3); n /= 10; } if (cube_sum == x) return 1; else return 0; } public static void main() { Scanner in = new Scanner(System.in); System.out.print("Enter Number: "); int num = in.nextInt(); Armstrong_Num obj = new Armstrong_Num(); int r = obj.armstrong(num); if (r == 1) System.out.println(num + " is an Armstrong number"); else System.out.println(num + " is not an Armstrong number"); 23 }} Variable Description Variable Data Type Description n num cube_sum digit int int int int x int r int Store value of num in method armstrong Input and store number given by user Sum of cube of digits Compute value of each digit to be added as cube to cube_sum Compute if sum of cube of digits of number is equal to the number given Return variable Output Enter Number: 153 153 is an Armstrong number Enter Number: 134 134 is not an Armstrong number 24 Q11. Write a class with the name Perimeter using method overloading that computes the perimeter of a square, a rectangle and a circle. Formula: Perimeter of a square = 4 * s Perimeter of a rectangle = 2 * (l + b) Perimeter of a circle = 2 * (22/7) * r A11. import java.util.*; public class Perimeter { public double perimeter(int s) { double p = 4 * s; return p; } public double perimeter(int l, int b) { double p = 2 * (l + b); return p; } public double perimeter(double r) { double p = 2 * 22/7 * r; return p; } public static void main() { Scanner in = new Scanner(System.in); Perimeter obj = new Perimeter(); System.out.print("Enter side of square: "); int side = in.nextInt(); System.out.println("Perimeter of square = " + obj.perimeter(side)); 25 System.out.print("Enter length of rectangle: "); int l = in.nextInt(); System.out.print("Enter breadth of rectangle: "); int b = in.nextInt(); System.out.println("Perimeter of rectangle = " + obj.perimeter(l, b)); System.out.print("Enter radius of circle: "); double r = in.nextDouble(); System.out.println("Perimeter of circle = " + obj.perimeter(r)); } } Variable Description Variable Data Type Description s l b r p int int int double double Input and store side of square Input and store length of rectangle Input and store breadth of rectangle Input and store radius of circle Compute and display perimeters of each shape Output Enter side of square: 3 Perimeter of square = 12.0 Enter length of rectangle: 2 Enter breadth of rectangle: 4 Perimeter of rectangle = 12.0 Enter radius of circle: 21 Perimeter of circle = 126.0 26 Q12. Define a class Telephone having the following description: Class name : Telephone Data Members Purpose int prv, pre to store the previous and present meter readings int call to store the calls made (i.e. pre - prv) String name to store name of the consumer double amt to store the amount double total to store the total amount to be paid Member functions Purpose void input() Stores the previous reading, present reading and name of the consumer void cal() Calculates the amount and total amount to be paid void display() Displays the name of the consumer, calls made, amount and total amount to be paid Write a program to compute the monthly bill to be paid according to the given conditions and display the output as per the given format. Calls made Rate Up to 100 calls No charge For the next 100 calls 90 paise per call 27 Calls made Rate For the next 200 calls 80 paise per call More than 400 calls 70 paise per call However, every consumer has to pay ₹180 per month as monthly rent for availing the service. A12. import java.util.*; public class Telephone { int prv; int pre; int call; String name; double amt; double total; void input() { Scanner in = new Scanner(System.in); System.out.print("Enter Customer Name: "); name = in.nextLine(); System.out.print("Enter previous reading: "); prv = in.nextInt(); System.out.print("Enter present reading: "); pre = in.nextInt(); } void cal() { call = pre - prv; if (call <= 100) amt = 0; else if (call <= 200) amt = (call - 100) * 0.9; 28 else if (call <= 400) amt = (100 * 0.9) + (call - 200) * 0.8; else amt = (100 * 0.9) + (200 * 0.8) + ((call - 400) * 0.7); total = amt + 180; } void display() { System.out.println("Name of the customer: " + name); System.out.println("Calls made: " + call); System.out.println("Amount to be paid: " + total); } public static void main() { Telephone obj = new Telephone(); obj.input(); obj.cal(); obj.display(); }} 29 Variable Description Variable Data Type Description prv int pre int call name amt int String double total double Input and store the previous meter reading of consumer Input and store the present meter reading of consumer Compute and display the number of calls made Input, store and display the name of consumer Compute and store the amount based on number of calls placed Compute and display the total amount to be paid Output Enter Customer Name: Trisha Enter previous reading: 190 Enter present reading: 300 Name of the customer: Trisha Calls made: 110 Amount to be paid: 189.0 30 Q13. Bank charges interest for the vehicle loan as given below: Number of years Rate of interest Up to 5 years 15% More than 5 and up to 10 years 12% Above 10 years 10% Write a program to model a class with the specifications given below: Class name: Loan Data Members Purpose int time Time for which loan is sanctioned double principal Amount sanctioned double rate Rate of interest double interest To store the interest double amt Amount to pay after given time Member Methods Purpose void getdata() To accept principal and time void calculate() To find interest and amount. Interest = (Principal*Rate*Time)/100 Amount = Principal + Interest void display() To display interest and amount 31 A13. import java.util.*; public class Loan { int time; double principal; double rate; double interest; double amt; public void getdata() { Scanner in = new Scanner(System.in); System.out.print("Enter principal: "); principal = in.nextInt(); System.out.print("Enter time: "); time = in.nextInt(); } public void calculate() { if (time <= 5) rate = 15.0; else if (time <= 10) rate = 12.0; else rate = 10.0; interest = (principal * rate * time) / 100.0; amt = principal + interest; } public void display() { System.out.println("Interest = " + interest); System.out.println("Amount Payable = " + amt); } 32 public static void main() { Loan obj = new Loan(); obj.getdata(); obj.calculate(); obj.display(); }} Variable Description Variable Data Type Description time principal rate interest amt int double double double double Input and store time period of loan Input and store principal of loan Store interest rate of loan Compute and display interest earned by loan Compute and display total amount payable at end of time period of loan Output Enter principal: 10000 Enter time: 2 Interest = 3000.0 Amount Payable = 13000.0 33 Q14. Define a class Bill that calculates the telephone bill of a consumer with the following description: Data Members Purpose int bno bill number String name name of consumer int call no. of calls consumed in a month double amt bill amount to be paid by the person Member Methods Purpose Bill() constructor to initialize data members with default initial value Bill(...) parameterised constructor to accept bill no., name and no. of calls consumed Calculate() to calculate the monthly telephone bill for a consumer as per the table given below Display() to display the details Units consumed Rate First 100 calls ₹0.60 / call Next 100 calls ₹0.80 / call Next 100 calls ₹1.20 / call 34 Units consumed Above 300 calls Rate ₹1.50 / call Fixed monthly rental applicable to all consumers: ₹125 Create an object in the main() method and invoke the above functions to perform the desired task. A14. import java.util.*; public class Bill { int b_no; String name; int call; double amt; Bill() { b_no = 0; name = ""; call = 0; amt = 0.0; } Bill(int b, String n, int c) { b_no = b; name = n; call = c; } void calculate() { double charge; if (call <= 100) 35 charge = call * 0.6; else if (call <= 200) charge = 60 + ((call - 100) * 0.8); else if (call <= 300) charge = 60 + 80 + ((call - 200) * 1.2); else charge = 60 + 80 + 120 + ((call - 300) * 1.5); amt = charge + 125; } void display() { System.out.println("Bill No: " + b_no); System.out.println("Name: " + name); System.out.println("Calls: " + call); System.out.println("Amount Payable: " + amt); } public static void main() { Scanner in = new Scanner(System.in); System.out.print("Enter Name: "); String custName = in.nextLine(); System.out.print("Enter Bill Number: "); int billNum = in.nextInt(); System.out.print("Enter Calls: "); int numCalls = in.nextInt(); Bill obj = new Bill(billNum, custName, numCalls); obj.calculate(); obj.display(); }} 36 Variable Description Variable Data Type Description b_no name call int String int charge double amt double b n c int String int Store and display bill number Store and display name of customer Store and display number of calls placed by customer Compute and store charge based on the number of calls Compute and display the final amount to be paid by customer Accept the bill number from billNum Accept the name of customer from custName Accept the number of calls placed by customer from numCalls Input and store customer name Input and store bill number Input and store number of calls placed custName String billNum int numCalls int Output Enter Name: Trisha Enter Bill Number: 123 Enter Calls: 165 Bill No: 123 Name: Trisha Calls: 165 Amount Payable: 237.0 37 Q15. Write a program by using a class with the following specifications: Class name — Factorial Data members — private int n Member functions: 1. void input() — to input a number 2. void fact() — to find and print the factorial of the number Use a main function to create an object and call member methods of the class. A15. import java.util.*; public class Factorial { int n; void input() { Scanner in = new Scanner(System.in); System.out.print("Enter the number: "); n = in.nextInt(); } void fact() { int f = 1; for (int i = 1; i <= n; i++) f *= i; System.out.println("Factorial of " + n + " = " + f); } public static void main() { Factorial obj = new Factorial(); obj.input(); obj.fact(); } } 38 Variable Description Variable Data Type Description n f i int int int Inputs and stores the value of n Computes and displays the factorial of n Counter Output Enter the number: 10 Factorial of 10 = 3628800 Enter the number: 4 Factorial of 4 = 24 39 Bibliography www.google.com www.pinterest.com www.bluej.org www.knowledgeboat.com www.microsoft.com www.youtube.com 40