[Go to site: main page, start]

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

Java Programs

The document contains a series of programming exercises written in Java, covering various concepts such as abstract classes, arrays, prime number checking, method overloading, and matrix operations. Each program includes code snippets and sample outputs demonstrating the functionality. The exercises are designed for BCA students to practice and enhance their programming skills.

Uploaded by

excellentayush77
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views17 pages

Java Programs

The document contains a series of programming exercises written in Java, covering various concepts such as abstract classes, arrays, prime number checking, method overloading, and matrix operations. Each program includes code snippets and sample outputs demonstrating the functionality. The exercises are designed for BCA students to practice and enhance their programming skills.

Uploaded by

excellentayush77
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

INDEX

Page Date
[Link]. Topic
No.
Write a program using abstract class that takes
1 1
radius as input and prints area of circle
Take an array of numbers as input and check if it
2 2
is an array sorted in ascending order.
Take a number as an input from user and check
3 3
whether number is prime or not.
Write a program to demonstrate method
4 4
overloading.
5 Write a program to store 3×4 matrix and print it 5
6 Write a program to reverse the string. 6
Write a program that prints the sum of all odd
7 7
numbers from 1 to n.
Taking a matrix as an input and search for an
8 8
element x in matrix.
9 Write a program to demonstrate array linear search 9
Write a program that prints the table of given
10 10
number.
11 Write a program that demonstrates interface 11-12
Write a program that generates random
12 13
numbers(if numbers =5, exit program).
13 Write a program to check even or odd number. 14
14 Write a program to find Fibonacci series. 15
Write a program to print palindromic number
15 16
pyramid

Teacher’s Signature

By- AYUSH TIWARI BCA(2024-27)


PROGRAM-01
Write a program using abstract class that takes radius as input and prints area of circle :

import [Link].*;
abstract class Shape{
abstract double area();
}
class Circle extends Shape{
private double radius;

public Circle(double radius) {


[Link] = radius;
}
@Override
double area(){
return [Link] * radius * radius;
}
}
public class
Abstraction {
public static void main(String[]args){
Scanner sc = new Scanner([Link]);
[Link]("Enter the radius: ");
double r = [Link]();
Shape cir = new Circle(r);
double calculatedArea = [Link]();
[Link]("The area of the circle is: " + calculatedArea); }

OUTPUT(1)
Enter the radius: 6.2

The area of the circle is: 120.76282160399165

By- AYUSH TIWARI BCA(2024-27)


PROGRAM-2
Take an array of numbers as input and check if it is an array sorted in ascending order:

import [Link] .*;

public class Arrays {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the size: ");
int size = [Link]();
int numbers[] = new int[size];
[Link]("Enter the numbers");
//input
for (int i = 0; i < size; i++) {
numbers[i] = [Link]();
}
boolean isAscending = true;
for (int i = 0; i < [Link] - 1; i++) { // NOTICE [Link] - 1 as
// termination condition
if (numbers[i] > numbers[i + 1]) { // This is the condition for
// descending order
isAscending = false;
}
}

if (isAscending) {
[Link]("The array is sorted in ascending order");
} else {
[Link]("The array is not sorted in ascending order");
}
}
}
OUTPUT(2)
Enter the size: 3
Enter the numbers
123
The array is sorted in ascending order

By- AYUSH TIWARI BCA(2024-27)


PROGRAM-3
Take a number as an input from user and check whether number is prime or not:
import [Link].*;

public class Loops {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

[Link]("Enter the number you want to check:");


int num = [Link]();
int i;
boolean isPrime = true;
for (i = 2; i < num / 2; i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
if (num == 1) {
[Link]("not prime neither composite");
} else {
[Link]("Prime Number");

}
} else {
[Link]("not prime");
}

}
}

OUTPUT(3)
Enter the number you want to check:
7
Prime Number

By- AYUSH TIWARI BCA(2024-27)


PROGRAM-4

Write a program to demonstrate method overloading:


class Overloading {
public int display(int a, int b) {
return a + b;
}

public int display(int a, int b, int c) {


return a + b + c;
}
}

public class MethOverloading {


public static void main(String[] args) {
Overloading mo = new Overloading();
int res = [Link](12, 15);
int res1 = [Link](2, 3, 4);
[Link](res);
[Link](res1);
}
}

OUTPUT(4)

27

By- AYUSH TIWARI BCA(2024-27)


PROGRAM-5

Write a program to store 3×4 matrix and print it:

public class MultiDArray {


public static void main(String[] args) {
int i, j;
int a[][] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
for (i = 0; i <= 2; i++) {
for (j = 0; j <= 3; j++) {
[Link](a[i][j]);
if (a[i][j] == a[0][1] || a[i][j] == a[0][2] || a[i][j] == a[1][1] || a[i][j] == a[1][2]) {
[Link](" ");

} else {
[Link](" ");
}
}
[Link]();
}

OUTPUT(5)
1 2 3 4
5 6 7 8
9 10 11 12

By- AYUSH TIWARI BCA(2024-27)


PROGRAM-6

write a program to reverse the string:

public class ReverseString {


public static void main (String []args){
StringBuilder sb = new StringBuilder("Ayush");
for(int i = 0; i<[Link]()/2; i++){
int front = i;
int back = [Link]()-1-i;
char frontChar = [Link](front);
char backChar = [Link](back);
[Link](front,backChar);
[Link](back,frontChar);
}
[Link]("Reversed String is: "+sb);
}
}

OUTPUT (6)

Reversed String is: hsuyA

By- AYUSH TIWARI BCA(2024-27)


PROGRAM-7
Write a program that prints the sum of all odd numbers from 1 to n:

import [Link].*;
public class sumOfOdds {
public static void sumOfOddNum(int n) {
int sum = 0;
for (int i = 1; i <= n; i++) {
if (i % 2 != 0) {
sum = sum + i;
}
}
[Link]("The sum of all odd numbers from 1 to " + n + " " + "is: " + sum);
}

public static void main(String[] args) {


Scanner sc = new Scanner([Link]);
[Link]("Enter the N: ");
int x = [Link]();
sumOfOddNum(x);

}
}

OUTPUT (7)
Enter the N: 5
The sum of all odd numbers from 1 to 5 is: 9

By- AYUSH TIWARI BCA(2024-27)


PROGRAM-8

Taking a matrix as an input and search for an element x in matrix:

import [Link].*;
public class TwoDArray {
public static void main(String[] args){
Scanner sc = new Scanner([Link]);

[Link]("Enter the rows: ");


int row = [Link]();
[Link]("Enter the columns: ");
int col = [Link]();
int [] [] matrix = new int[row][col];
[Link]("Input the matrix elements:");
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
matrix[i][j] = [Link]();
}
}

[Link]("Enter the element that you want so search in matrix: ");


int x = [Link]();
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (matrix[i][j]==x){
[Link](x + " is found at location ("+i+","+j+")");
}
}
}
}
}

OUTPUT (8)
Enter the rows: 2
Enter the columns: 3
Input the matrix elements:
123
456

By- AYUSH TIWARI BCA(2024-27)


Enter the element that you want so search in matrix: 2
2 is found at location (0,1)

PROGRAM-9

Write a program to demonstrate array linear search:

import [Link].*;
public class ArrayLinearSearch {
public static void main (String []args){
Scanner sc = new Scanner([Link]);
[Link]("Enter the size of Array: ");
int size = [Link]();
int [] numbers = new int[size];
for (int i =0; i< [Link]; i++){
[Link]("Enter number for index "+i+": ");
numbers[i] =[Link]();
}
[Link]("Enter the number you want to search: ");
int x = [Link]();
for (int i = 0; i < [Link]; i++) {
if (numbers[i]==x) {
[Link](x+" is found at index "+ i);
}
}
}
}

OUTPUT (9)
Enter the size of Array: 3
Enter number for index 0: 1
Enter number for index 1: 2
Enter number for index 2: 3
Enter the number you want to search: 2
2 is found at index 1

By- AYUSH TIWARI BCA(2024-27)


PROGRAM10

Write a program that prints the table of given number:


import [Link].*;
public class tablePrinter {
public static void printTable(int a) {
for (int i = 1; i <= 10; i++) {
[Link](a + "*" + i + "=" + a * i);
}
}

public static void main(String[] args) {


Scanner sc = new Scanner([Link]);
[Link]("Enter the Number whose table you want to print: ");
int x = [Link]();
printTable(x);
}
}

OUTPUT (10)
Enter the Number whose table you want to print: 44
44*1=44
44*2=88
44*3=132
44*4=176
44*5=220
44*6=264
44*7=308
44*8=352
44*9=396
44*10=440

By- AYUSH TIWARI BCA(2024-27)


PROGRAM:-11

Write a program that demonstrates interface:

interface SmartTvRemote{
public void VoiceCommand();
public void SmartSearch();
}
interface TvRemote extends SmartTvRemote{
public void powerOn();
public void powerOff();
}
class Tv implements TvRemote{
@Override
public void powerOn() {
[Link]("Power On");
}

@Override
public void VoiceCommand() {
[Link]("Listening Your Voice Command..");
}

@Override
public void SmartSearch() {
[Link]("Searching content on your preferences");
}

@Override
public void powerOff() {
[Link]("Power Off");
}
}

public class demonstrateInterface {


public static void main(String[] args) {

By- AYUSH TIWARI BCA(2024-27)


Tv LG = new Tv();
[Link]();

[Link]();
[Link]();
[Link]();

}
}

OUTPUT (11)
Power On

Listening Your Voice Command..

Searching content on your preferences

Power Off

By- AYUSH TIWARI BCA(2024-27)


PROGRAM:--12
Write a program that generates random numbers:

public class RandomNumGen {


public static void main (String []args){
while(true){
int num = (int) ([Link]()* 10+1);
if (num == 5) {
break;
}
[Link](num+ " ");
}
}
}

//output changes every time you execute the program

OUTPUT (12)

8 8 4 3 10 4 8 6 4

By- AYUSH TIWARI BCA(2024-27)


PROGRAM:--13

WRITE A PROGRAM TO CHECK EVEN OR ODD NUMBER:-

import [Link].*;

public class OddEven {

public static void main(String[] args) {


[Link]("Enter a number: ");
Scanner sc = new Scanner([Link]);
int num = [Link]();
if (num % 2 == 0) {
[Link]("The given number is even number");
} else {
[Link]("The given number is odd number");
}

}
}

OUTPUT (13)
Enter a number:

The given number is odd number

By- AYUSH TIWARI BCA(2024-27)


PROGRAM:--14

WRITE A PROGRAM TO FIND FIBONACCI SERIES:--


import [Link].*;
public class fibonacciSeries {
public static void fibonacci(int n){
int c, a=0;
int b=1;
[Link]("The Fibonacci series of "+n+" terms: ");
for (int i = 0; i <=n; i++) {
[Link](" "+a);
c = a+b;
a=b;
b=c;
}
}
public static void main (String[] args){
Scanner sc = new Scanner([Link]);
[Link]("Enter the terms: ");
int n = [Link]();
fibonacci(n);
}
}

OUTPUT (14)
Enter the terms: 5

The Fibonacci series of 5 terms: 0 1 1 2 3 5

By- AYUSH TIWARI BCA(2024-27)


PROGRAM:--15

Write a program to print palindromic number pyramid:

public class Patterns {


public static void main(String[]args) {

int n=5;
for (int i = 1; i <=n ; i++) {
for (int j = 1; j <=n-i ;j++ ) {
[Link](" ");
}
for (int j = i; j >=1 ; j--) {
[Link](j);
}
for (int j = 2; j <=i ; j++) {
[Link](j);
}
[Link]();
}

OUTPUT (15)
1

212

32123

4321234

543212345

By- AYUSH TIWARI BCA(2024-27)

You might also like