0% found this document useful (0 votes)
112 views3 pages

Java Code Examples for Beginners

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
112 views3 pages

Java Code Examples for Beginners

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

1. Program to find maximum number in an array.

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

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


{
if(arr[i]>max)
{
max = arr[i];
}
}
[Link](max);
}
}

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

import [Link];
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<[Link];i++)
{
if(arr[i]>max)
{
second_max = max;
max = arr[i];
}
else if(arr[i]>second_max && arr[i] != max)
{
second_max = arr[i];
}
}
[Link](second_max);
}
}

3. Reverse a String

import [Link];
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
String a = [Link]();
String b = "";
for(int i=0; i<[Link]();i++)
{
b+=[Link]([Link]()-i-1);
}
[Link](b);
}
}
(or)

import [Link];
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
String a = [Link]();
String b = "";
for(int i=[Link]()-1; i>=0;i--)
{
b+=[Link](i);
}
[Link](b);
}
}

4. Palindrome

import [Link];
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
String a = [Link]();
String b = "";
for(int i=[Link]()-1; i>=0;i--)
{
b+=[Link](i);
}
if([Link](b)) // [Link](b) if cases need to be ignored
{
[Link]("Palindome");
}
else
{
[Link]("Not a Palindome");
}
[Link](b);
}
}

5. sum of digits
import [Link];
class Main{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
int a = [Link]();
String b = [Link](a);
int sum = 0;
for(int i=0; i<[Link]();i++)
{
sum += [Link]([Link]([Link](i)));
}
[Link](sum);
}
}
[Link] number

import [Link];

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

You might also like