Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
19 views

Java Program by Akshay

The document describes 6 Java programs: 1) A program to reverse a string by iterating through the string backwards and adding each character to the output. 2) A program to find the largest element in an array by iterating through the array and tracking the largest number. 3) A program to check if a string is a palindrome by reversing the string and comparing it to the original.

Uploaded by

akshay daud
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)
19 views

Java Program by Akshay

The document describes 6 Java programs: 1) A program to reverse a string by iterating through the string backwards and adding each character to the output. 2) A program to find the largest element in an array by iterating through the array and tracking the largest number. 3) A program to check if a string is a palindrome by reversing the string and comparing it to the original.

Uploaded by

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

# Java Programs :

1) Reverse String:
String input= “Akshay”;
String output = “”;
For(int i=input.length()-1;i>=0;i--)
{
Output += input.charAt(i);
}
System.out.println(output);

1. Reverse string :
Public class ReverseString {
Public static void main(String[] args) {
String input = “Hello, World!”;
String reversed = new
StringBuilder(input).reverse().toString();
System.out.println(“Original: “ + input);
System.out.println(“Reversed: “ + reversed);
}
}

2. Largest element in an Array :


Public class LargestElement {
Public static void main(String[] args) {
Int[] numbers = {5, 12, 9, 20, 14};
Int largest = numbers[0];
For (int num : numbers) {
If (num > largest) {
Largest = num;
}
}
System.out.println(“The largest number is: “ +
largest);
}
}

3. Check string is a palindrome :


Public class PalindromeCheck {
Public static void main(String[] args) {
String input = “racecar”;
String reversed = new
StringBuilder(input).reverse().toString();
Boolean isPalindrome = input.equals(reversed);
System.out.println(“Is a palindrome: “ +
isPalindrome);
}
}

4. Find Factorial of number :


Public class Factorial {
Public static void main(String[] args) {
Int num = 5;
Long factorial = 1;
For (int i = 1; i <= num; i++) {
Factorial *= i;
}
System.out.println(“Factorial of “ + num + “ is: “ +
factorial);
}
}
5. Fibonacci series :
Public class FibonacciSeries {
Public static void main(String[] args) {
Int n = 10;
Int first = 0, second = 1;
System.out.print(“Fibonacci Series: “);
For (int i = 0; i < n; i++) {
System.out.print(first + “ “);
Int next = first + second;
First = second;
Second = next;
}
}
}

6. Check if a number is prime :


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(“Is prime: “ + isPrime);
}
}

You might also like