You are on page 1of 59

JAVA LAB ASSIGNMENT

Name: Ritesh Verma


University Roll Number: 31401219005
Stream :BCA
Year: 2nd
Semester: 4th
Subject : Programming Lab with Java
Subject Code: BCAN-492
Assignment : PCA1
College: Techno India College of Technology
University: Maulana Abul Kalam Azad
University of Technology (MAKAUT)
Submitted to: Soumen Santra Sir.
JAVA LAB QUESTIONS

1. Write a program to check profit loss using if-else.


CODE:
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.*;
/**
*
* @author ritesh
*/
public class ProfitLoss {
public static void main(String[] args) {
int cp, sp, profit, loss;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Cost Price of the Product : ");
cp=sc.nextInt();
System.out.println("Enter the Selling Price of the Product : ");
sp=sc.nextInt();
if(cp < sp )
{
profit=sp-cp;
System.out.println("The Profit is :" +profit);
}
else if(sp < cp)
{
loss=cp-sp;
System.out.println("The Loss is :" +loss);
}
else
{
System.out.println("No PROFIT NO LOSS");
}
}
}
OUTPUT:

2. Write a program to find a number is prime or not.

Code:
//prime class
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author ritesh
*/
public class prime {
int n, i, p=1;
prime()
{
//default constructor
}
prime(int m)
{
n=m; //parameterised constructor
}
void checkPrime()
{
for(i=2;i<n;i++){
if(n%i==0) {
p=0;
break;
}
}
if(p==1)
System.out.println(n+ " is Prime Number ");
else
System.out.println(n+ " is not a Prime Number");

}
}
//Driver class

import java.util.Scanner;

/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author ritesh
*/
public class Demoprime {
public static void main(String[] args) {
int n;
Scanner sc=new Scanner(System.in);
System.out.print("Enter a number");
n=sc.nextInt();
prime obj=new prime(n);
obj.checkPrime();
}
}
OUTPUT:

3. Write a program to find sum of digits of a number.


CODE:
//SumOfDigits Class
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author ritesh
*/
public class SumOfDigits1 {
int n=0,num,sum=0;

public SumOfDigits1(int x)
{
num=x;
}

public void isSum()


{
while(num!=0)
{
n=num%10;
sum=sum+n;
num=num/10;
}
System.out.println("The Sum of digits of the number is: " +sum);
}
}

//Driver Class
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.*;
/**
*
* @author ritesh
*/
public class SumOfDigits1Demo {
public static void main(String[] args) {
int digit;
Scanner sc= new Scanner(System.in);
System.out.println("Enter the Number: ");
digit=sc.nextInt();
SumOfDigits1 obj= new SumOfDigits1(digit);
obj.isSum();
}
}
OUTPUT:

4. Write a program to find a number is palindrome or not.


CODE:
//Pallindrome Class
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author ritesh
*/
public class pallin {
int n ,rem, rev=0,temp;

public pallin(int x){


n=x;
}
void check()
{
temp=n;

while(n>0)
{
rem=n%10;
rev=rev*10+rem;
n=n/10;
}
System.out.println(" Given Number is =" +temp);
System.out.println("The Reverse of a Number is =" +rev);
if(rev==temp)
System.out.println("\nThe number is pallindrome");
else
System.out.println("\nThe Number is not a pallindrome");

}
}
//Driver Class
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author ritesh
*/
import java.util.*;
public class Demostration {
public static void main(String[] args) {
int n;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
n=sc.nextInt();
pallin obj=new pallin(n);
obj.check();

}
}
OUTPUT:

5. Write a program to find a number is perfect number or


not.
CODE:
//Perfect Class
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author ritesh
*/
public class Perfect {
int i,num,sum;
public Perfect (int x)
{
num=x;
}
void check(){
for(i=1;i<num;i++)
{
if(num%i==0)
{
sum=sum+i;
}
}
if(sum==num)
System.out.println(num+ "is a perfect number");
else
System.out.println(num+ "is not a perfect number");
}
}
//Driver Class
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author ritesh
*/
import java.util.*;
public class Deemoo {
public static void main(String[] args) {
int num;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number ");
num=sc.nextInt();
Perfect obj= new Perfect(num);
obj.check();
}
}
OUTPUT:

6. Write a program to print all perfect nos. in the range 1-


100.
CODE:
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author ritesh
*/
public class perfectno {
public static void main(String[] args)
{
int i,sum=1;
System.out.println("Perfect nos from 1 to 100 are ,");
for(int j=2;j<=100;j++)
{

sum=1;
for(i=2;i<j;i++)
{
if(j%i==0)
sum=sum+i;
}

if(j==sum)
System.out.print(j+",");
}

}
}
OUTPUT:

7. Write a program to find a number is magic number or


not.
CODE:
//Magic Class
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author ritesh
*/
public class magic {
int n, remainder = 1, number, sum = 0;

public magic(int x){


n=x;
}
void check(){
number = n;
//outer while loop
while (number > 9) //while(number > 0 || sum > 9)
{
//inner while loop
while (number > 0)
{
//determines the remainder
remainder = number % 10;
sum = sum + remainder;
//divides the number by 10 and removes the last digit of the number
number = number / 10;
}
number = sum;
sum = 0;
}
if (number == 1)
{
System.out.println(n+ " is a magic number.");
}
else
{
System.out.println(n+ " is not a magic number.");
}
}
}
//Driver Class
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author ritesh
*/
import java.util.*;
public class Demo10 {
public static void main(String args[])
{
int n;
//creating a constructor of the Scanner class
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number you want to check: ");
//reading an integer form the user
n = sc.nextInt();
//assigning the entered number in the variable num
magic obj=new magic(n);
obj.check();
}
}
OUTPUT:

8. Write a program to print Fibonacci series in the range


1-20.
CODE:
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author ritesh
*/
public class Fibonacci {
public static void main(String[] args) {
int count=20, first=0, second=1;
System.out.println("Fibonacci Series upto "+count+ ":");
while(first<=count){
System.out.print(first +",");
int next=first+second;
first=second;
second=next;
}
}
}
OUTPUT:

9. Write a program to create a class Calculator which


performs addition, subtraction, multiplication, and
division using switch case.
CODE:
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author ritesh
*/
import java.util.*;
public class Calculator {
public static void main(String[] args)
{
int m, n, opt, add, sub, mul;
double div;
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number:");
m = sc.nextInt();
System.out.print("Enter second number:");
n = sc.nextInt();
while(true)
{
System.out.println("Enter 1 for addition");
System.out.println("Enter 2 for subtraction");
System.out.println("Enter 3 for multiplication");
System.out.println("Enter 4 for division");
System.out.println("Enter 5 to Exit");
opt = sc.nextInt();
switch(opt)
{
case 1:
add = m + n;
System.out.println("Result:"+add);
break;

case 2:
sub = m - n;
System.out.println("Result:"+sub);
break;

case 3:
mul = m * n;
System.out.println("Result:"+mul);
break;

case 4:
div = (double)m / n;
System.out.println("Result:"+div);
break;

case 5:
System.exit(0);

}
}
}
}
OUTPUT:

10. Write a program to convert a decimal number to octal


number.
CODE:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author ritesh
*/
import java.util.*;
public class DecimaltoOctal {
public static void main(String[] args) {
int num,octl=0,i=1,n;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the Decimal Number :");
num=sc.nextInt();
n=num;
while(n>0)
{
octl+=(n%8)*i;
n/=8;
i*=10;
}
System.out.println("Octal number of "+num+" is "+octl);
}
}
OUTPUT:

11.Write a program to find whether a number is present in


an array by linear search.
CODE:
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.*;
/**
*
* @author ritesh
*/
public class LinearSearch {
public static void main(String[] args) {
int i,num,item,a[];
int flag=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of elements in an array:");
num=sc.nextInt();
a= new int[num];
System.out.println("Enter " +num+ " array elements");
for(i=0;i<num;i++)
a[i]=sc.nextInt();
System.out.println("Enter the numbers to be Searched");
item=sc.nextInt();
for(i=0;i<num;i++)
{
if(a[i]==item)
{
flag=1;
break;
}
}
if(flag==1)
System.out.println(item+" is presented at location " +(i+1) );
else
System.out.println(item+" is not present in an array");
}
}
OUTPUT:

12.Write a program to find whether a number is present in


an array by binary search.
CODE:
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.*;
/**
*
* @author ritesh
*/
public class BinarySearch {
public static void main(String[] args) {
int i,num,item,a[];
Scanner sc= new Scanner(System.in);
System.out.println("Enter the number of elements in an array");
num=sc.nextInt();
a= new int[num];
System.out.println("Enter " +num+ " array elements");
for(i=0;i<num;i++)
a[i]=sc.nextInt();
System.out.println("Enter the numbers to be Searched");
item=sc.nextInt();
int flag=0,mid = 0;
int low=0,high=num-1;
while(low<=high)
{
mid=(low+high)/2;
if(item==a[mid])
{
flag=1;
break;
}
else if(item<a[mid])
high=mid-1;
else
low=mid+1;
}
if(flag==1)
System.out.println(item+ " is found at location :" +(mid+1));
else
System.out.println(item+ " is is not present in an array");
}
}
OUTPUT:

13. Write a program to show Hierarchical inheritance in java


by staff details of company being given in the problem.
CODE:
Staff Class
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.*;
/**
*
* @author ritesh
*/
public class Staff {
protected int code;
protected String name;
Scanner sc = new Scanner(System.in);
public void staffInput(){
System.out.println("Enter the code of Staff: ");
code = sc.nextInt();
System.out.println("Enter the Name of Staff: ");
name = sc.next();
}
public void staffDislay(){
System.out.println("Staff Code: "+code);
System.out.println("Staff Name: "+name);
}
}

Teacher Class
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.*;
/**
*
* @author ritesh
*/
public class Teacher extends Staff{
protected String sub;
protected String dept;
Scanner sc1 = new Scanner(System.in);
public void teacherInput(){
System.out.println("Enter the subject of teacher: ");
sub = sc1.next();
System.out.println("Enter the department of teacher: ");
dept = sc1.next();
}
public void teacherDisplay(){
System.out.println("Subject of teacher: "+sub);
System.out.println("Department of teacher: "+dept);
}
}

Typist Class
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.*;
/**
*
* @author ritesh
*/
public class Typist extends Staff{
protected int speed;
Scanner sc2 = new Scanner(System.in);
public void typistInput(){
System.out.println("Enter the Speed of typist: ");
speed = sc2.nextInt();
}
public void typistDisplay(){
System.out.println("Number of word "+speed+"per minute.");
}

}
Officer Class
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.*;
/**
*
* @author ritesh
*/
public class Officer extends Staff{
protected String grade;
Scanner sc3 = new Scanner(System.in);
public void officerInput(){
System.out.println("Enter the Grade of officer: ");
grade = sc3.next();
}
public void officerDisplay(){
System.out.println("Grade of Officer: "+grade);
}

Regular Class
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.*;
/**
*
* @author ritesh
*/
public class Regular extends Typist{
protected double salary;
Scanner sc4 = new Scanner(System.in);
public void regularInput(){
System.out.println("Enter the salary of Regular Typist: ");
salary = sc4.nextDouble();
}
public void regularDisplay(){
System.out.println("Salary of regular Typist: "+salary);
}

Casual Class
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.*;
/**
*
* @author ritesh
*/
public class Casual extends Typist{
protected double daily_wages;
Scanner sc5 = new Scanner(System.in);
public void casualInput(){
System.out.println("Enter the daily wages of casual typist: ");
daily_wages = sc5.nextDouble();
}
public void casualDisplay(){
System.out.println("Daily Wages of Casual typist: "+daily_wages);
}

}
Driver Class
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.*;
/**
*
* @author ritesh
*/
public class Demo {
public static void main(String []arrgs){
int ch;
Scanner sc = new Scanner(System.in);
System.out.println(" 1.Teacher\n 2.Typist\n 3.Officer");
System.out.println("Enter your Choice: ");
ch = sc.nextInt();

switch(ch){
case 1 : {
Teacher obj = new Teacher();
obj.staffInput();
obj.teacherInput();
System.out.println();
obj.staffDislay();
obj.teacherDisplay();
}
case 2 : {
Typist obj1 = new Typist();
obj1.staffInput();
obj1.typistInput();
System.out.println();
obj1.staffDislay();
obj1.typistDisplay();
System.out.println();
int n;
System.out.println("1.Regular Typist\n2.Casual Typist");
System.out.println("Enter your choice: ");
n = sc.nextInt();
switch(n){
case 1 : {
Regular obj3 = new Regular();
obj3.regularInput();
System.out.println();
obj3.regularDisplay();
}
case 2 : {
Casual obj4 = new Casual();
obj4.casualInput();
System.out.println();
obj4.casualDisplay();
}
}

}
case 3 : {
Officer obj2 = new Officer();
obj2.staffInput();
obj2.officerInput();
System.out.println();
obj2.staffDislay();
obj2.officerDisplay();
}
case 4 : {
System.exit(0);
}
default : {
System.out.println("Invalid Choice");
}
}

}
OUTPUT:
14. Write a program to implement stack using array.
CODE:
//Stack class
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.*;
/**
*
* @author ritesh
*/
public class Stackarray {
final int MAX=5;
private final int info[];
private int top;
public Stackarray()
{
info=new int[MAX];
top=-1;
}
public void push()throws Exception
{
if(top==MAX-1)
System.out.println("The Stack OVERFLOW");
else
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
int x=Integer.parseInt(br.readLine());
top++;
info[top]=x;
}
}
public void pop()
{
if(top==-1)
System.out.println("The Stack is UNDERFLOW");
else
{
int a;
a=info[top];
top--;
System.out.println("The Poped element is "+a);
}
}
public void display()
{
System.out.println("The elements of the Stack is");
for(int i=0;i<=top;i++)
{
System.out.println(""+info[i]);
}
}
}
//Driver Class
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.*;
/**
*
* @author ritesh
*/
public class Driver {
public static void main(String args[]) throws IOException,Exception
{
Stackarray s=new Stackarray();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int i,ch;
while(true)
{
System.out.println("Enter 1 to PUSH,\nEnter 2 to POP,\nEnter 3 to
DISPLAY,\ndefault EXIT");
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
{
s.push();
break;
}
case 2:
{
s.pop();
break;
}
case 3:
{
s.display();
break;
}
default:
{
System.out.println("EXIT");
System.exit(0);
}
}
}
}
}
OUTPUT:

You might also like