0% found this document useful (0 votes)
7 views16 pages

Bin

The document contains multiple Java programs demonstrating various programming concepts, including Fibonacci series, prime number checking, palindrome checking, factorial calculation, and Armstrong number validation. It also covers object-oriented programming with classes like Student, BankAcct, and ObjectCounter, as well as array manipulations such as finding sums, frequencies, and separating odd and even numbers. Additionally, it includes string manipulation techniques and recursive function implementations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views16 pages

Bin

The document contains multiple Java programs demonstrating various programming concepts, including Fibonacci series, prime number checking, palindrome checking, factorial calculation, and Armstrong number validation. It also covers object-oriented programming with classes like Student, BankAcct, and ObjectCounter, as well as array manipulations such as finding sums, frequencies, and separating odd and even numbers. Additionally, it includes string manipulation techniques and recursive function implementations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

// 1a) Fibonacci Series in Java

class Fibonacci {
public static void main(String[] args) {
int n = 10, first = 0, second = 1;
System.out.print("Fibonacci Series: " + first + " " + second);
for (int i = 2; i < n; i++) {
int next = first + second;
System.out.print(" " + next);
first = second;
second = next;
}
}
}

// 1b) Prime Number Program in Java


class PrimeCheck {
public static void main(String[] args) {
int num = 29;
boolean isPrime = true;
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
System.out.println(num + " is " + (isPrime ? "a Prime Number" :
"not a Prime Number"));
}
}

// 1c) Palindrome Program in Java


class Palindrome {
public static void main(String[] args) {
int num = 121, reversed = 0, original = num;
while (num != 0) {
int digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
}
System.out.println(original + " is " + (original == reversed ? "a
Palindrome" : "not a Palindrome"));
}
}

// 1d) Factorial Program in Java


class Factorial {
public static void main(String[] args) {
int num = 5, fact = 1;
for (int i = 1; i <= num; i++) {
fact *= i;
}
System.out.println("Factorial of " + num + " is " + fact);
}
}

// 1e) Armstrong Number in Java


class Armstrong {
public static void main(String[] args) {
int num = 153, original = num, sum = 0;
while (num != 0) {
int digit = num % 10;
sum += digit * digit * digit;
num /= 10;
}
System.out.println(original + " is " + (original == sum ? "an
Armstrong Number" : "not an Armstrong Number"));
}
}
// **2a) Student Class with ID and Name**
class Student {
int id;
String name;

Student(int id, String name) {


this.id = id;
this.name = name;
}

void display() {
System.out.println("ID: " + id + ", Name: " + name);
}

public static void main(String[] args) {


Student student1 = new Student(1, "John");
student1.display();
}
}

// **2b) Outer Class BankAcct & Inner Class Interest**


class BankAcct {
double balance = 1000;

class Interest {
double rate = 5.0;

void displayInterest() {
System.out.println("Interest on balance " + balance + " at rate
" + rate + "% is: " + (balance * rate / 100));
}
}

public static void main(String[] args) {


BankAcct bankAcct = new BankAcct();
BankAcct.Interest interest = bankAcct.new Interest();
interest.displayInterest();
}
}

// **2c) Find Number of Objects Created of a Class**


class ObjectCounter {
static int count = 0;

ObjectCounter() {
count++;
}

static void displayCount() {


System.out.println("Number of objects created: " + count);
}

public static void main(String[] args) {


new ObjectCounter();
new ObjectCounter();
ObjectCounter.displayCount();
}
}

// **2d) Passing and Returning Objects**


class Sample {
int value;

Sample(int value) {
this.value = value;
}

Sample modify(Sample obj) {


return new Sample(obj.value + 10);
}

void display() {
System.out.println("Value: " + value);
}

public static void main(String[] args) {


Sample obj1 = new Sample(20);
Sample obj2 = obj1.modify(obj1);
obj2.display();
}
}

// **2e) Swap Objects using Swap() Method**


class SwapObjects {
int value;

SwapObjects(int value) {
this.value = value;
}

static void swap(SwapObjects a, SwapObjects b) {


int temp = a.value;
a.value = b.value;
b.value = temp;
}

public static void main(String[] args) {


SwapObjects obj1 = new SwapObjects(5);
SwapObjects obj2 = new SwapObjects(10);
System.out.println("Before Swap: obj1 = " + obj1.value + ", obj2 =
" + obj2.value);
swap(obj1, obj2);
System.out.println("After Swap: obj1 = " + obj1.value + ", obj2 = "
+ obj2.value);
}
}
// **3a) Find Sum and Average of All Elements in an Array**
class ArraySumAvg {
public static void main(String[] args) {
int[] arr = {10, 20, 30, 40, 50};
int sum = 0;
for (int num : arr) {
sum += num;
}
double avg = (double) sum / arr.length;
System.out.println("Sum: " + sum + ", Average: " + avg);
}
}

// **3b) Find the Frequency of All Duplicate Elements in an Array**


import java.util.HashMap;
class ArrayFrequency {
public static void main(String[] args) {
int[] arr = {1, 2, 2, 3, 3, 3, 4};
HashMap<Integer, Integer> freqMap = new HashMap<>();
for (int num : arr) {
freqMap.put(num, freqMap.getOrDefault(num, 0) + 1);
}
System.out.println("Element Frequency: " + freqMap);
}
}

// **3c) Print All Non-Repeated Elements in an Array**


import java.util.HashMap;
class UniqueElements {
public static void main(String[] args) {
int[] arr = {1, 2, 2, 3, 3, 4, 5};
HashMap<Integer, Integer> freqMap = new HashMap<>();
for (int num : arr) {
freqMap.put(num, freqMap.getOrDefault(num, 0) + 1);
}
System.out.print("Non-repeated elements: ");
for (int num : arr) {
if (freqMap.get(num) == 1) {
System.out.print(num + " ");
}
}
}
}

// **3d) Multiply Two Matrices**


class MatrixMultiplication {
public static void main(String[] args) {
int[][] A = {{1, 2}, {3, 4}};
int[][] B = {{2, 0}, {1, 2}};
int[][] C = new int[2][2];
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) {
C[i][j] += A[i][k] * B[k][j];
}
}
}
System.out.println("Resultant Matrix:");
for (int[] row : C) {
for (int val : row) {
System.out.print(val + " ");
}
System.out.println();
}
}
}

// **3e) Separate Odd and Even Numbers from an Array**


class SeparateOddEven {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8};
System.out.print("Even Numbers: ");
for (int num : arr) {
if (num % 2 == 0) System.out.print(num + " ");
}
System.out.println();
System.out.print("Odd Numbers: ");
for (int num : arr) {
if (num % 2 != 0) System.out.print(num + " ");
}
}
}
// **4a) Concatenate Two Strings**
class StringConcatenation {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = " World";
String result = str1 + str2;
System.out.println("Concatenated String: " + result);
}
}

// **4b) Reverse Each Word in a String**


class ReverseWords {
public static void main(String[] args) {
String sentence = "Java Programming";
String[] words = sentence.split(" ");
StringBuilder reversedSentence = new StringBuilder();
for (String word : words) {
StringBuilder sb = new StringBuilder(word);
reversedSentence.append(sb.reverse()).append(" ");
}
System.out.println("Reversed Words: " +
reversedSentence.toString().trim());
}
}

// **4c) Explain Call by Value and Call by Reference**


class CallByValueRef {
void callByValue(int num) {
num = num + 10;
}
void callByReference(int[] arr) {
arr[0] += 10;
}
public static void main(String[] args) {
CallByValueRef obj = new CallByValueRef();
int num = 5;
obj.callByValue(num);
System.out.println("After Call by Value: " + num);
int[] arr = {5};
obj.callByReference(arr);
System.out.println("After Call by Reference: " + arr[0]);
}
}

// **4d) Implement Recursion (Factorial of a Number)**


class FactorialRecursion {
static int factorial(int n) {
if (n == 0 || n == 1) return 1;
return n * factorial(n - 1);
}
public static void main(String[] args) {
int num = 5;
System.out.println("Factorial of " + num + " is " + factorial(num));
}
}

// **4e) Recursive Fibonacci Series using Function**


class RecursiveFibonacci {
static int fibonacci(int n) {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
public static void main(String[] args) {
int n = 10;
System.out.print("Fibonacci Series: ");
for (int i = 0; i < n; i++) {
System.out.print(fibonacci(i) + " ");
}
}
}

You might also like