0% found this document useful (0 votes)
12 views13 pages

Java Lab

Ths is java parictical made by binod.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
12 views13 pages

Java Lab

Ths is java parictical made by binod.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 13

NAME : BINOD KAPADI

ROLLNO : 12201221
COURSE : B.TECH (CSE)
YEAR : 3rd YEAR (5th SEMESTER)
SUBJECT : JAVA PROGRAMMING LAB

TEACHER : Dr. Navjot Kaur

UNIVERSITY: Punjabi University, Patiala


1) WAP to print first 15 numbers of Fibonacci series.
import java.util.Scanner;
public class Fibonacci {
public static void main(String[] args){
try(Scanner sc = new Scanner(System.in)){
System.out.println("Enter a number: ");
int N = sc.nextInt();
fibbo(N);
}
}
public static void fibbo(int N){
int num1 = 0, num2 = 1;
System.out.print(num1 + " " + num2 + " ");
for(int i = 2;i < N; i++){
int num3 = num1 + num2;
System.out.print(num3 + " ");
num1 = num2;
num2 = num3;
}
OUTPUT =>
PS E:\JAVA PROJECT> & 'C:\Program Files\Eclipse Adoptium\jdk-17.0.12.7-hotspot\bin\
java.exe' '-XX:+ShowCodeDetailsInExceptionMessages' '-cp' 'E:\JAVA PROJECT\bin' 'Fibonacci'
Enter a number:
15
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377

2) WAP to do basic Calculator Operation.


import java.util.Scanner;
public class Calculator {
public static void main(String[] args){
try (Scanner a = new Scanner(System.in)){
System.out.println("Enter the first number: ");
float b = a.nextFloat();
System.out.println("Enter the Second number: ");
float c = a.nextFloat();
System.out.println("Enter the choice (+ , - , * , /): ");
char ch = a.next().charAt(0);
float z=0;
switch(ch) {
case '+':
z = b + c;
System.out.println(b + " " + ch + " " + c + " = " + z);
break;
case '-':
z = b - c;
System.out.println(b + " " + ch + " " + c + " = " + z);
break;
case '*':
z = b * c;
System.out.println(b + " " + ch + " " + c + " = " + z);
break;
case '/':
z = b / c;
System.out.println(b + " " + ch + " " + c + " = " + z);
break;
default:
System.out.println("You entered wrong input.");
}
}
}
}

OUTPUT =>
PS E:\JAVA PROJECT> & 'C:\Program Files\Eclipse Adoptium\jdk-17.0.12.7-hotspot\bin\
java.exe' '-XX:+ShowCodeDetailsInExceptionMessages' '-cp' 'E:\JAVA PROJECT\bin' 'Calculator'
Enter the first number: 24
Enter the Second number: 45
Enter the choice (+ , - , * , /): +
24.0 + 45.0 = 69.0

Enter the first number: 35


Enter the Second number: 7
Enter the choice (+ , - , * , /): /
35.0 / 7.0 = 5.0
3) WAP to calculate a factorial of a given number using recursion.
import java.util.Scanner;
public class Factorialrecursion {
public static long factorial(int a) {
if (a >= 1) {
return a * factorial(a - 1);
} else {
return 1;
}
}
public static void main(String[] args){
try(Scanner sc = new Scanner(System.in)){
System.out.println("Enter a number: ");
int a = sc.nextInt();

long fact = factorial(a);


System.out.println("The factorial of " + a + " is : " + fact);
}
}
}

Output =>
PS E:\JAVA PROJECT> & 'C:\Program Files\Eclipse Adoptium\jdk-17.0.12.7-hotspot\bin\
java.exe' '-XX:+ShowCodeDetailsInExceptionMessages' '-cp' 'E:\JAVA PROJECT\bin'
'Factorialrecursion'
Enter a number:
7
The factorial of 7 is : 5040
4) WAP to check wheather a given Number and String is palindrome or not

FOR NUMBER=>
import java.util.Scanner;
public class Palindrome {
public static void main(String[] arg) {
try (Scanner sc = new Scanner(System.in)) {
System.out.println("Enter a number: ");
int num = sc.nextInt();
int m = num;
int x = palindrome(num);
if (x == m) {
System.out.println("The number " + m + " is palindrome.");
} else {
System.out.println("The number " + m + " is not palindrome.");
}
}
}
public static int palindrome(int num) {
int reverse = 0;
while(num != 0){
int rem = num % 10;
reverse = reverse * 10 +rem;
num /= 10;
}
return reverse;
}
}
OUTPUT =>
PS E:\JAVA PROJECT> e:; cd 'e:\JAVA PROJECT'; & 'C:\Program Files\Eclipse Adoptium\jdk-
17.0.12.7-hotspot\bin\java.exe' '-XX:+ShowCodeDetailsInExceptionMessages' '-cp' 'E:\JAVA
PROJECT\bin' 'Palindrome'
Enter a number: 12221
The number 12221 is palindrome.

Enter a number: 123


The number 123 is not palindrome.
FOR STRING =>
import java.util.Scanner;
public class Palindromestring {
public static void main(String[] args){
String input;
try(Scanner sc = new Scanner(System.in)){
System.out.println("Enter the String you wnat to check: ");
input = sc.nextLine();
String str = input.toLowerCase(); // Convert string to lower case to make comparision
case in sensitive
int length = str.length();
boolean isPalindrome = true;
int start = 0;
int end = (length -1);
while(start<end){
if(str.charAt(start) != str.charAt(end)){
isPalindrome = false;
break;
}
start++;
end--;
}
if(isPalindrome){
System.out.println(str + " is a palindrome string.");
}
else {
System.out.println(str + " is not a palindrome string.");
}
}
}
}

OUTPUT =>
PS E:\JAVA PROJECT> e:; cd 'e:\JAVA PROJECT'; & 'C:\Program Files\Eclipse Adoptium\jdk-
17.0.12.7-hotspot\bin\java.exe' '-XX:+ShowCodeDetailsInExceptionMessages' '-cp' 'E:\JAVA
PROJECT\bin' 'Palindromestring'
Enter the String you wnat to check: madam
madam is a palindrome string.

Enter the String you wnat to check: palpa


palpa is not a palindrome string.
5) WAP to implement Function Overloading in java.
class Overloading {
public int add(int a, int b) {
return a + b;
}
public int add(int a, int b, int c) {
return a + b + c; }
public double add(double a, double b) {
return a + b;
}
public String add(String a, String b) {
return a + b;
}
}
public class Functionoverloading {
public static void main(String[] args) {
Overloading Ov = new Overloading();
int sum1 = Ov.add(17, 26);
int sum2 = Ov.add(19, 29, 33);
double sum3 = Ov.add(17.9, 26.3);
String str1 = Ov.add("Binod ", "Kapadi");
System.out.println("Sum of two integers is : " + sum1);
System.out.println("Sum of three integers is : " + sum2);
System.out.println("Sum of two doubles is : " + sum3);
System.out.println("Concatenated String is : " + str1);
}
}
OUTPUT =>
PS E:\JAVA PROJECT> & 'C:\Program Files\Eclipse Adoptium\jdk-17.0.12.7-hotspot\bin\
java.exe' '-XX:+ShowCodeDetailsInExceptionMessages' '-cp' 'E:\JAVA PROJECT\bin'
'Functionoverloading'
Sum of two integers is : 43
Sum of three integers is : 81
Sum of two doubles is : 44.2
Concatenated String is : Binod Kapadi
6) WAP to implement Function Overriding in java.
class Vehicle{
public void input(){
System.out.println("This is the Vehicle.");
}
public void brand(){
System.out.println("Name the brand of the vehicle: ");
}
}
class Car{
public void input(){
System.out.println("This is a car.");
}
public void brand(){
System.out.println("ROLLS ROYCE.");
}
}
public class Functionoverriding {
public static void main(String [] args){
Vehicle Vh = new Vehicle();
Car Cr = new Car();
Vh.input();
Cr.input();
Vh.brand();
Cr.brand();
}
}
OUTPUT = >

PS E:\JAVA PROJECT> e:; cd 'e:\JAVA PROJECT'; & 'C:\Program Files\Eclipse Adoptium\jdk-


17.0.12.7-hotspot\bin\java.exe' '-XX:+ShowCodeDetailsInExceptionMessages' '-cp' 'E:\JAVA
PROJECT\bin' 'Functionoverriding'
Name of the Vehicle.
CAR.
Name the brand of the vehicle:
ROLLS ROYCE.

You might also like