Java Programs PracticalFile
Java Programs PracticalFile
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);
}
}
}
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;
}
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;
}
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) {
binary /= 10;
}
return decimalNumber;
}
String octalString =
Integer.toOctalString(decimalNumber);
return octalNumber;
Page | 12
}
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;
}
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) {
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");
}
}
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 SI = (p * r * t) / 100;
}
}
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) {
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 {
Output:
30
10
24.0
Page | 21
Question17. Write a Java program to multiply two matrices.
Solution Code:
class MatricesMultiplication {
static int N = 4;
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 } };
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) {
}
}
Output:
Programmer Salary: 50000
Programmer Bonus: 6000
Page | 24