JP Lab Solution

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

Name of Student: Kanishk Chouhan Class: BE-II YEAR

Enrollment No: 0827CS201112 Batch:2020-24


Date of Experiment Date of Submission Submitted on:
Remarks by faculty: Grade:
Signature of student: Signature of Faculty:

1. Write a program to print “Hello World”.


public class Hello
{
public static void main(String[] args) {
System.out.println("Kanishk Chouhan");
System.out.println("0827CS201112");
System.out.println("Hello World");
}
}
Output:

2. Write a program to calculate simple interest and compound interest.


import java.util.*;
public class SimpleAndCompountInterest{
public static void main(String []args){
double p, r, t, s_interest, c_interest;
Scanner scanner = new Scanner (System. in);
System.out.println("Kanishk Chouhan");
System.out.println("0827CS201112");
System.out.println("Enter the value of Principal = ");
p = scanner.nextDouble();
System. out. println("Enter the Annual Rate of Interest = ");
r = scanner.nextDouble();
System. out. println("Enter the Time (years) = ");
t = scanner.nextDouble();
s_interest = (p * r * t)/100;
c_interest = p * Math.pow(1.0+r/100.0,t) - p;
System.out.println("Simple Interest: "+s_interest);
System.out. println("Compound Interest: "+c_interest);
}
}

Output:

Kanishk Chouhan
(0827CS201112)
Name of Student: Kanishk Chouhan Class: BE-II YEAR
Enrollment No: 0827CS201112 Batch:2020-24
Date of Experiment Date of Submission Submitted on:
Remarks by faculty: Grade:
Signature of student: Signature of Faculty:

3. Write a program to find factorial of a given number using recursion.


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class JavaFactorialUsingRecursion{
public static void main(String args[]) throws NumberFormatException, IOException{
System.out.println("Kanishk Chouhan");
System.out.println("0827CS201112");
System.out.println("Enter the number: ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int a = Integer.parseInt(br.readLine());
int result= fact(a);
System.out.println("Factorial of the number is: " + result);
}
static int fact(int b){
if(b <= 1)
return 1;
else
return b * fact(b-1);
}
}
Output:

Kanishk Chouhan
(0827CS201112)
Name of Student: Kanishk Chouhan Class: BE-II YEAR
Enrollment No: 0827CS201112 Batch:2020-24
Date of Experiment Date of Submission Submitted on:
Remarks by faculty: Grade:
Signature of student: Signature of Faculty:

4. Write a program to check weather a given number is palindrome or not.


import java.util.*;
class PalindromeExample {
public static void main(String args[]){
String original, reverse = ""; // Objects of String class
Scanner in = new Scanner(System.in);
System.out.println("Kanishk Chouhan");
System.out.println("0827CS201112");
System.out.println("Enter a string/number to check if it is a palindrome");
original = in.nextLine();
int length = original.length();
for ( int i = length - 1; i >= 0; i-- )
reverse = reverse + original.charAt(i);
if (original.equals(reverse))
System.out.println("Entered string/number is a palindrome.");
else
System.out.println("Entered string/number isn't a palindrome.");
}
}

Output of Java Palindrome Number Example would be

Kanishk Chouhan
(0827CS201112)
Name of Student: Kanishk Chouhan Class: BE-II YEAR
Enrollment No: 0827CS201112 Batch:2020-24
Date of Experiment Date of Submission Submitted on:
Remarks by faculty: Grade:
Signature of student: Signature of Faculty:

5. Write a program to show following Pyramids:

1.*
**
***
****
*****
2. *****
****
***
**
*
1. public class JavaPyramid1{
public static void main(String[] args){
System.out.println("Kanishk Chouhan");
System.out.println("0827CS201112”);
for(int i=1; i<= 5 ;i++){
for(int j=0; j < i; j++){
System.out.print("*");
}
System.out.println("");
}
}
}

Output of the above program would be

Kanishk Chouhan
(0827CS201112)
Name of Student: Kanishk Chouhan Class: BE-II YEAR
Enrollment No: 0827CS201112 Batch:2020-24
Date of Experiment Date of Submission Submitted on:
Remarks by faculty: Grade:
Signature of student: Signature of Faculty:

2. public class JavaPyramid2 {


public static void main(String[] args){
System.out.println("Deepakshi Choudhary");
System.out.println("0827CS201064");
for(int i=5; i>0 ;i--){
for(int j=0; j < i; j++){
System.out.print("*");
}
System.out.println("");
}
}
}

Output of the example would be

6. Write a program to find sum and average of the given number in an array.
public class CalculateArrayAverageExample {
public static void main(String[] args){
System.out.println("Kanishk Chouhan");
System.out.println("0827CS201112");

Kanishk Chouhan
(0827CS201112)
Name of Student: Kanishk Chouhan Class: BE-II YEAR
Enrollment No: 0827CS201112 Batch:2020-24
Date of Experiment Date of Submission Submitted on:
Remarks by faculty: Grade:
Signature of student: Signature of Faculty:

int[] numbers = new int[]{10,20,15,25,16,60,100};


int sum = 0;
for(int i=0; i < numbers.length ; i++)
sum = sum + numbers[i];
double average = sum / numbers.length;
System.out.println("Average value of array elements is : " + average);
System.out.println("Sum of array elements is : " + sum);
}
}
Output of Calculate Average and sum of Array elements using Java Example would be

7. Write a program to show the reverse of the given number.


public class ReverseNumber{
public static void main(String[] args) {
System.out.println("Deepakshi Choudhary");
System.out.println("0827CS201064");
int number = 1234;
int reversedNumber = 0;
int temp = 0;
while(number > 0){
temp = number%10;
reversedNumber = reversedNumber * 10 + temp;
number = number/10;
}
System.out.println("Reversed Number is: " + reversedNumber);
}
}

Kanishk Chouhan
(0827CS201112)
Name of Student: Kanishk Chouhan Class: BE-II YEAR
Enrollment No: 0827CS201112 Batch:2020-24
Date of Experiment Date of Submission Submitted on:
Remarks by faculty: Grade:
Signature of student: Signature of Faculty:

Output of this Number Reverse program would be

8. Write a program to find the largest and smallest number of an array.


public class FindLargestSmallestNumber{
public static void main(String[] args){
System.out.println("Kanishk Chouhan");
System.out.println("0827CS201112");
int numbers[] = new int[]{32,43,53,54,32,65,63,98,43,23};
int smallest = numbers[0];
int largetst = numbers[0];
for(int i=1; i< numbers.length; i++){
if(numbers[i] > largetst)
largetst = numbers[i];
else if (numbers[i] < smallest)
smallest = numbers[i];
}
System.out.println("Largest Number is : " + largest);
System.out.println("Smallest Number is : " + smallest);
}
}

Output of this program would be

9. Write a program to implement Bubble Sort using i/o.


import java.io.*;
class Bubble{
public static void main(String args[]) throws IOException{
System.out.println("Kanishk Chouhan");
System.out.println("0827CS201112");

Kanishk Chouhan
(0827CS201112)
Name of Student: Kanishk Chouhan Class: BE-II YEAR
Enrollment No: 0827CS201112 Batch:2020-24
Date of Experiment Date of Submission Submitted on:
Remarks by faculty: Grade:
Signature of student: Signature of Faculty:

int num;
System.out.println("Program to demonstrate bubble sort");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the number of elements to be entered <10");
num=Integer.parseInt(br.readLine());
System.out.println("Enter the numbers to be sorted");
int arr[]=new int[10];
for(int i=0;i< num;i++){
arr[i]=Integer.parseInt(br.readLine());
}
System.out.println("The number you have entered:");
for(int i=0;i< num;i++){
System.out.println(arr[i]);
}
for(int i=0;i< num;i++){
for(int j=i+1;j< num;j++){
if(arr[i]>arr[j]){
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
System.out.println("The sorted numbers are");
for(int i=0;i< num;i++){
System.out.println(arr[i]);
}
}
}
Output

Kanishk Chouhan
(0827CS201112)
Name of Student: Kanishk Chouhan Class: BE-II YEAR
Enrollment No: 0827CS201112 Batch:2020-24
Date of Experiment Date of Submission Submitted on:
Remarks by faculty: Grade:
Signature of student: Signature of Faculty:

10. Write a program to demonstrate various operations on matrix like-


a)Addition.

b)Subtraction.

c) Transpose.

d)Multiplication.
class Matrix{
public static void main(String args[]){

Kanishk Chouhan
(0827CS201112)
Name of Student: Kanishk Chouhan Class: BE-II YEAR
Enrollment No: 0827CS201112 Batch:2020-24
Date of Experiment Date of Submission Submitted on:
Remarks by faculty: Grade:
Signature of student: Signature of Faculty:

System.out.println("Kanishk Chouhan");
System.out.println("0827CS201112");
int i,j,k;
int mat1 [][]={ {1,2,3}, {4,5,6}, {7,8,9} };
int mat2 [][]={ {10,11,12}, {13,14,15}, {16,17,18} };
System.out.println("\nMatrix A:");
for(i=0;i< 3;i++){
for(j=0;j< 3;j++)
System.out.print("\t"+mat1[i][j]);
System.out.println("");
}
System.out.println("\nMatrix B:");
for(i=0;i< 3;i++){
for(j=0;j< 3;j++)
System.out.print("\t"+mat2[i][j]);
System.out.println("");
}
System.out.println("\nOperation ON Matrices \n1.Addition \n");
int sum [][] = new int [3][3];
for(i=0;i< 3;i++){
for(j=0;j< 3;j++){
sum[i][j] = mat1[i][j] + mat2[i][j];
System.out.print("\t" + sum[i][j]);
}
System.out.println("");
}
System.out.println("2.Subtraction\n");
int diff[][] = new int[3][3];
for(i=0;i< 3;i++){
for(j=0;j< 3;j++){
diff [i][j] = mat1[i][j] - mat2[i][j];
System.out.print("\t"+ diff[i][j]);
}
System.out.println("");

Kanishk Chouhan
(0827CS201112)
Name of Student: Kanishk Chouhan Class: BE-II YEAR
Enrollment No: 0827CS201112 Batch:2020-24
Date of Experiment Date of Submission Submitted on:
Remarks by faculty: Grade:
Signature of student: Signature of Faculty:

}
System.out.println("3. Transpose Of A\n");
int trans[][] = new int[3][3];
for(i=0;i< 3;i++){
for(j=0;j< 3;j++){
trans [i][j] = mat1[j][i];
System.out.print("\t"+ trans[i][j]);
}
System.out.println("");
}
System.out.println("4.Multiplication\n");
int prod[][] = new int[3][3];
for(i=0;i< 3;i++){
for(j=0;j< 3;j++){
prod[i][j] = 0;
for(k=0;k< 3;k++){
prod[i][j] = prod[i][j]+mat1[i][k]*mat2[k][j];
}
System.out.print("\t"+ prod[i][j]);
}
System.out.println("");
}
}
}

Output:
java -cp /tmp/RmpNdZjclM Matrix
Kanishk Chouhan
0827CS201112
Matrix A:
1 2 3
4 5 6
7 8 9

Matrix B:

Kanishk Chouhan
(0827CS201112)
Name of Student: Kanishk Chouhan Class: BE-II YEAR
Enrollment No: 0827CS201112 Batch:2020-24
Date of Experiment Date of Submission Submitted on:
Remarks by faculty: Grade:
Signature of student: Signature of Faculty:

10 11 12
13 14 15
16 17 18

Operation ON Matrices
1.Addition

11 13 15
17 19 21
23 25 27
2.Subtraction

-9 -9 -9
-9 -9 -9
-9 -9 -9
3. Transpose Of A

1 4 7
2 5 8
3 6 9
4.Multiplication

84 90 96
201 216 231
318 342 366

11. Write a program to show concept of polymorphism in java.


abstract class Shape{
protected final static double PI = 22.0/7.0;
protected double length;
public abstract double area();
}
class Square extends Shape{
Square(double side){
length=side;// initialises inherited length
}
public double area(){// overrides area() of Shape
return length*length;// length inherited from Shape
}

Kanishk Chouhan
(0827CS201112)
Name of Student: Kanishk Chouhan Class: BE-II YEAR
Enrollment No: 0827CS201112 Batch:2020-24
Date of Experiment Date of Submission Submitted on:
Remarks by faculty: Grade:
Signature of student: Signature of Faculty:

}
class Circle extends Shape{
Circle(double radius){
length=radius;// initialises inherited length
}
public double area(){// overrides area() of Shape
return PI*length*length;// PI & length inherited from Shape
}
}
class PolyTest{
public static void main(String[] args){
System.out.println("Kanishk Chouhan");
System.out.println("0827CS201112");
Shape sh;// no object instance just variable declaration
Square sq = new Square(10.0);// sq is a Square object reference
Circle circ = new Circle(10.0);// circ is a Circle object reference
sh=sq;// sh dynamically bound to the Square object referenced by sq
System.out.println("Area of Square = " + sh.area());
sh=circ; // sh dynamically bound to the Circle object referenced by circ
System.out.println("Area of circle = " + sh.area());
}
}

Output:

Kanishk Chouhan
(0827CS201112)
Name of Student: Kanishk Chouhan Class: BE-II YEAR
Enrollment No: 0827CS201112 Batch:2020-24
Date of Experiment Date of Submission Submitted on:
Remarks by faculty: Grade:
Signature of student: Signature of Faculty:

12. Write a program to show concept of inheritance in java.


class Animal{
public void move(){
System.out.println("Animals can move");
}
}
class Dog extends Animal{
public void move(){
System.out.println("Dogs can walk and run");
}
}
class TestDog{
public static void main(String args[]){
System.out.println("Kanishk Chouhan");
System.out.println("0827CS201112");
Animal a = new Animal(); // Animal reference and object
Animal b = new Dog(); // Animal reference but Dog object
a.move();// runs the method in Animal class
b.move();//Runs the method in Dog class
}
}

Output:

13. Write a program to show Permutation and Combination.import java.util.Scanner;


public class PermutationCombinationMain{
public static int fact(int num){
int fact=1, i;
for(i=1; i<=num; i++){

Kanishk Chouhan
(0827CS201112)
Name of Student: Kanishk Chouhan Class: BE-II YEAR
Enrollment No: 0827CS201112 Batch:2020-24
Date of Experiment Date of Submission Submitted on:
Remarks by faculty: Grade:
Signature of student: Signature of Faculty:

fact = fact*i;
}
return fact;
}
public static void main(String args[]){
int n, r;
Scanner scanner = new Scanner(System.in);
System.out.println("Kanishk Chouhan");
System.out.println("0827CS201112");
System.out.println("Enter Value of n : ");
n = scanner.nextInt();
System.out.println("Enter Value of r : ");
r = scanner.nextInt();
System.out.println("NCR is " +(fact(n)/(fact(n-r)*fact(r))));
System.out.println("\nNPR is " +(fact(n)/(fact(n-r))));
}
}
Output:

14. Write a program for Binary to Octal Conversion.


import java.util.Scanner;
class Binary_Octal{
Scanner scan;
int num;
void getVal(){
System.out.println("Kanishk Chouhan");
System.out.println("0827CS201112");
System.out.println("Binary to Octal");
scan = new Scanner(System.in);
System.out.println("\nEnter the number :");
num = Integer.parseInt(scan.nextLine(), 2);
}
void convert(){

Kanishk Chouhan
(0827CS201112)
Name of Student: Kanishk Chouhan Class: BE-II YEAR
Enrollment No: 0827CS201112 Batch:2020-24
Date of Experiment Date of Submission Submitted on:
Remarks by faculty: Grade:
Signature of student: Signature of Faculty:

String octal = Integer.toOctalString(num);


System.out.println("Octal Value is : " + octal);
}
}
class Main_Class{
public static void main(String... d){
Binary_Octal obj = new Binary_Octal();
obj.getVal();
obj.convert();
}
}
Output:

15. Write a program to show the difference between Equal function [.equal()] and
Compare Operator [==].
public class Equal {
public static void main(String[] args){
System.out.println("Kanishk Chouhan");
System.out.println("0827CS201112");
String s1 = "HELLO";
String s2 = "HELLO";
String s3 = new String("HELLO");
System.out.println(s1 == s2); // true
System.out.println(s1 == s3); // false
System.out.println(s1.equals(s2)); // true
System.out.println(s1.equals(s3)); // true
}

Kanishk Chouhan
(0827CS201112)
Name of Student: Kanishk Chouhan Class: BE-II YEAR
Enrollment No: 0827CS201112 Batch:2020-24
Date of Experiment Date of Submission Submitted on:
Remarks by faculty: Grade:
Signature of student: Signature of Faculty:

}
Output:

Kanishk Chouhan
(0827CS201112)

You might also like