Find Largest and Smallest Number
/*
Find Largest and Smallest Number in an Array Example
This Java Example shows how to find largest and smallest number in an
array.
*/
public class FindLargestSmallestNumber {
public static void main(String[] args) {
//array of 10 numbers
int numbers[] = new int[]{32,43,53,54,32,65,63,98,43,23};
//assign first element of an array to largest and smallest
int smallest = numbers[0];
int largetst = numbers[0];
for(int i=1; i< numbers.length; i++)
{
if(numbers[i] > largetst)
largetst = numbers[i];
else if (numbers[i] < smallest)
smallest = numbers[i];
}
System.out.println("Largest Number is : " + largetst);
System.out.println("Smallest Number is : " + smallest);
}
}
/*
Output of this program would be
Largest Number is : 98
Smallest Number is : 23
*/
FindEvenOrOddNumber
/*
Even Odd Number Example
This Java Even Odd Number Example shows how to check if the given
number is even or odd.
*/
public class FindEvenOrOddNumber {
public static void main(String[] args) {
//create an array of 10 numbers
int[] numbers = new int[]{1,2,3,4,5,6,7,8,9,10};
for(int i=0; i < numbers.length; i++){
/*
* use modulus operator to check if the number is even or odd.
* If we divide any number by 2 and reminder is 0 then the number is
* even, otherwise it is odd.
*/
if(numbers[i]%2 == 0)
System.out.println(numbers[i] + " is even number.");
else
System.out.println(numbers[i] + " is odd number.");
}
}
}
/*
Output of the program would be
1 is odd number.
2 is even number.
3 is odd number.
4 is even number.
5 is odd number.
6 is even number.
7 is odd number.
8 is even number.
9 is odd number.
10 is even number.
*/
remove duplicates from an array:
import java.util.HashSet;
public class test {
static int[] array = {4, 3, 3, 4, 5, 2, 4};
static HashSet l = new HashSet();
public static void main(String ar[])
{
for(int i=0;i<array.length;i++)
{
l.add(array[i]);
}
System.out.println(l);
}}
Reverse a numbere:
import java.util.Scanner;
class ReverseNumber
{
public static void main(String args[])
{
int n, reverse = 0;
System.out.println("Enter the number to reverse");
Scanner in = new Scanner(System.in);
n = in.nextInt();
while( n != 0 )
{
reverse = reverse * 10;
reverse = reverse + n%10;
n = n/10;
}
System.out.println("Reverse of entered number is "+reverse);
}
}
Java – Reverse String without using Reverse function
package JavaPrograms;
import java.util.*;
public class ReverseString {
public static void main(String args[])
{
String original, reverse ="";
Scanner in = new Scanner(System.in);
System.out.println("Enter a string to reverse");
original = in.nextLine();
int length = original.length();
for ( int i = length - 1 ; i >= 0 ; i-- )
reverse = reverse + original.charAt(i);
System.out.println("Reverse of entered string is "+reverse);
}
}
Java – Remove Duplicate Number from Array
package JavaPrograms;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class RemoveduplicateArray {
// Remove duplicate entries from an array of long integers
public static void main(String[] args)
{
//
// A long integer array with some duplicated values
//
long [] longArray = { 11,0, 11, 22, 44, 55, 66, 77, 88, 0, 99, 77, 66, 42, 55, 66, 99, 11};
System.out.println("Original array size = " + longArray.length);
System.out.println("Original array : " +
Arrays.toString(longArray));
//
// Want to let Java convert the array to a Set, since
// the Set object won't have duplicate entries.
//
// But first...
//
// Since the Set constructor needs a List (not an array),
// "convert" the array to a List.
//
List<Long> longList = new ArrayList<Long>();
for (int i = 0; i < longArray.length; i++)
{
longList.add(longArray[i]);
}
// Now, instantiate a Set with its constructor that has a List argument
Set <Long> longSet = new HashSet<Long>(longList);
//
// Create an array of Long objects and use Set.toArray()
// to "convert" the Set to the array.
Long[] resultNoDups = new Long[longSet.size()];
longSet.toArray(resultNoDups);
// Finally, if we want an array of long ints rather
// than an array of Long objects, create the array
// and copy the values
long [] finalArray = new long[resultNoDups.length];
for (int i = 0; i < resultNoDups.length; i++) {
finalArray[i] = resultNoDups[i];
}
// Taa-daa
System.out.println("After removing duplicates : " +
Arrays.toString(finalArray));
System.out.println("Result array size = " + finalArray.length);
System.out.println();
}
}
Program to Find Prime Number
package JavaPrograms;
import java.util.*;
public class PrimeNumber {
//Note: Scanner class work with JDK1.5 or above
public static void main(String args[])
{
int n, i, res;
boolean flag=true;
System.out.println("Please Enter a No.");
Scanner scan= new Scanner(System.in);
n=scan.nextInt();
for(i=2;i<=n/2;i++)
{
res=n%i;
if(res==0)
{
flag=false;
break;
}
}
if(flag)
System.out.println(n + " is Prime Number");
else
System.out.println(n + " is not Prime Number");
}
}
Program Palindrome Series
package JavaPrograms;
import java.util.*;
public class palindrome {
public static void main(String args[])
{
String original, reverse="";
Scanner in = new Scanner(System.in);
System.out.println("Enter a string to check if it is a palindrome");
original = in.nextLine();
int length = original.length();
for ( int i = length - 1 ; i >= 0 ; i-- )
reverse = reverse + original.charAt(i);
if (original.equals(reverse))
System.out.println("Entered string is a palindrome.");
else
System.out.println("Entered string is not a palindrome.");
}
}
Integer array sorting:
package JavaExamplea;
public class sort_int_example {
public static void main(String[]args){
//Sorting integers
int a[] = {10,5,6,3};
a = sort(a);
for(int i:a)
System.out.println(i);
}
public static int[] sort(int[] a)
{
int n = a.length;
int temp;
for(int i=0;i<n-1;++i)
{
for(int j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
return a;
}
}
Program to find Fibonacci series of a given number
package JavaPrograms;
public class fabonacciSeries {
/*Write a program to find Fibonacci series of a given no.
Example :
Input - 8
Output - 1 1 2 3 5 8 13 21
*/
public static void main(String args[])
{
int num = 6; //taking no. as command line argument.
System.out.println("*****Fibonacci Series*****");
int f1=0, f2=0, f3=1, f4=0, temp=0;
for(int i=1;i<=num;i++){
System.out.print(" "+f3+" ");
temp =f1+f2-1;
f1 = f2;
f2 = f3;
f3 = f1 + f2;
f4 = f3 + temp;
}
System.out.print(" \n sum of Fabonacci series numbers "+f4+" ");
}
}
Compare Two Array Program
package JavaPrograms;
public class CompareArray {
public static void compareArrays(int[] array1, int[] array2) {
boolean b = true;
if (array1 != null && array2 != null){
if (array1.length != array2.length)
b = false;
else
for (int i = 0; i < array2.length; i++) {
if (array2[i] != array1[i]) {
b = false;
}
}
}else{
b = false;
}
System.out.println(b);
}
}
BubbleSort Program
package JavaPrograms;
import java.util.Scanner;
class BubbleSort {
public static void main(String []args) {
int n, c, d, swap;
Scanner in = new Scanner(System.in);
System.out.println("Input number of integers to sort");
n = in.nextInt();
int array[] = new int[n];
System.out.println("Enter " + n + " integers");
for (c = 0; c < n; c++)
array[c] = in.nextInt();
for (c = 0; c < ( n - 1 ); c++) {
for (d = 0; d < n - c - 1; d++) {
if (array[d] > array[d+1]) /* For descending order use < */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
System.out.println("Sorted list of numbers");
for (c = 0; c < n; c++)
System.out.println(array[c]);
}
}
Program Array to Vector
package JavaPrograms;
import java.util.*;
public class ArraytoVector {
public static void main(String[] args) {
// declare and initialize array of object of given class.
Object[] arrObject = {"Manish", new Date(), "Rajesh"};
//create list for this object array.
List list = Arrays.asList(arrObject);
// create vector for given list.
Vector vector = new Vector(list);
// access the elements of the vector.
Enumeration enume = vector.elements();
while (enume.hasMoreElements()) {
System.out.println(enume.nextElement().toString());
}
}
}
Reverse String Array
/*
Java Reverse String Array Example
This Java Reverse String Array example shows how to find sort an array of
String in Java using Arrays and Collections classes.
*/
import java.util.Collections;
import java.util.List;
import java.util.Arrays;
public class ReverseStringArrayExample {
public static void main(String args[]){
//String array
String[] strDays = new String[]{"Sunday", "Monday", "Tuesday","Wednesday"};
/*
* There are basically two methods, one is to use temporary array and
* manually loop through the elements of an Array and swap them or to use
* Arrays and Collections classes.
*
* This example uses the second approach i.e. without temp variable.
*
*/
//first create a list from String array
List<String> list = Arrays.asList(strDays);
//next, reverse the list using Collections.reverse method
Collections.reverse(list);
//next, convert the list back to String array
strDays = (String[]) list.toArray();
System.out.println("String array reversed");
//print the reversed String array
for(int i=0; i < strDays.length; i++){
System.out.println(strDays[i]);
}
}
}
/*
Output of above given Java Reverse String Array example would be
String array reversed
Wednesday
Tuesday
Monday
Sunday
*/