0% found this document useful (0 votes)
4 views6 pages

Programming in Java Practical Programs

The document contains multiple Java programs demonstrating various functionalities such as finding the largest of 'n' natural numbers, checking if a number is prime, displaying a Fibonacci series, determining if a number is odd or even, and checking if a number is a palindrome. Each program includes a class definition, methods for the specific tasks, and a main method for execution. Example outputs are provided for each program to illustrate their functionality.

Uploaded by

evn.vamshi
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)
4 views6 pages

Programming in Java Practical Programs

The document contains multiple Java programs demonstrating various functionalities such as finding the largest of 'n' natural numbers, checking if a number is prime, displaying a Fibonacci series, determining if a number is odd or even, and checking if a number is a palindrome. Each program includes a class definition, methods for the specific tasks, and a main method for execution. Example outputs are provided for each program to illustrate their functionality.

Uploaded by

evn.vamshi
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

1.

Write a java program to find the largest of ‘n’


natural numbers.

// Java Program to find the largest element in

// array using iterative approach

class Sun

// Array declared

static int arr[] = {20, 10, 20, 4, 100};

// Method to find maximum in arr[]

static int largest()

// Initialize maximum element

int max = arr[0];

// Traversing and comparing max element

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

// If current element is greater than max

if (arr[i] > max)

// Then update max element

max = arr[i];

return max;

public static void main(String[] args)


{

[Link](largest());

Out put :

100

2. Write a java program to find whether a given


number is prime or not

class Sun

static boolean isPrime(int n)

// Corner case

if (n <= 1)

return false;

// Check from 2 to n-1

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

if (n % i == 0)

return false;

return true;

// Driver Program
public static void main(String args[])

if (isPrime(11))

[Link](" true");

else

[Link](" false");

if (isPrime(15))

[Link](" true");

else

[Link](" false");

3. Write a menu driven program for following:


Display a Fibonacci series and Compute Factorial
of a number
import [Link].*;

class Sun

// Function to print N Fibonacci Number

static void Fibonacci(int N)

int n1 = 0, n2 = 1;

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

// Print the number

[Link](n1 + " ");

// Swap
int n3 = n2 + n1;

n1 = n2;

n2 = n3;

// Driver Code

public static void main(String args[])

// Given Number N

int N = 10;

// Function Call

Fibonacci(N);

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

4. Write a Java Program to check if a Given Integer is


Odd or Even

class Root {

public static void main(String[] args) {

int num = 10;

if (num % 2 == 0) {

[Link]("Entered Number is Even");


} else {

[Link]("Entered Number is Odd");

Out put:
Entered Number is Even

5. write a java program to check whether the given number is palindrome or not

class Data

// Iterative function to

// reverse the digits of number

static int reversNumber(int n)

int reversed_n = 0;

while (n > 0) {

reversed_n = reversed_n * 10 + n % 10;

n = n / 10;

return reversed_n;

// Main function

public static void main(String[] args)

int n = 123464321;

int reverseN = reversNumber(n);


[Link]("Reverse of n = " + reverseN);

// Checking if n is same

// as reverse of n

if (n == reverseN)

[Link]("Palindrome = Yes");

else

[Link]("Palindrome = No");

Out put :

Reverse of n = 123464321
Palindrome = Yes

You might also like