CSA09 Programming in Java Medium
CSA09 Programming in Java Medium
1. Write a program to count all the prime and composite numbers entered by the user.
Sample Input:
Enter the numbers
4
54
29
71
7
59
98
23
Sample Output:
Composite number:3
Prime number:5
Test cases:
1. 33, 41, 52, 61,73,90
2. TEN, FIFTY, SIXTY-ONE, SEVENTY-SEVEN, NINE
3. 45, 87, 09, 5.0 ,2.3, 0.4
4. -54, -76, -97, -23, -33, -98
5. 45, 73, 00, 50, 67, 44
import java.util.Scanner;
public class Main {
2. Find the Mth maximum number and Nth minimum number in an array and then find the
sum of it and difference of it.
Sample Input:
Array of elements = {14, 16, 87, 36, 25, 89, 34}
M=1
N=3
Sample Output:
1stMaximum Number = 89
3rdMinimum Number = 25
Sum = 114
SAVEETHA SCHOOL OF ENGINEERING
SAVEETHA INSTITUTE OF MEDICAL AND TECHNICAL SCIENCES
INSTITUTE OF PLACEMENT AND TRAINING
CSA09 –JAVA PROGRAMMING
Difference = 64
Test cases:
1. {16, 16, 16 16, 16}, M = 0, N = 1
2. {0, 0, 0, 0}, M = 1, N = 2
3. {-12, -78, -35, -42, -85}, M = 3 , N = 3
4. {15, 19, 34, 56, 12}, M = 6 , N = 3
5. {85, 45, 65, 75, 95}, M = 5 , N = 7
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("enter the size of the array:- ");
int size = input.nextInt();
int[] arr = new int[size];
System.out.println("enter the values in the array:- ");
for(int i=0;i<size;i++){
arr[i] = input.nextInt();
}
Arrays.sort(arr);
System.out.print("enter the Mth max number:- ");
int m = input.nextInt();
System.out.print("enter the Nth min number:- ");
int n = input.nextInt();
int max=0,min=0;
if(m==0)
System.out.println("please enter the valid input");
else{
max = arr[arr.length-m];
min = arr[n-1];
System.out.println("the max is "+max);
SAVEETHA SCHOOL OF ENGINEERING
SAVEETHA INSTITUTE OF MEDICAL AND TECHNICAL SCIENCES
INSTITUTE OF PLACEMENT AND TRAINING
CSA09 –JAVA PROGRAMMING
3. Write a program to print the total amount available in the ATM machine with the
conditions applied.
Total denominations are 2000, 500, 200, 100, get the denomination priority from the user
and the total number of notes from the user to display the total available balance to the
user
Sample Input:
Enter the 1st Denomination: 500
Enter the 1st Denomination number of notes: 4
Enter the 2nd Denomination: 100
Enter the 2nd Denomination number of notes: 20
Enter the 3rd Denomination: 200
Enter the 3rd Denomination number of notes: 32
Enter the 4th Denomination: 2000
Enter the 4th Denomination number of notes: 1
Sample Output:
Total Available Balance in ATM: 12400
Test Cases:
3 Hidden Test cases (Think Accordingly based on Denominations)
import java.util.*;
class atm
{
public static void main(String[] args)
{
int balance=0;
Scanner sc = new Scanner(System.in);
for( int i=0;i<=4;i++)
SAVEETHA SCHOOL OF ENGINEERING
SAVEETHA INSTITUTE OF MEDICAL AND TECHNICAL SCIENCES
INSTITUTE OF PLACEMENT AND TRAINING
CSA09 –JAVA PROGRAMMING
{
System.out.println("denomination:" );
int d=sc.nextInt();
System.out.println("enter the number of notes");
int n=sc.nextInt();
balance += d*n;
}
System.out.println("the balance is" +balance);
}
}
import java.util.*;
public class Pallindrome {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("enter the string:- ");
String value = input.next();
try{
SAVEETHA SCHOOL OF ENGINEERING
SAVEETHA INSTITUTE OF MEDICAL AND TECHNICAL SCIENCES
INSTITUTE OF PLACEMENT AND TRAINING
CSA09 –JAVA PROGRAMMING
5. Write a program to convert Decimal number equivalent to Binary number and octal
numbers?
Sample Input:
Decimal Number: 15
Sample Output:
Binary Number = 1111
SAVEETHA SCHOOL OF ENGINEERING
SAVEETHA INSTITUTE OF MEDICAL AND TECHNICAL SCIENCES
INSTITUTE OF PLACEMENT AND TRAINING
CSA09 –JAVA PROGRAMMING
Octal = 17
Test cases:
1. 111
2. 15.2
3. 0
4. B12
5. 1A.2
import java.util.Scanner;
6. In an organization they decide to give bonus to all the employees on New Year. A 5%
bonus on salary is given to the grade A workers and 10% bonus on salary to the grade B
workers. Write a program to enter the salary and grade of the employee. If the salary of
the employee is less than $10,000 then the employee gets an extra 2% bonus on salary
Calculate the bonus that has to be given to the employee and print the salary that the
employee will get.
Sample Input & Output:
Enter the grade of the employee: B
Enter the employee salary: 50000
Salary=50000
Bonus=5000.0
Total to be paid:55000.0
Test cases:
1. Enter the grade of the employee: A
Enter the employee salary: 8000
2. Enter the grade of the employee: C
Enter the employee salary: 60000
3. Enter the grade of the employee: B
Enter the employee salary: 0
SAVEETHA SCHOOL OF ENGINEERING
SAVEETHA INSTITUTE OF MEDICAL AND TECHNICAL SCIENCES
INSTITUTE OF PLACEMENT AND TRAINING
CSA09 –JAVA PROGRAMMING
import java.util.*;
class employee
{
public static void main(String[] args)
{
double bonus,bonus1,paid;
Scanner sc = new Scanner(System.in);
System.out.println("enter the salary");
double sal= sc.nextDouble();
System.out.println("enter the employee grade:");
char gr= sc.next().charAt(0);
if (gr=='A'&&sal>10000)
{
bonus=(sal*5)/100+sal;
paid=sal+bonus;
System.out.println("the bonus of grade A :" +bonus);
System.out.println("the paid amount of grade A:" +paid);
}
else if (gr=='B' && sal>10000)
{
bonus=sal*(0.1)+sal;
paid=sal+bonus;
System.out.println("the bonus of grade B :" +bonus);
System.out.println("the paid amount of grade B:" +paid);
}
else if (gr=='A' && sal<=10000)
{
bonus=sal*(0.05);
bonus1=sal*(0.02);
paid=sal+bonus+bonus1;
System.out.println("the bonus of grade A :" +bonus);
System.out.println("the paid amount of grade A with extra 2%:" +paid);
}
else if (gr=='B' && sal<=10000)
{
bonus=sal*(0.1);
SAVEETHA SCHOOL OF ENGINEERING
SAVEETHA INSTITUTE OF MEDICAL AND TECHNICAL SCIENCES
INSTITUTE OF PLACEMENT AND TRAINING
CSA09 –JAVA PROGRAMMING
bonus1=sal*(0.02);
paid=sal+bonus+bonus1;
System.out.println("the bonus of grade B :" +bonus);
System.out.println("the paid amount of grade B with extra 2%:" +paid);
}
else
System.out.println("there is no bonus");
System.out.println("the salary is" +sal);
}
}
7. Write a program to print the first n perfect numbers. (Hint Perfect number means a
positive integer that is equal to the sum of its proper divisors)
Sample Input:
N=3
Sample Output:
First 3 perfect numbers are: 6 , 28 , 496
Test Cases:
1. N = 0
2. N = 5
3. N = -2
4. N = -5
5. N = 0.2
import java.util.Scanner;
public class PerfectNumber
{
public static void main(String args[])
{
long n, sum=0;
Scanner sc=new Scanner(System.in);
System.out.print("Enter the number: ");
n=sc.nextLong();
int i=1;
//executes until the condition becomes false
while(i <= n/2)
SAVEETHA SCHOOL OF ENGINEERING
SAVEETHA INSTITUTE OF MEDICAL AND TECHNICAL SCIENCES
INSTITUTE OF PLACEMENT AND TRAINING
CSA09 –JAVA PROGRAMMING
{
if(n % i == 0)
{
//calculates the sum of factors
sum = sum + i;
} //end of if
//after each iteration, increments the value of variable i by 1
i++;
} //end of while
//compares sum with the number
if(sum==n)
{
//prints if sum and n are equal
System.out.println(n+" is a perfect number.");
} //end of if
else
//prints if sum and n are not equal
System.out.println(n+" is not a perfect number.");
}
}
8. Write a program to enter the marks of a student in four subjects. Then calculate the total
and aggregate, display the grade obtained by the student. If the student scores an
aggregate greater than 75%, then the grade is Distinction. If aggregate is 60>= and <75,
then the grade is First Division. If aggregate is 50 >= and <60, then the grade is Second
Division. If aggregate is 40>= and <50, then the grade is Third Division. Else the grade is
Fail.
import java.util.*;
public class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int n;
System.out.println("Enter the total subjects ");
n=sc.nextInt();
int arr[] = new int[n];
for(int i=0;i<n;i++)
{
System.out.println("Enter subject mark:");
arr[i]=sc.nextInt();
int total=0;
for(int i=0;i<n;i++)
{
SAVEETHA SCHOOL OF ENGINEERING
SAVEETHA INSTITUTE OF MEDICAL AND TECHNICAL SCIENCES
INSTITUTE OF PLACEMENT AND TRAINING
CSA09 –JAVA PROGRAMMING
total=total+arr[i];
}
System.out.println("The total marks obtained is "+total);
float percentage;
percentage = (total / (float)n);
System.out.println( "Total Percentage : " + percentage + "%");
}
}
9. Write a program to read the numbers until -1 is encountered. Find the average of positive
numbers and negative numbers entered by user.
Sample Input:
Enter -1 to exit…
Enter the number: 7
Enter the number: -2
Enter the number: 9
Enter the number: -8
Enter the number: -6
Enter the number: -4
Enter the number: 10
Enter the number: -1
Sample Output:
The average of negative numbers is: -5.0
The average of positive numbers is : 8.66666667
Test cases:
1. -1,43, -87, -29, 1, -9
2. 73, 7-6,2,10,28,-1
3. -5, -9, -46,2,5,0
4. 9, 11, -5, 6, 0,-1
5. -1,-1,-1,-1,-1
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
SAVEETHA SCHOOL OF ENGINEERING
SAVEETHA INSTITUTE OF MEDICAL AND TECHNICAL SCIENCES
INSTITUTE OF PLACEMENT AND TRAINING
CSA09 –JAVA PROGRAMMING
10. Write a program to read a character until a * is encountered. Also count the number of
uppercase, lowercase, and numbers entered by the users.
Sample Input:
Enter * to exit…
Enter any character: W
Enter any character: d
Enter any character: A
Enter any character: G
Enter any character: g
Enter any character: H
Enter any character: *
SAVEETHA SCHOOL OF ENGINEERING
SAVEETHA INSTITUTE OF MEDICAL AND TECHNICAL SCIENCES
INSTITUTE OF PLACEMENT AND TRAINING
CSA09 –JAVA PROGRAMMING
Sample Output:
Total count of lower case:2
Total count of upper case:4
Total count of numbers =0
Test cases:
1. 1,7,6,9,5
2. S, Q, l, K,7, j, M
3. M, j, L, &, @, G
4. D, K, I, 6, L, *
5. *, K, A, e, 1, 8, %, *
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
char ch = '0';
int up_c =0;
int lo_c = 0,num_c=0;
while (ch != '*') {
System.out.print("enter the character:- ");
ch = input.next().charAt(0);
if(Character.isUpperCase(ch)){
up_c++;
}
else if(Character.isLowerCase(ch)){
lo_c++;
}
else if(Character.isDigit(ch)){
num_c++;
SAVEETHA SCHOOL OF ENGINEERING
SAVEETHA INSTITUTE OF MEDICAL AND TECHNICAL SCIENCES
INSTITUTE OF PLACEMENT AND TRAINING
CSA09 –JAVA PROGRAMMING
}
}
System.out.println("the no.of.uppercase is "+up_c);
System.out.println("the no.of.lowercase is "+(lo_c));
System.out.println("the no.of.numbers is "+num_c);
}
}
11. Write a program to calculate the factorial of number using recursive function.
Sample Input & Output:
Enter the value of n: 6
Sample Input & Output:
The factorial of 6 is: 720
Test cases:
1. N = 0
2. N = -5
3. N = 1
4. N = M
5. N = %
import java.util.Scanner;
class Main{
public static void main(String args[]){
//Scanner object for capturing the user input
Scanner = new Scanner(System.in);
System.out.println("Enter the number:");
//Stored the entered value in variable
int num = scanner.nextInt();
//Called the user defined function fact
int factorial = fact(num);
SAVEETHA SCHOOL OF ENGINEERING
SAVEETHA INSTITUTE OF MEDICAL AND TECHNICAL SCIENCES
INSTITUTE OF PLACEMENT AND TRAINING
CSA09 –JAVA PROGRAMMING
Test cases:
1. N = 0
2. N = -5
3. N = 1
4. N = M
5. N = %
Test cases:
1. 211
2. 11011
3. 22122
4. 111011.011
5. 1010.0101
import java.util.Scanner;
14. Write a program to find the number of special characters in the given statement
Sample Input:
Given statement: Modi Birthday @ September 17, #&$% is the wishes code for him.
Sample Output:
Number of special Characters: 5
import java.util.*;
class special {
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("enter the string");
String input = sc.nextLine();
int specialCharCount = 0;
specialCharCount++;
}
}
import java.util.Scanner;
int i;
{
SAVEETHA SCHOOL OF ENGINEERING
SAVEETHA INSTITUTE OF MEDICAL AND TECHNICAL SCIENCES
INSTITUTE OF PLACEMENT AND TRAINING
CSA09 –JAVA PROGRAMMING
arr_elements[i] = sc.nextInt();
System.out.println(arr_elements[i]);
if (arr_elements[initial_element] == arr_elements[next_element]) {
arr_size = arr_size - 1;
} else
next_element++;
System.out.println(arr_elements[i]);
16. Bank is a class that provides method to get the rate of interest. But, rate of interest may
differ according to banks. For example, SBI, ICICI and AXIS banks are providing 8.4%,
7.3% and 9.7% rate of interest. Write a Java program for above scenario.
Sample Input SBI, 8.4
SAVEETHA SCHOOL OF ENGINEERING
SAVEETHA INSTITUTE OF MEDICAL AND TECHNICAL SCIENCES
INSTITUTE OF PLACEMENT AND TRAINING
CSA09 –JAVA PROGRAMMING
Sample Output
Test case
1. SBI, 8.3
2. ICICI, 7.3
3. AXIS, 9.7
4. SBI, 8.6
5. AXIX, 7.6
import java.util.*;
import java.io.*;
class Bank
{
float getRateOfIntrest(){return 0;}
}
class sbi extends Bank{
float getRateOfIntrest(){return 8.4f;}
}
class icici extends Bank{
float getRateOfIntrest(){return 7.3f;}
}
class axis extends Bank{
float getRateOfIntrest(){return 9.7f;}
}
class testpolymorphism{
public static void main(String args[])
{
Bank b;
b=new sbi();
System.out.println("SBI rate of intrest"+b.getRateOfIntrest());
b=new icici();
System.out.println("ICICI rate of intrest"+b.getRateOfIntrest());
b=new axis();
System.out.println("AXIS rate of intrest"+b.getRateOfIntrest());
}
}
17. Bring out the situation in which member names of a subclass hide members by the same
name in the super class. How it can be resolved? Write Suitable code in Java and
Implement above scenario with the Parametrized Constructor (accept int type
parameter) of the Super Class can be called from Sub Class Using super () and display
the input values provided.
1. 10, 20
2. -20, -30
3. 0, 0
4. EIGHT FIVE
5. 10.57, 12.58
18. Display Multiplication table for 5 and 10 using various stages of life cycle of the thread
by generating a suitable code in Java.
Sample Input 5, 10
5X1=5
5 X 2 =10
….
10 X 1 =10
10 X 2 = 20
….
Test Cases:
1. 10, 20
2. -10, -30
3. 0, 0
4. SIX, SIX
5. 9.8, 9.6
import java.util.*;
public class Main {
static Scanner input = new Scanner(System.in);
public static void main(String[] args){
int m,n;
System.out.print("enter the starting number:- ");
m = input.nextInt();
System.out.print("enter the ending number:- ");
n = input.nextInt();
if(m<0 && n<0)
fun1(m,n);
else
fun2(m,n);
}
public static void fun1(int m,int n){
int i;
for(i=-1;i>=n;i--){
System.out.println(i+"*"+m+"="+(i*m));
}
}
SAVEETHA SCHOOL OF ENGINEERING
SAVEETHA INSTITUTE OF MEDICAL AND TECHNICAL SCIENCES
INSTITUTE OF PLACEMENT AND TRAINING
CSA09 –JAVA PROGRAMMING
19. Using the concepts of thread with implementing Runnable interface in Java to generate
Fibonacci series.
Sample Input : 5
Sample Output : 0 1 1 2 3 …..
Test Cases
1. 7
2. -10
3. 0
4. EIGHT FIVE
5. 12.65
20. Generate a Java code to find the sum of N numbers using array and throw
ArrayIndexOutOfBoundsException when the loop variable beyond the size N.
Sample Input : 5
12345
Sample Output : 15
Test Cases
1. 4, 10
2. -10
3. 0
4. EIGHT SEVEN
5. 12.68
21. Using the concepts of thread with implementing Runnable interface in Java to find
whether a given number is prime or not.
Sample Input : 5
Sample Output : 5 is Prime
Sample Output : 15
Test Cases
1. 4
2. -10
3. 0
4. EIGHT SEVEN
5. 11.48
SAVEETHA SCHOOL OF ENGINEERING
SAVEETHA INSTITUTE OF MEDICAL AND TECHNICAL SCIENCES
INSTITUTE OF PLACEMENT AND TRAINING
CSA09 –JAVA PROGRAMMING
23. Generate a Java Code to Write and Read the string “Computer Science and Engineering”
using FileWriter and FileReader Class.
24. Create a java program to construct the volume of Box using default constructor method.
25. Accept the string “Welcome to Saveetha university” from the user and perform the
following operations by writing a suitable Java code.
i) Replace any word in the given String
ii) Find the length
iii) Uppercase Conversion
26. Create a HashTable to maintain a bank detail which includes Account number and
Customer name. Let Account number be the key in the HashTable. Write a Java program
to implement the following operations in the HashTable
i) Add 3 records
ii) Display the size of HashTable
iii) Clear the HashTable
27. Create a employee record using map interface and do the following operations.
i. Add object iii. Remove specified object
ii. isEmpty or not iv. Clear
28. Create a simple generics class with type parameters for sorting values of different types.
29. Develop a Java code to insert the following elements, using ListIterator to append +
symbol in each element and print them in reverse order. {C, A, E, B, D, F}.
30. Generate a Java code to perform simple arithmetic operations and to throw Arithmetic
Exception for Division-by-Zero.
31. Write a Java program to create three threads in parallel and display the natural numbers in
orders using sleep() method.
32. If n = 8, then array ‘a’ will have 7 elements in the range from 1 to 8. For example {1,
4, 5, 3, 7, 8, 6}. One number will be missing in ‘a’ (2 in this case). Write a source code to
find out that missing number
33. Create a class with a method that prints "This is parent class" and its subclass with another
method that prints "This is child class". Now, create an object for each of the class and call
1 - method of parent class by object of parent class
2 - method of child class by object of child class
3 - method of parent class by object of child class
SAVEETHA SCHOOL OF ENGINEERING
SAVEETHA INSTITUTE OF MEDICAL AND TECHNICAL SCIENCES
INSTITUTE OF PLACEMENT AND TRAINING
CSA09 –JAVA PROGRAMMING
34. Write a Java program to create a class Student and create constructor which assigns the values for
the student details such as student name, register number, and five subject marks. Calculate the
total and average of five subject marks and display the marks and average.
35. Generate a code to Count the Number of Words, Character and Lines from the File using
Stream I/O in Java.
36. Generate a code to non-negative integer’s num1 and num2 represented as strings; return
the product of num1 and num2, also represented as a string.
37. Implement pow(x, n), which calculates x raised to the power n
Input: x = 2.00000, n = 10
Output: 1024.00000
38. Given an integer array nums, find the subarray with the largest sum, and return its sum.
Input: nums = [-2,1,-3,4,-1,2,1,-5,4]
Output: 6
Explanation: The subarray [4,-1, 2, 1] has the largest sum 6.
39. There is an exam room with n seats in a single row labeled from 0 to n - 1.When a student
enters the room, they must sit in the seat that maximizes the distance to the closest person.
If there are multiple such seats, they sit in the seat with the lowest number. If no one is in
the room, then the student sits at seat number 0.Design a class that simulates the
mentioned exam room. Implement the ExamRoom class: ExamRoom (int n) Initializes
the object of the exam room with the number of the seats n. int seat () Returns the label of
the seat at which the next student will set. Void leave (int p) indicates that the student
sitting at seat p will leave the room. It is guaranteed that there will be a student sitting at
seat p.
Input["ExamRoom", "seat", "seat", "seat", "seat", "leave", "seat"]
[[10], [], [], [], [], [4], []]
Output
[null, 0, 9, 4, 2, null, 5]
40. You have n tiles, where each tile has one letter tiles[i] printed on it. Return the number of
possible non-empty sequences of letters you can make using the letters printed on those
tiles.
Input: tiles = "AAB"
Output: 8
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA",
"BAA".
SAVEETHA SCHOOL OF ENGINEERING
SAVEETHA INSTITUTE OF MEDICAL AND TECHNICAL SCIENCES
INSTITUTE OF PLACEMENT AND TRAINING
CSA09 –JAVA PROGRAMMING