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

Java Programs PracticalFile

Uploaded by

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

Java Programs PracticalFile

Uploaded by

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

INDEX

Sr. Page
No. Topic no.
3
1. Write a Java program for Fibonacci series.
4
2. Write a Java program for prime number.
5
3. Write a Java program for factorial of a number.
6
4. Write a Java program for reversing a number.
Write a Java program to display even numbers
7
5. from 1 to 100.
Write a Java program to display odd numbers
8
6. from 1 to 100.
Write a Java program to find sum of natural
9
7. numbers.
10
8. Write a Java program for linear Search.
Write a Java program to display current date and
11
9. time.
12
10. Write a Java program to convert binary to octal.
14
11. Write a Java program to convert octal to decimal.
Write a Java program to check given year is leap
15
12. year or not.
Write a Java program to calculate Simple Interest
17
13. and Compound Interest.
Write a Java program to print triangle star
18
14. pattern.
Page | 1
Write a Java program to check whether the
20
15. character is vowel or consonant.
Write a Java program to show Overloading of
21
16. methods in class.
22
17. Write a Java program to multiply two matrices.
24
18. Write a Java program to show Inheritance in class.

Page | 2
Question1. Write a Java program for Fibonacci series.
Solution Code:
class FibonacciSeries {
static int fibonacci(int n) {
if (n <= 1) {
return n;
}
return fibonacci(n - 2) + fibonacci(n - 1);
}

public static void main(String args[]) {


int N = 10;

for (int i = 0; i < N; i++) {


System.out.print(fibonacci(i) + " ");
}

}
}
Output:
0 1 1 2 3 5 8 13 21 34

Page | 3
Question2. Write a Java program for prime number.
Solution Code:
class Prime {
static boolean isPrime(int n) {
if (n <= 1) {
return false;
}
for (int i = 2; i <= n / 2; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}

public static void main(String[] args) {


if (isPrime(11)) {
System.out.println("11 is Prime Number");
} else {
System.out.println("11 is not Prime Number");
}
if (isPrime(15)) {
System.out.println("15 is Prime Number");
} else {
System.out.println("15 is not Prime Number");
}
}
}

Output:
11 is Prime Number
15 is not Prime Number

Page | 4
Question3. Write a Java program for factorial of a number.
Solution Code:
class Factorial {
// Method to find factorial
// of given number
static int factorial(int n) {
int res = 1, i;
for (i = 2; i <= n; i++)
res *= i;
return res;
}

// main method
public static void main(String[] args) {
int num = 5;
System.out.println("Factorial of " + num + " is "
+ factorial(5));
}
}
Output:
Factorial of 5 is 120

Page | 5
Question4. Write a Java program for reversing a number.
Solution Code:
public class ReverseNumber {
// Function to reverse the number
static int reverse(int n) {
// reversed number
int rev = 0;
// remainder
int rem;
while (n > 0) {
rem = n % 10;
rev = (rev * 10) + rem;
n = n / 10;
}
return rev;
}
// Driver Function
public static void main(String[] args) {
int n = 4526;
System.out.print("Reversed Number is "
+ reverse(n));
}
}
Output:
Reversed Number is 6254

Page | 6
Question5. Write a Java program to display even numbers
from 1 to 100.
Solution Code:
class DisplayEvenNumbers {
public static void main(String args[]) {
int number = 100;
System.out.println("List of Even Numbers from 1 to " +
number + ": ");
for (int i = 1; i <= number; i++) {

if (i % 2 == 0) {
System.out.print(i + " ");
}
}
}
}

Output:
List of Even Numbers from 1 to 100:
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42
44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80
82 84 86 88 90 92 94 96 98 100

Page | 7
Question6. Write a Java program to display odd numbers
from 1 to 100.
Solution Code:
class DisplayOddNumbers {
public static void main(String args[]) {
int number = 100;
System.out.println("List of Odd Numbers from 1 to " +
number + ": ");
for (int i = 1; i <= number; i++) {
if (i % 2 != 0) {
System.out.print(i + " ");
}
}
}
}

Output:
List of Odd Numbers from 1 to 100:
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41
43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79
81 83 85 87 89 91 93 95 97 99

Page | 8
Question7. Write a Java program to find sum of natural
numbers.
Solution Code:
class SumOfNaturalNumbers {
public static void main(String[] args) {
int n = 10, sum = 0;
for (int i = 1; i <= n; i++) {
sum = sum + i;
}
System.out.println("Sum of first " + n + " Natural
Numbers = " + sum);
}
}

Output:
Sum of first 10 Natural Numbers = 55

Page | 9
Question8. Write a Java program for linear Search.
Solution Code:
public class LinearSearch {
static int search(int arr[], int n, int x) {
for (int i = 0; i < n; i++) {
if (arr[i] == x) {
return i;
}
}
return -1;
}

public static void main(String[] args) {


int arr[] = { 1, 3, 8, 2, 11 };
int n = arr.length;
int x = 3;
int index = search(arr, n, x);
if (index == -1) {
System.out.println("Number not found");
} else {
System.out.println("Number found at position " +
index);
}
}
}
Output:
Number found at position 1
Page | 10
Question9. Write a Java program to display current date and
time.
Solution Code:
import java.util.Date;

class CurrentDateTime {
public static void main(String[] args) {
Date current_Date = new Date();
System.out.println(current_Date);
}
}

Output:
Sun Jan 07 17:38:06 IST 2024

Page | 11
Question10. Write a Java program to convert binary to
octal.
Solution Code:
class BinaryToOctal {
int binaryToDecimal(long binary) {

int decimalNumber = 0, i = 0;
while (binary > 0) {

decimalNumber += Math.pow(2, i++) * (binary % 10);

binary /= 10;
}

return decimalNumber;
}

int decimalToOctal(long binary) {

int decimalNumber = binaryToDecimal(binary);

String octalString =
Integer.toOctalString(decimalNumber);

int octalNumber = Integer.parseInt(octalString);

return octalNumber;

Page | 12
}

public static void main(String[] args) {

BinaryToOctal ob = new BinaryToOctal();

System.out.println("octal number:" +
ob.decimalToOctal(100100));

}
}

Output:
octal number:44

Page | 13
Question11. Write a Java program to convert octal to
decimal.
Solution Code:
class OctalToDecimal {
static int octalToDecimal(int x) {

int result = 0;
int copy_x = x;
for (int i = 0; copy_x > 0; i++) {
int temp = copy_x % 10;
double p = Math.pow(8, i);
result += p * temp;
copy_x /= 10;
}
return result;
}

public static void main(String[] args) {


int a = 167;
int ans = octalToDecimal(a);
System.out.println("Decimal of Octal Number (" + a +
"): " + ans);
}
}

Output:
Decimal of Octal Number (167): 119
Page | 14
Question12. Write a Java program to check given year is
leap year or not.
Solution Code:
class LeapYear {
static void isLeapYear(int year) {

boolean is_leap_year = false;

if (year % 4 == 0) {
is_leap_year = true;

if (year % 100 == 0) {

if (year % 400 == 0) {
is_leap_year = true;
} else {
is_leap_year = false;
}
}
} else {

is_leap_year = false;
}
Page | 15
if (!is_leap_year) {
System.out.println(year + " : Non Leap-year");
} else {
System.out.println(year + " : Leap-year");
}
}

public static void main(String[] args) {

isLeapYear(1900);
isLeapYear(1996);
isLeapYear(2000);
isLeapYear(2002);

}
}

Output:
1900 : Non Leap-year
1996 : Leap-year
2000 : Leap-year
2002 : Non Leap-year

Page | 16
Question13. Write a Java program to calculate Simple
Interest and Compound Interest.
Solution Code:
class SimpleAndCompoundInterest {
public static void main(String[] args) {

double p = 10000, r = 8.25, t = 5;

System.out.println("Principle :" + p);


System.out.println("Annual Interest Rate :" + r);
System.out.println("Time(Years) :" + t);

double SI = (p * r * t) / 100;

double CI = p * Math.pow(1.0 + r / 100.0, r) - p;


System.out.println("Simple Interest : " + SI);
System.out.println("Compound Interest : " + CI);

}
}

Output:
Principle :10000.0
Annual Interest Rate :8.25
Time(Years) :5.0
Simple Interest : 4125.0
Compound Interest : 9232.255345361798
Page | 17
Question14. Write a Java program to print triangle star
pattern.
Solution Code:
import java.util.Scanner;

class TriangleStarPattern {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println(" Triangle Star Pattern");
System.out.print("Enter number of rows to be Printed:
");
int rows = sc.nextInt();
for (int i = 1; i <= rows; i++) {
for (int j = rows; j >= i; j--) {
System.out.print(" ");
}
for (int j = 1; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
}
Page | 18
Output:
Triangle Star Pattern
Enter number of rows to be Printed: 5
*
**
***
****
*****

Page | 19
Question15. Write a Java program to check whether the
character is vowel or consonant.
Solution Code:
import java.util.Scanner;

class VowelConsonant {
public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter any character from a to z (A to


Z): ");
char ch = Character.toLowerCase(sc.next().charAt(0));

if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch ==


'u') {
System.out.println("It is a Vowel.");
} else {
System.out.println("It is a Consonant.");
}
}
}

Output:
Enter any character from a to z (A to Z): a
It is a Vowel.

Page | 20
Question16. Write a Java program to show Overloading
of methods in class.
Solution Code:
class Sum {

public static int sum(int a, int b) {


return a + b;
}

public static int sum(int x, int y, int z) {


return x + y + z;
}
public static double sum(double p, double q) {
return p + q;
}
public static void main(String[] args) {
System.out.println(sum(10, 20));
System.out.println(sum(1, 4, 5));
System.out.println(sum(14.5, 9.5));
}
}

Output:
30
10
24.0
Page | 21
Question17. Write a Java program to multiply two matrices.
Solution Code:
class MatricesMultiplication {
static int N = 4;

static void multiply(int mat1[][], int mat2[][], int res[][]) {

for (int i = 0; i < N; i++) {


for (int j = 0; j < N; j++) {
res[i][j] = 0;
for (int k = 0; k < N; k++) {
res[i][j] += mat1[i][k] * mat2[k][j];
}
}
}
}

public static void main(String[] args) {

int m1[][] = { { 1, 2, 3, 4 },
{ 1, 2, 3, 4 },
{ 1, 2, 3, 4 },
{ 1, 2, 3, 4 } };

int m2[][] = { { 1, 2, 3, 4 },
{ 1, 2, 3, 4 },
{ 1, 2, 3, 4 },

Page | 22
{ 1, 2, 3, 4 } };

int result[][] = new int[N][N];


multiply(m1, m2, result);

System.out.println("Result Matrix is");


for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
System.out.print(result[i][j] + " ");
}
System.out.println();
}
}
}

Output:
Result Matrix is
10 20 30 40
10 20 30 40
10 20 30 40
10 20 30 40

Page | 23
Question18. Write a Java program to show Inheritance in class.
Solution Code:
// Base Class
class Employee {
int salary = 50000;
}

// Inherited Class
class Programmer extends Employee {
int bonus = 6000;
}

// Driver Class
class TestInheritance {
public static void main(String[] args) {

Programmer p = new Programmer();


System.out.println("Programmer Salary: " + p.salary);
System.out.println("Programmer Bonus: " + p.bonus);

}
}

Output:
Programmer Salary: 50000
Programmer Bonus: 6000

Page | 24

You might also like