0% found this document useful (0 votes)
6 views

Java_Practical_Answers

The document contains several Java programs demonstrating basic programming concepts. These include reversing a string, swapping two numbers, checking for prime numbers, generating a Fibonacci series, method overloading, finding the second-highest number in an array, and checking for Armstrong numbers. Each program is accompanied by its respective code implementation.

Uploaded by

rp377156
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)
6 views

Java_Practical_Answers

The document contains several Java programs demonstrating basic programming concepts. These include reversing a string, swapping two numbers, checking for prime numbers, generating a Fibonacci series, method overloading, finding the second-highest number in an array, and checking for Armstrong numbers. Each program is accompanied by its respective code implementation.

Uploaded by

rp377156
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
You are on page 1/ 4

1. Write a Java Program to reverse a string without using the String inbuilt function.

Code:

public class ReverseString {

public static void main(String[] args) {

String str = "Hello";

String reversed = "";

for (int i = str.length() - 1; i >= 0; i--) {

reversed += str.charAt(i);

System.out.println("Reversed String: " + reversed);

2. Write a Java Program to swap two numbers using the third variable.

Code:

public class SwapNumbers {

public static void main(String[] args) {

int a = 5, b = 10, temp;

temp = a;

a = b;

b = temp;

System.out.println("After swapping: a = " + a + ", b = " + b);

3. Write a Java Program to find whether a number is prime or not.

Code:
public class PrimeCheck {

public static void main(String[] args) {

int num = 29;

boolean isPrime = true;

for (int i = 2; i <= num / 2; i++) {

if (num % i == 0) {

isPrime = false;

break;

System.out.println(num + " is " + (isPrime ? "Prime" : "Not Prime"));

4. Write a Java Program for the Fibonacci series.

Code:

public class Fibonacci {

public static void main(String[] args) {

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

System.out.print("Fibonacci Series: " + a + ", " + b);

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

int next = a + b;

System.out.print(", " + next);

a = b;

b = next;

}
}

5. Write a Java Program for method overloading.

Code:

class Overload {

void display(int a) {

System.out.println("Integer: " + a);

void display(String b) {

System.out.println("String: " + b);

public class MethodOverloading {

public static void main(String[] args) {

Overload obj = new Overload();

obj.display(5);

obj.display("Hello");

6. Write a Java Program to find the second-highest number in an array.

Code:

import java.util.Arrays;

public class SecondHighest {

public static void main(String[] args) {

int[] arr = {12, 35, 1, 10, 34, 1};

Arrays.sort(arr);
System.out.println("Second Highest: " + arr[arr.length - 2]);

7. Write a Java Program to check Armstrong number.

Code:

public class Armstrong {

public static void main(String[] args) {

int num = 153, original = num, result = 0;

while (original != 0) {

int digit = original % 10;

result += Math.pow(digit, 3);

original /= 10;

System.out.println(num + " is " + (result == num ? "an Armstrong number" : "not an Armstrong

number"));

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy