[Go to site: main page, start]

0% found this document useful (0 votes)
3 views23 pages

Basic Programs Java

The document provides a collection of basic Java programs demonstrating fundamental programming concepts such as printing output, arithmetic operations, control structures, loops, arrays, strings, and object-oriented programming. Each program includes a brief description, the code, and the expected output. Examples range from simple tasks like printing 'Hello World' to more complex operations like exception handling and class implementations.

Uploaded by

kartikbadraksh
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)
3 views23 pages

Basic Programs Java

The document provides a collection of basic Java programs demonstrating fundamental programming concepts such as printing output, arithmetic operations, control structures, loops, arrays, strings, and object-oriented programming. Each program includes a brief description, the code, and the expected output. Examples range from simple tasks like printing 'Hello World' to more complex operations like exception handling and class implementations.

Uploaded by

kartikbadraksh
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

BASIC PROGRAMS

// 1. Print "Hello World"

class HelloWorld {

public static void main(String[] args) {

[Link]("Hello World");

Output:

Hello World

// 2. Add two numbers

class AddNumbers {

public static void main(String[] args) {

int a = 10, b = 20;

int sum = a + b;

[Link]("Sum: " + sum);

Output:

Sum: 30

// 3. Check even or odd


class EvenOdd {

public static void main(String[] args) {

int num = 15;

if (num % 2 == 0)

[Link](num + " is Even");

else

[Link](num + " is Odd");

Output:

15 is Odd

// 6. Leap year check

class LeapYear {

public static void main(String[] args) {

int year = 2024;

if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))

[Link](year + " is a Leap Year");

else

[Link](year + " is not a Leap Year");

Output:

2024 is a Leap Year

// 9. Palindrome number
class PalindromeNumber {

public static void main(String[] args) {

int num = 121, original = num, rev = 0;

while (num != 0) {

rev = rev * 10 + num % 10;

num /= 10;

if (original == rev)

[Link](original + " is a Palindrome");

else

[Link](original + " is not a Palindrome");

Output:

121 is a Palindrome

// 10. Armstrong number

class ArmstrongNumber {

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;

if (original == sum)

[Link](original + " is an Armstrong Number");

else

[Link](original + " is not an Armstrong Number");

Output:

153 is an Armstrong Number

LOOP PROGRAMS
// 12. Fibonacci series

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

int n = 10, a = 0, b = 1;

[Link]("Fibonacci Series: " + a + " " + b);

for (int i = 2; i < n; i++) {

int c = a + b;

[Link](" " + c);

a = b;

b = c;

Output:

Fibonacci Series: 0 1 1 2 3 5 8 13 21 34

// 13. Factorial of a number

class Factorial {

public static void main(String[] args) {

int num = 5, fact = 1;

for (int i = 1; i <= num; i++) {

fact *= i;

[Link]("Factorial of " + num + " = " + fact);

Output:

Factorial of 5 = 120

// 15. Sum of digits


class SumOfDigits {

public static void main(String[] args) {

int num = 1234, sum = 0;

while (num != 0) {

sum += num % 10;

num /= 10;

[Link]("Sum of digits: " + sum);

Output:

Sum of digits: 10

// 16. Prime numbers 1-100

class PrimeNumbers {

public static void main(String[] args) {

[Link]("Prime numbers from 1 to 100:");

for (int i = 2; i <= 100; i++) {

boolean isPrime = true;

for (int j = 2; j <= [Link](i); j++) {

if (i % j == 0) {

isPrime = false;

break;

if (isPrime)

[Link](i + " ");

}
}

Output:

Prime numbers from 1 to 100:

2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

// 17. Check prime number

class CheckPrime {

public static void main(String[] args) {

int num = 29;

boolean isPrime = true;

if (num <= 1) {

isPrime = false;

} else {

for (int i = 2; i <= [Link](num); i++) {

if (num % i == 0) {

isPrime = false;

break;

if (isPrime)

[Link](num + " is Prime");

else

[Link](num + " is not Prime");

Output:

29 is Prime
// 18. Star patterns

class StarPattern {

public static void main(String[] args) {

int n = 5;

for (int i = 1; i <= n; i++) {

for (int j = 1; j <= i; j++) {

[Link]("* ");

[Link]();

Output:

**

***

****

*****

ARRAY PROGRAMS

// 21. Largest element in array

class LargestInArray {

public static void main(String[] args) {

int[] arr = {10, 45, 23, 67, 12};


int max = arr[0];

for (int i = 1; i < [Link]; i++) {

if (arr[i] > max)

max = arr[i];

[Link]("Largest element: " + max);

Output:

Largest element: 67

// 23. Sort an array

class SortArray {

public static void main(String[] args) {

int[] arr = {64, 25, 12, 22, 11};

// Bubble sort

for (int i = 0; i < [Link] - 1; i++) {

for (int j = 0; j < [Link] - i - 1; j++) {

if (arr[j] > arr[j + 1]) {

int temp = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = temp;

[Link]("Sorted array: ");

for (int num : arr)


[Link](num + " ");

Output:

Sorted array: 11 12 22 25 64

// 24. Linear search

class LinearSearch {

public static void main(String[] args) {

int[] arr = {10, 23, 45, 12, 67};

int key = 45, index = -1;

for (int i = 0; i < [Link]; i++) {

if (arr[i] == key) {

index = i;

break;

if (index != -1)

[Link]("Element found at index: " + index);

else

[Link]("Element not found");

Output:

Element found at index: 2


// 25. Binary search

class BinarySearch {

public static void main(String[] args) {

int[] arr = {10, 20, 30, 40, 50};

int key = 30, left = 0, right = [Link] - 1, index = -1;

while (left <= right) {

int mid = left + (right - left) / 2;

if (arr[mid] == key) {

index = mid;

break;

} else if (arr[mid] < key) {

left = mid + 1;

} else {

right = mid - 1;

if (index != -1)

[Link]("Element found at index: " + index);

else

[Link]("Element not found");

Output:

Element found at index: 2

// 27. Duplicate elements in array


class DuplicateElements {

public static void main(String[] args) {

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

[Link]("Duplicate elements:");

for (int i = 0; i < [Link]; i++) {

for (int j = i + 1; j < [Link]; j++) {

if (arr[i] == arr[j]) {

[Link](arr[i] + " ");

break;

Output:

Duplicate elements:

23

STRING PROGRAMS

// 31. Reverse string (StringBuilder)

class ReverseString {

public static void main(String[] args) {

String str = "Hello";

StringBuilder sb = new StringBuilder(str);

[Link]();

[Link]("Reversed: " + [Link]());

}
Output:

Reversed: olleH

// 32. String palindrome

class StringPalindrome {

public static void main(String[] args) {

String str = "madam";

String rev = new StringBuilder(str).reverse().toString();

if ([Link](rev))

[Link](str + " is a Palindrome");

else

[Link](str + " is not a Palindrome");

Output:

madam is a Palindrome

// 35. Character frequency

class CharacterFrequency {

public static void main(String[] args) {

String str = "hello world";

int[] freq = new int[256];

for (int i = 0; i < [Link](); i++) {

freq[[Link](i)]++;

[Link]("Character frequencies:");

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

if (freq[i] > 0) {

[Link]((char) i + ": " + freq[i]);

}
}

Output:

Character frequencies:

:1

d: 1

e: 1

h: 1

l: 3

o: 2

r: 1

w: 1

OOPS PROGRAMS

// 41. Class and object example

class Student {

String name;

int rollNo;

void display() {

[Link]("Name: " + name + ", Roll No: " + rollNo);

public static void main(String[] args) {

Student s1 = new Student();


[Link] = "John";

[Link] = 101;

[Link]();

Output:

Name: John, Roll No: 101

// 42. Constructor (default & parameterized)

class Employee {

String name;

int salary;

// Default constructor

Employee() {

name = "Unknown";

salary = 0;

// Parameterized constructor

Employee(String n, int s) {

name = n;

salary = s;

void display() {
[Link]("Name: " + name + ", Salary: " + salary);

public static void main(String[] args) {

Employee e1 = new Employee();

Employee e2 = new Employee("Alice", 50000);

[Link]();

[Link]();

Output:

Name: Unknown, Salary: 0

Name: Alice, Salary: 50000

// 43. Method overloading

class Calculator {

int add(int a, int b) {

return a + b;

int add(int a, int b, int c) {

return a + b + c;

double add(double a, double b) {

return a + b;

public static void main(String[] args) {

Calculator calc = new Calculator();


[Link]("Sum (2 params): " + [Link](10, 20));

[Link]("Sum (3 params): " + [Link](10, 20, 30));

[Link]("Sum (double): " + [Link](10.5, 20.5));

Output:

Sum (2 params): 30

Sum (3 params): 60

Sum (double): 31.0

// 44. Method overriding

class Animal {

void sound() {

[Link]("Animal makes a sound");

class Dog extends Animal {

@Override

void sound() {

[Link]("Dog barks");

public static void main(String[] args) {

Animal a = new Dog();

[Link]();
}

Output:

Dog barks

// 45. Single inheritance

class Parent {

void display() {

[Link]("This is Parent class");

class Child extends Parent {

void show() {

[Link]("This is Child class");

public static void main(String[] args) {

Child c = new Child();

[Link]();

[Link]();

}
}

Output:

This is Parent class

This is Child class

// 46. Multilevel inheritance

class GrandParent {

void show1() {

[Link]("GrandParent class");

class ParentClass extends GrandParent {

void show2() {

[Link]("Parent class");

class ChildClass extends ParentClass {

void show3() {

[Link]("Child class");

public static void main(String[] args) {

ChildClass obj = new ChildClass();

obj.show1();
obj.show2();

obj.show3();

Output:

GrandParent class

Parent class

Child class

// 47. Interface implementation

interface Drawable {

void draw();

class Circle implements Drawable {

public void draw() {

[Link]("Drawing Circle");

public static void main(String[] args) {

Circle c = new Circle();

[Link]();

Output:

Drawing Circle

// 48. Abstract class example


abstract class Shape {

abstract void area();

void display() {

[Link]("This is a shape");

class Rectangle extends Shape {

void area() {

int length = 10, breadth = 5;

[Link]("Area of Rectangle: " + (length * breadth));

public static void main(String[] args) {

Rectangle r = new Rectangle();

[Link]();

[Link]();

Output:

This is a shape

Area of Rectangle: 50

// 49. Encapsulation (get/set)

class Person {

private String name;


private int age;

public String getName() {

return name;

public void setName(String name) {

[Link] = name;

public int getAge() {

return age;

public void setAge(int age) {

[Link] = age;

public static void main(String[] args) {

Person p = new Person();

[Link]("Bob");

[Link](25);

[Link]("Name: " + [Link]() + ", Age: " +


[Link]());

Output:

Name: Bob, Age: 25


// 50. Exception handling

class ExceptionHandlingExample {

public static void main(String[] args) {

try {

int a = 10, b = 0;

int result = a / b;

[Link]("Result: " + result);

} catch (ArithmeticException e) {

[Link]("Exception caught: " + [Link]());

} finally {

[Link]("Finally block executed");

Output:

Exception caught: / by zero

Finally block executed

You might also like