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) {
[Link]("Kanishk Chouhan");
[Link]("0827CS201112");
[Link]("Hello World");
}
}
Output:
2. Write a program to calculate simple interest and compound interest.
import [Link].*;
public class SimpleAndCompountInterest{
public static void main(String []args){
double p, r, t, s_interest, c_interest;
Scanner scanner = new Scanner (System. in);
[Link]("Kanishk Chouhan");
[Link]("0827CS201112");
[Link]("Enter the value of Principal = ");
p = [Link]();
System. out. println("Enter the Annual Rate of Interest = ");
r = [Link]();
System. out. println("Enter the Time (years) = ");
t = [Link]();
s_interest = (p * r * t)/100;
c_interest = p * [Link](1.0+r/100.0,t) - p;
[Link]("Simple Interest: "+s_interest);
[Link]. 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 [Link];
import [Link];
import [Link];
public class JavaFactorialUsingRecursion{
public static void main(String args[]) throws NumberFormatException, IOException{
[Link]("Kanishk Chouhan");
[Link]("0827CS201112");
[Link]("Enter the number: ");
BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
int a = [Link]([Link]());
int result= fact(a);
[Link]("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 [Link].*;
class PalindromeExample {
public static void main(String args[]){
String original, reverse = ""; // Objects of String class
Scanner in = new Scanner([Link]);
[Link]("Kanishk Chouhan");
[Link]("0827CS201112");
[Link]("Enter a string/number to check if it is a palindrome");
original = [Link]();
int length = [Link]();
for ( int i = length - 1; i >= 0; i-- )
reverse = reverse + [Link](i);
if ([Link](reverse))
[Link]("Entered string/number is a palindrome.");
else
[Link]("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){
[Link]("Kanishk Chouhan");
[Link]("0827CS201112”);
for(int i=1; i<= 5 ;i++){
for(int j=0; j < i; j++){
[Link]("*");
}
[Link]("");
}
}
}
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){
[Link]("Deepakshi Choudhary");
[Link]("0827CS201064");
for(int i=5; i>0 ;i--){
for(int j=0; j < i; j++){
[Link]("*");
}
[Link]("");
}
}
}
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){
[Link]("Kanishk Chouhan");
[Link]("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 < [Link] ; i++)
sum = sum + numbers[i];
double average = sum / [Link];
[Link]("Average value of array elements is : " + average);
[Link]("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) {
[Link]("Deepakshi Choudhary");
[Link]("0827CS201064");
int number = 1234;
int reversedNumber = 0;
int temp = 0;
while(number > 0){
temp = number%10;
reversedNumber = reversedNumber * 10 + temp;
number = number/10;
}
[Link]("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){
[Link]("Kanishk Chouhan");
[Link]("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< [Link]; i++){
if(numbers[i] > largetst)
largetst = numbers[i];
else if (numbers[i] < smallest)
smallest = numbers[i];
}
[Link]("Largest Number is : " + largest);
[Link]("Smallest Number is : " + smallest);
}
}
Output of this program would be
9. Write a program to implement Bubble Sort using i/o.
import [Link].*;
class Bubble{
public static void main(String args[]) throws IOException{
[Link]("Kanishk Chouhan");
[Link]("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;
[Link]("Program to demonstrate bubble sort");
BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter the number of elements to be entered <10");
num=[Link]([Link]());
[Link]("Enter the numbers to be sorted");
int arr[]=new int[10];
for(int i=0;i< num;i++){
arr[i]=[Link]([Link]());
}
[Link]("The number you have entered:");
for(int i=0;i< num;i++){
[Link](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;
}
}
}
[Link]("The sorted numbers are");
for(int i=0;i< num;i++){
[Link](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:
[Link]("Kanishk Chouhan");
[Link]("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} };
[Link]("\nMatrix A:");
for(i=0;i< 3;i++){
for(j=0;j< 3;j++)
[Link]("\t"+mat1[i][j]);
[Link]("");
}
[Link]("\nMatrix B:");
for(i=0;i< 3;i++){
for(j=0;j< 3;j++)
[Link]("\t"+mat2[i][j]);
[Link]("");
}
[Link]("\nOperation ON Matrices \[Link] \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];
[Link]("\t" + sum[i][j]);
}
[Link]("");
}
[Link]("[Link]\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];
[Link]("\t"+ diff[i][j]);
}
[Link]("");
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:
}
[Link]("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];
[Link]("\t"+ trans[i][j]);
}
[Link]("");
}
[Link]("[Link]\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];
}
[Link]("\t"+ prod[i][j]);
}
[Link]("");
}
}
}
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
[Link]
11 13 15
17 19 21
23 25 27
[Link]
-9 -9 -9
-9 -9 -9
-9 -9 -9
3. Transpose Of A
1 4 7
2 5 8
3 6 9
[Link]
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){
[Link]("Kanishk Chouhan");
[Link]("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
[Link]("Area of Square = " + [Link]());
sh=circ; // sh dynamically bound to the Circle object referenced by circ
[Link]("Area of circle = " + [Link]());
}
}
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(){
[Link]("Animals can move");
}
}
class Dog extends Animal{
public void move(){
[Link]("Dogs can walk and run");
}
}
class TestDog{
public static void main(String args[]){
[Link]("Kanishk Chouhan");
[Link]("0827CS201112");
Animal a = new Animal(); // Animal reference and object
Animal b = new Dog(); // Animal reference but Dog object
[Link]();// runs the method in Animal class
[Link]();//Runs the method in Dog class
}
}
Output:
13. Write a program to show Permutation and [Link] [Link];
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([Link]);
[Link]("Kanishk Chouhan");
[Link]("0827CS201112");
[Link]("Enter Value of n : ");
n = [Link]();
[Link]("Enter Value of r : ");
r = [Link]();
[Link]("NCR is " +(fact(n)/(fact(n-r)*fact(r))));
[Link]("\nNPR is " +(fact(n)/(fact(n-r))));
}
}
Output:
14. Write a program for Binary to Octal Conversion.
import [Link];
class Binary_Octal{
Scanner scan;
int num;
void getVal(){
[Link]("Kanishk Chouhan");
[Link]("0827CS201112");
[Link]("Binary to Octal");
scan = new Scanner([Link]);
[Link]("\nEnter the number :");
num = [Link]([Link](), 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 = [Link](num);
[Link]("Octal Value is : " + octal);
}
}
class Main_Class{
public static void main(String... d){
Binary_Octal obj = new Binary_Octal();
[Link]();
[Link]();
}
}
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){
[Link]("Kanishk Chouhan");
[Link]("0827CS201112");
String s1 = "HELLO";
String s2 = "HELLO";
String s3 = new String("HELLO");
[Link](s1 == s2); // true
[Link](s1 == s3); // false
[Link]([Link](s2)); // true
[Link]([Link](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)