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

java codes

The document contains six Java programs demonstrating basic algorithms: finding the maximum number in an array, finding the second largest number, reversing a string, checking for palindromes, summing the digits of a number, and determining if a number is prime. Each program includes a main class with a method to perform the specified task and print the result. The code snippets utilize standard input and output for user interaction.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

java codes

The document contains six Java programs demonstrating basic algorithms: finding the maximum number in an array, finding the second largest number, reversing a string, checking for palindromes, summing the digits of a number, and determining if a number is prime. Each program includes a main class with a method to perform the specified task and print the result. The code snippets utilize standard input and output for user interaction.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

1. Program to find maximum number in an array.

import java.util.Scanner;
class Main {
public static void main(String[] args) {
int[] arr = {5,3,8,9,7};
int max = arr[0];

for(int i=1; i<arr.length;i++)


{
if(arr[i]>max)
{
max = arr[i];
}
}
System.out.println(max);
}
}

2. Program to find 2nd largest number in an array.

import java.util.Scanner;
class Main {
public static void main(String[] args) {
int[] arr = {5,3,8,9,7};
int max = Integer.MIN_VALUE;
int second_max = Integer.MIN_VALUE;
for(int i=0; i<arr.length;i++)
{
if(arr[i]>max)
{
second_max = max;
max = arr[i];
}
else if(arr[i]>second_max && arr[i] != max)
{
second_max = arr[i];
}
}
System.out.println(second_max);
}
}

3. Reverse a String

import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String a = sc.nextLine();
String b = "";
for(int i=0; i<a.length();i++)
{
b+=a.charAt(a.length()-i-1);
}
System.out.println(b);
}
}
(or)

import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String a = sc.nextLine();
String b = "";
for(int i=a.length()-1; i>=0;i--)
{
b+=a.charAt(i);
}
System.out.println(b);
}
}

4. Palindrome

import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String a = sc.nextLine();
String b = "";
for(int i=a.length()-1; i>=0;i--)
{
b+=a.charAt(i);
}
if(a.equals(b)) // a.equalsIgnoreCase(b) if cases need to be ignored
{
System.out.println("Palindome");
}
else
{
System.out.println("Not a Palindome");
}
System.out.println(b);
}
}

5. sum of digits
import java.util.Scanner;
class Main{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
String b = String.valueOf(a);
int sum = 0;
for(int i=0; i<b.length();i++)
{
sum += Integer.parseInt(String.valueOf(b.charAt(i)));
}
System.out.println(sum);
}
}
6.prime number

import java.util.Scanner;

class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int count = 0;
for(int i=2; i<=a;i++)
{
if(a%i==0)
{
count += 1;
}
}
if(count<=1)
{
System.out.println("Prime Number");
}
else
{
System.out.println("Not a Prime Number");
}
sc.close();
}
}

You might also like