NAME – RAGHUVEER BAMNIYA SCHOLAR NO.
- 19U02036
BRANCH -CSE
Q 1.WAP to find the factorial of a number.
import java.util.Scanner;
class Factorl
{
public static void main(String args[])
long n,fact=0;
Scanner input =new Scanner(System.in);
n=input.nextLong();
fact=factCal(n);
System.out.println("fact="+fact);
static long factCal(long x)
{
long fact=1;
for(int i=1;i<=x;i++)
{
fact=fact*i;
}
return fact;
}
Q.2 WAP to print all the prime numbers in an array of n elements by taking
command line arguments.
public class Exercise7 {
public static void main(String[] args) {
int n,count=0;
for (int i=0;i<args.length;i++)
n=Integer.parseInt(args[i]);
for (int j=2;j<n/2;j++)
if (n%j==0)
count=1;
break;
if(count==0&&n%2!=0&&n!=1||n==2)
System.out.print(n+" ");
count=0;
}
Q.3 Write a program to print the sum and average of the even and odd
numbers separately given on command line argument.
public class Exercise4 {
public static void main(String [] args) {
float sum_even=0,n,sum_odd=0;
for (int i=0;i<args.length;i++)
n=Integer.parseInt(args[i]);
if (n%2==0)
sum_even+=n;
if(n%2!=0)
sum_odd+=n;
float avg1=sum_even/args.length;
float avg2=sum_even/args.length;
System.out.println("sum and average of even number is :");
System.out.println(sum_even+"\n"+avg1);
System.out.println("sum and average of odd number is :");
System.out.println(sum_odd+"\n"+avg2);
Q4 WAP to print the following pattern given n as arguments(for n=3)
1
2 2
3 3 3
import java.util.Scanner;
public class pattern
{
public static void main(String[] args)
{
int n;
Scanner s = new Scanner(System.in);
System.out.print("Enter number :");
n = s.nextInt();
for(int i=1;i<=n;i++)
{
For(int j=1;j<=i;j++)
{System.out.print(i+”\t”);
System.out.println(”“);
Q5 WAP to print all prime number in array of n elements.
import java.util.Scanner;
public class PrimeNumbers{
public static void main (String[] args){
System.out.println("Enter number of elements”);
Scanner in = new Scanner (System.in);
//int n=new.nextInt();
int[] array = new int [5];
System.out.println("Enter the elements of the array: ");
for(int i=0; i<5; i++)
{
array[i] = in.nextInt();
}
//loop through the numbers one by one
for(int i=0; i<array.length; i++){
boolean isPrime = true;
//check to see if the numbers are prime
for (int j=2; j<i; j++){
if(i%j==0){
isPrime = false;
break;
}
}
//print the number
if(isPrime)
System.out.println(i + " are the prime numbers in the array ");
}
}
}
Q6 WAP to find following pattern (for n=4).
1 2 3 4
2 4 6
3 6
import java.util.Scanner;
public class pattern
{
public static void main(String[] args)
{
int n;
Scanner s = new Scanner(System.in);
System.out.print("Enter number :");
n = s.nextInt();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n-i+1;j++)
{System.out.print(j*i+"\t");
}
System.out.println("");
}
}
}
Q7 WAP thatshould print square of natural numbers the square should not go beyond 100 & array size
should not store more than 10 elements.
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
int[] temp = { 1, 2, 3, 4, 5, 6, 7, 8, 9,10};
for (int i = 0; i < temp.length; i++) {
System.out.println(temp[i] + "\t" + (int)Math.pow(temp[i], 2));
}
}
OutPut
1 1
2 4
3 9
4 16
5 25
6 36
7 49
8 64
9 81
10 100
Q8 define a clock class that does the following
1. Accept hours, minutes ,and second .
2. Check validity number s.
3. Set the time to AM/PM mode.
Use necessary constructor and methods to do the above task.
public class Clock
{
private NumberDisplay hours;
private NumberDisplay minutes;
private String displayInternationalString;
private String displayUSString;
public Clock()
{
hours = new NumberDisplay(24);
minutes = new NumberDisplay(60);
updateDisplayInternational();
updateDisplayUS();
public Clock(int hour, int minute)
{
hours = new NumberDisplay(24);
minutes = new NumberDisplay(60);
setTime(hour, minute);
}
public void timeTick()
{
minutes.increment();
if(minutes.getValue() == 0) { // it just rolled over!
hours.increment();
}
updateDisplayInternational();
updateDisplayUS();
}
public void setTime(int hour, int minute)
{
hours.setValue(hour);
minutes.setValue(minute);
updateDisplayInternational();
updateDisplayUS();
}
public String getInternationalTime()
{
return displayInternationalString;
}
public String getUSTime()
{
return displayUSString;
}
private void updateDisplayInternational()
private void updateDisplayUS()
{
if(hours.getValue() < 12)
{
displayUSString = hours.getDisplayValue() + ":" +
minutes.getDisplayValue() + " am";
}
else if(hours.getValue() > 12 && hours.getValue() <24)
{
displayUSString = Integer.toString(hours.getValue() - 12) + ":" +
minutes.getDisplayValue() + " pm";
}
else if(hours.getValue() == 0)
{
hours.setValue(12);
displayUSString = hours.getDisplayValue() + ":"+
minutes.getDisplayValue() + " am";
else
{
hours.setValue(12);
displayUSString = hours.getDisplayValue() + ":" +
minutes.getDisplayValue() + " pm";
}
}
}
Q.9 Write a java program which initialization earning of an employee. The
program should calculate the income tax to be paid by the employee as per
the criteria given below:
Slab rate IT rate
Upto Rs. 50,000 Nil
Upto Rs. 60,000 10% on additional amount
Upto Rs. 1,50,000 20% on additional amount
Above Rs. 1,50,000 30% on additional amount
Hint: - Run: - java calculates 1,25,000
Result: - income tax is …………………………….
import java.util.Scanner;
public class Exercise2 {
public static void main(String [] args)
Scanner in=new Scanner(System.in);
float n=in.nextFloat();
if (n<=50000)
System.out.println("the tax to be paid by the employee:"+"Nil");
else if (n<=60000)
System.out.println("the tax to be paid by the employee:"+(n-50000)*o.1);
else if (n<=150000)
System.out.println("the tax to be paid by the employee:"+(n-50000)*0.2);
else
System.out.println("the tax to be paid by the employee:"+(n-50000)*0.3);
input-1,25,000
output- the tax to be paid by the employee : 15,000
Q.10 Design a class for a bank database the database should support the
following operations.
1. Deposit a certain amount into an account,
2. Withdrawing a certain amount from an account,
3. Return a value specifying the amount (i.e. balance) in an amount.
import java.util.Scanner;
class Bank
static Scanner in=new Scanner(System.in);
static double balance=10000;
public static void withdraw()
double withdrawAmount=in.nextDouble();
if (balance>=withdrawAmount)
balance-=withdrawAmount;
else System.out.println("Less Balance..Transaction Failed..");
public static void deposit()
double depositeAmount=in.nextDouble();
balance+=depositeAmount;
static void getamount() {
System.out.println(balance);
public class Main
public static void main (String [] args)
Scanner in=new Scanner(System.in);
int ch1;
int ch2;
do {
System.out.println("select the option");
System.out.println("1.deposite\n2.withdraw\n3.balance\n");
ch1=in.nextInt();
switch(ch1)
case 1:
System.out.println("enter the Amount");
Bank.deposit();
break;
case 2:
System.out.println("withdraw the Amount");
Bank.withdraw();
break;
case 3:
System.out.println("your balance is\n");
Bank.getamount();
break;
System.out.println("do you want to perfom again\n for yes -press 1\nfor no-
press 2");
ch2=in.nextInt();
} while(ch2==1);
Q.11 Create a person inherit two classes from it politician & sportsman provide constructors & calculate
salary & display functions.
class Person {
String name;
String profession;
String contact;
public Person(String name,String pro,String c) {
this.name=name;
profession=pro;
contact=c;
void display() {
System.out.println(name);
System.out.println(profession);
System.out.println(contact);
class Politician extends Person{
double salary;
public Politician(String name,String pro,String c,double salary) {
super(name,pro,c);
this.salary=salary;
void display(){
super.display();
System.out.println(salary);
class Sportsman extends Person{
double salary;
public Sportsman(String name,String pro,String c,double salary) {
super(name,pro,c);
this.salary=salary;
}
void display(){
super.display();
System.out.println(salary);
Q.12 Create a abstract class employee, having its properties & abstract function for calculating net salary
and displaying the information. Drive manager & clerk class from this abstract class & implement the
abstract method net salary and override the display method.
abstract class Employee {
String name;
String designation;
String contact;
String id;
public Employee(String name,String desig,String con,String id) {
this.name=name;
designation=desig;
contact=con;
this.id=id;
}
abstract double netsalary(double s);
void display() {
System.out.println("Name = "+name);
System.out.println("Designation = "+designation);
System.out.println("Contact = "+contact);
System.out.println("Identity = "+id);
class Manager extends Employee{
double salary;
public Manager(String name,String desig,String c,String id,double salary) {
super(name,desig,c,id);
this.salary=salary;
double netsalary(double s) {
salary=s;
return salary;
void display(){
super.display();
System.out.println("Net Salary ="+salary);
}
class Clerk extends Employee{
double salary;
public Clerk(String name,String desig,String c,String id,double salary) {
super(name,desig,c,id);
this.salary=salary;
double netsalary(double s) {
salary=s;
return salary;
void display(){
super.display();
System.out.println("Net Salary ="+salary);