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

Java Practical Word fILE

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Java Practical Word fILE

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Palindrome program ---

import java.util.Scanner;
public class Main {
// Function to check if a number is palindrome
public static boolean isPalindrome(int num) {
String numStr = Integer.toString(Math.abs(num));
// Convert number to string
return isPalindrome(numStr); // Delegate to string palindrome
check
}
// Function to check if a string is palindrome
public static boolean isPalindrome(String str) {
StringBuilder reversedStr = new
StringBuilder(str).reverse();
return str.equals(reversedStr.toString());
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Check if there is input
if (scanner.hasNext()) {
String input = scanner.next();
// Check if input is an integer
try {
int number = Integer.parseInt(input);
// Check if the number is palindrome
if (isPalindrome(number)) {
System.out.println("palindrome");
} else {
System.out.println("Notpalindrome");
}
} catch (NumberFormatException e) {
// If input is not an integer, check if it's a string
palindrome
if (isPalindrome(input)) {
System.out.println("palindrome");
} else {
System.out.println("Notpalindrome");
}
}
} else {
// If no input is provided
System.out.println("Notpalindrome");
}
scanner.close();
}
}
Armsrtong Checker –

import java.util.Scanner;
public class Main {
// Function to check if a number is an Armstrong number
public static boolean isArmstrong(int number) {
// Calculate the number of digits in the number
int originalNumber = number;
int numDigits = String.valueOf(number).length();
// Calculate the sum of each digit raised to the power of
numDigits
int sumOfDigits = 0;
while (number > 0) {
int digit = number % 10;
sumOfDigits += Math.pow(digit, numDigits);
number /= 10;
}
// Check if the sum of digits is equal to the original number
return sumOfDigits == originalNumber;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int number = scanner.nextInt();
if (isArmstrong(number)) {
System.out.println("Armstrong");
} else {
System.out.println("Not Armstrong");
}
scanner.close();
}
}
Print I to N ----

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input the value of N
int N = scanner.nextInt();
// Loop to print even numbers from 1 to N
for (int i = 1; i <= N; i++) {
// Check if the current number i is even
if (i % 2 == 0) {
System.out.print(i);
}
}
System.out.println(); // Print a new line after the loop
scanner.close();
}
}
Fibbonacci Number –

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Prompt user to enter the value of N
int N = scanner.nextInt();
// Close the scanner
scanner.close();
// Print Fibonacci numbers up to N using recursion
for (int i = 0; i < N; i++) {
System.out.print(fibonacci(i));
}
System.out.println(); // Print a newline at the end
}
// Recursive function to calculate Fibonacci number
public static int fibonacci(int n) {
if (n <= 1) {
return n;
}
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
Largest Array –

// Import ArrayList class from java.util package


import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Create an ArrayList to store the elements dynamically
ArrayList<Integer> arrayList = new ArrayList<>();
// Read input until a non-integer is entered
while (scanner.hasNextInt()) {
int num = scanner.nextInt();
arrayList.add(num);
}
// Close the scanner
scanner.close();
// Handle case where no integers were entered
if (arrayList.isEmpty()) {
System.out.println("No elements entered.");
return;
}
// Initialize max with the first element of the array
int max = arrayList.get(0);
// Iterate through the ArrayList starting from the second
element
for (int i = 1; i < arrayList.size(); i++) {
// If current element is greater than max, update max
if (arrayList.get(i) > max) {
max = arrayList.get(i);
}
}
// Print the largest element
System.out.println(max);
}
}

You might also like