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

Assignment Pca1

The document is an assignment submission for a Java programming class. It includes 12 programming problems and their solutions in Java code. The problems cover topics like profit/loss calculation, prime number checking, digit sum calculation, palindrome checking, perfect number checking, Fibonacci series, calculator operations using switch case, decimal to octal conversion, and linear/binary search of an array. The assignment includes the student's name, course details, teacher name, and output for each problem.

Uploaded by

Ritesh Verma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
118 views

Assignment Pca1

The document is an assignment submission for a Java programming class. It includes 12 programming problems and their solutions in Java code. The problems cover topics like profit/loss calculation, prime number checking, digit sum calculation, palindrome checking, perfect number checking, Fibonacci series, calculator operations using switch case, decimal to octal conversion, and linear/binary search of an array. The assignment includes the student's name, course details, teacher name, and output for each problem.

Uploaded by

Ritesh Verma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 42

ASSIGNMENT OF JAVA

NAME: Sunny Kumar Sah


ROLL: 31401219002
SEMESTER: 4th 2nd Year
SUBJECT: Programming with Java (BCAN-492)
COLLEGE: Techno India College of Technology
UNIVERSITY: MAKAUT
ASSIGMENT: PCA1 (Java practical Lab)
TEACHER: Soumen Sir
1. Write a program to check profit loss using if-else.
Program:-
package profit_loss;
import java.util.Scanner;
public class Profit_Loss {
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.


Program:-
package prime;
public class Prime {
int n, i, p=1;
Prime()
{
//default constructor
}
Prime(int m)
{
n=m;
}
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");
}
}
DEMO CLASS:-
package prime;
import java.util.Scanner;
public class Demo {
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.


Program:-
package sumofdigit;
import java.util.Scanner;
public class SumofDigit {
int n=0,num,sum=0;
public SumofDigit(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);
}
public static void main(String[] args) {
int digit;
Scanner sc= new Scanner(System.in);
System.out.println("Enter the Number: ");
digit=sc.nextInt();
SumofDigit obj= new SumofDigit(digit);
obj.isSum();
}
}
Output:-

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


Program:-
package pallindrome;
public class Pallindrome {
int n ,rem, rev=0,temp;
public Pallindrome(int x){
n=x;
}
public 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");
}
}

DEMO CLASS:-
package pallindrome;
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
int n;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
n=sc.nextInt();
Pallindrome obj=new Pallindrome(n);
obj.check();
}
}
Output:-

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


Program:-
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");
}
}

DEMO CLASS:-
import java.util.Scanner;
public class Demo {
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
Program:-
package pallindrome;
public class Perfect_01 {
public static void main(String []arrgs){
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.


Program:-
public class Magic {
int n, remainder = 1, number, sum = 0;
public Magic(int x){
n=x;
}
void check(){
number = n;
while (number > 9)
{
while (number > 0)
{
remainder = number % 10;
sum = sum + remainder;
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.");
}
}

}
DEMO CLASS:-
import java.util.Scanner;
public class Demo {
public static void main(String args[])
{
int n;
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number you want to check: ");
n = sc.nextInt();
Magic obj=new Magic(n);
obj.check();
}

}
Output:-

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


Program:-
package fibonacci;
public class Fibonacci {
public static void main(String[] args) {
int i,n1=0,n2=1,n3;
System.out.println(n1+ " " +n2);
for(i=0;i<20;i++){
n3=n1+n2;
System.out.println(" "+n3);
n1=n2;
n2=n3;
}
}
}

Output:-
9. Write a program to create a class Calculator which performs addition,
subtraction, multiplication, and division using switch case.
Program:-
import java.util.Scanner;
public class Calculator {
public static void main(String []arrgs){
Scanner scanner = new Scanner(System.in);
int num;
System.out.println("1. Addition");
System.out.println("2. Substraction");
System.out.println("3. Multiplication");
System.out.println("4. Division");
System.out.println("Choose the operation you want to perform.");
num = scanner.nextInt();
switch(num){
case 1 -> {
int x,y;
System.out.println("Enter number1. ");
x = scanner.nextInt();
System.out.println("Enter number2. ");
y = scanner.nextInt();
int z = x + y;
System.out.println("Sum of two number is "+z);
}
case 2 -> {
int x,y;
System.out.println("Enter number1. ");
x = scanner.nextInt();
System.out.println("Enter number2. ");
y = scanner.nextInt();
int z = x - y;
System.out.println("Sum of two number is "+z);
}
case 3 -> {
int x,y;
System.out.println("Enter number1. ");
x = scanner.nextInt();
System.out.println("Enter number2. ");
y = scanner.nextInt();
int z = x * y;
System.out.println("Sum of two number is "+z);
}
case 4 -> {
int x,y;
System.out.println("Enter number1. ");
x = scanner.nextInt();
System.out.println("Enter number2. ");
y = scanner.nextInt();
int z = x / y;
System.out.println("Sum of two number is "+z);
}
default -> {
System.out.println("you have entered invalid choice");
}
}
}
}
Output:-

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


Program:-
import java.util.Scanner;
public class Decimal_Octal {
public static void main(String []arrgs){
int rem, dec;
String oct = "";
char a[] = {'0','1','2','3','4','5','6','7'};
System.out.println("Enter the Decimal Number");
Scanner scanner = new Scanner(System.in);
dec = scanner.nextInt();
while(dec>0){
rem = dec % 8;
oct = a[rem]+oct;
dec = dec/8;
}
System.out.println("The Octal number is "+oct);
}
}

Output:-

11. Write a program to find whether a number is present in an array by linear


search.
Program:-
import java.util.Scanner;
public class Linear_Search {

public static void main(String []arrgs){


int array[],key,len;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the length of Array");
len = scanner.nextInt();
array = new int[len];
System.out.println("Enter Array elements");
for(int i=0;i<len;i++){
array[i] = scanner.nextInt();
}
System.out.println("Enter number to Perform linear search");
key = scanner.nextInt();
for(int i=0;i<len;i++){
if(array[i]==key)
System.out.println(key+ " is present at location " +(i+1));

if (i == array.length)
System.out.println(key + " doesn't exist in array.");
}
}
}
Output:-

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


binary search.
Program:-
import java.util.Scanner;
public class Binary_Search {
public static void binarySearch(int arr[], int first, int last, int key){
int mid = (first + last)/2;
while( first <= last ){
if ( arr[mid] < key ){
first = mid + 1;
}else if ( arr[mid] == key ){
System.out.println(key+ " Element is found at index: " +(mid+1));
break;
}else{
last = mid - 1;
}
mid = (first + last)/2;
}
if ( first > last ){
System.out.println("Element is not found!");
}
}
public static void main(String args[]){
int arr[];
int key,len;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter length of Array");
len = scanner.nextInt();
arr = new int[len];
System.out.println("Enter the array elements");
for(int i=0;i<len;i++){
arr[i] = scanner.nextInt();
}
int last=arr.length-1;
System.out.println("Enter the number to be searched");
key = scanner.nextInt();
binarySearch(arr,0,last,key);
}
}

Output:-
13. Write a program to show Hierarchical inheritance in java by staff details
of company being given in the problem.
Program:-
Staff class:-

import java.util.Scanner;

public class Staff {


//code and name
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:-
import java.util.Scanner;
public class Teacher extends Staff{
//Subject and Department
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:-
import java.util.Scanner;
public class Typist extends Staff{
//speed
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:-
import java.util.Scanner;
public class Officer extends Staff{
//grade
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:-
import java.util.Scanner;
public class Regular extends Typist{
//salary
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:-
import java.util.Scanner;
public class Casual {
//daily_wages
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);
}
}
Demo class:-
import static java.lang.System.exit;
import java.util.Scanner;
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 -> {
exit(0);
}
default -> {
System.out.println("Invalid Choice");
}
}

}
}
Output:-

14. Write a program to implement stack using array.


Program:-
import java.io.BufferedReader;
import java.io.InputStreamReader;
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]);
}
}

DEMO CLASS:-
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Demo {
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