Top 30+ Simple Java Programs Examples With Output

Java programing

Programs Based on Numeric Array

Here we will have some great scenario where array is useful to store the data and display when required.Playing with arrays and getting the desired result is a role of a passionate coder. Array makes the environment grouped when large amount of data to be stored.

Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same type.An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.Here we will see different array programs.

1.Write a program to convert the given decimal number in to binary number.

import java.io.*;
import java.util.*;
class DecimaltoBinary {
public static void main(String args[]) {
int index = 0;
Scanner sc = new Scanner(System.in);
System.out.println("\n Enter the decimal number you want to convert :");
int number = sc.nextInt();
int[] Binary = new int[number];
while (number > 0) {
Binary[index] = number % 2;
number = number / 2;
index++;
}
System.out.println("\n Binary of given decimal number is :");
for (int i = index - 1; i >= 0; i--) {
System.out.print(Binary[i]);
}
}
}
Output:
Enter the decimal number you want to convert :
12
Binary of given decimal number is :
1100

2.Write a program to delete an element from array.

import java.io.*;
import java.lang.*;
import java.util.*;
class deletearrayelement {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("\n Enter size of array \n");
int n = sc.nextInt();
int a[] = new int[n + 1];
System.out.println("\n Enter array elements \n");
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
System.out.println("\n\n Array elements are : \n");
for (int i = 0; i < n; i++) {
System.out.print(a[i] + "\t");
}
System.out.println("\n\n Enter the position of element to be deleted");
int position = sc.nextInt();
position = position - 1;
int k = 0;
for (int i = 0; i < n; i++) {
if (i != position) {
a[k] = a[i];
k++;
}
}
System.out.println("\n Array elements are : \n");
for (int i = 0; i < k; i++) {
System.out.print(a[i] + "\t");
}
}
}
Output:
Enter size of array 
3
Enter array elements 
9
24
8
Array elements are : 
9 24 8	
Enter the position of element to be deleted
2
Array elements are : 
9 8

3.Write a program to insert an element in array.

import java.io.*;
import java.lang.*;
import java.util.*;
class insert {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("\n Enter size of array \n");
int n = sc.nextInt();
int a[] = new int[n + 1];
System.out.println("\n Enter array elements \n");
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
System.out.println("\n\n Array elements are : \n");
for (int i = 0; i < n; i++) {
System.out.print(a[i] + "\t");
}
System.out.println("\n\n Enter the element to be inserted ");
int element = sc.nextInt();
System.out.println("\n\n Enter the position where " + element + " is to be     inserted ");
int position = sc.nextInt();
position = position - 1;
for (int i = n - 1; i >= position; i--) {
a[i + 1] = a[i];
}
a[position] = element;
System.out.println("\n Array elements are : \n");
for (int i = 0; i < n + 1; i++) {
System.out.print(a[i] + "\t");
}
}
}
Output:
Enter size of array 
3
Enter array elements 
15
2
9
Array elements are : 
15 2 9	
Enter the element to be inserted 
5
Enter the position where 5 is to be inserted 
1
Array elements are : 
5 15 2 9

4.Write a program to remove consecutive duplicates from an array.

import java.io.*;
import java.lang.*;
import java.util.*;
class removeconsecutive {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("\n Enter size of array");
int n = sc.nextInt();
int a[] = new int[n];
int k = 1, element = 0;
System.out.println("\n Enter array elements");
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
element = a[0];
for (int i = 1; i < n; i++) {
if (a[i] != element) {
a[k] = a[i];
element = a[i];
k++;
}
}
System.out.println("\n Array without consecutive duplicate \n");
for (int i = 0; i < k; i++) {
System.out.print(a[i] + "\t");
}
System.out.println("\n");
}
}
Output:
Enter size of array
5
Enter array elements
12
56
12
12
36
Array without consecutive duplicate 
12 56 12 36	

5.Write a program to print array without duplicate numbers.

import java.io.*;
import java.lang.*;
import java.util.*;
class removeduplicate {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("\n Enter size of array");
int n = sc.nextInt();
int a[] = new int[n];
int b[] = new int[n];
int k = 0, flag = 1;
System.out.println("\n Enter array elements");
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < k; j++) {
if (a[i] == b[j]) {
flag = 0;
break;
} else {
flag = 1;
}
}
if (flag == 1) {
b[k] = a[i];
k++;
}
}
System.out.println("\n Array without duplicates : \n");
for (int i = 0; i < k; i++) {
System.out.print(b[i] + "\t");
}
System.out.println("\n");
}
}
Output:
Enter size of array
3
Enter array elements
25
35
25
Array without duplicates : 
25 35	

6.Write a program to search an element in an array.

import java.io.*;
import java.lang.*;
import java.util.*;
class arraysearch {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("\n Enter size of array");
int n = sc.nextInt();
int a[] = new int[n];
System.out.println("\n Enter array elements");
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
System.out.println("\n Enter an element to be searched ");
int ele = sc.nextInt();
int k = 0;
String location = "";
for (int i = 0; i < n; i++) {
if (a[i] == ele) {
if (k == 0) {
location = location + (i + 1);
} else {
location = location + "," + (i + 1);
}
k++;
}
}
if (k > 0) {
System.out.println("\n Element " + ele + " is present at positions : " + location);
} else {
System.out.println("\n Element " + ele + " not found");
}
}
}
Output:
Enter size of array
4
Enter array elements
2
8
31
24
Enter an element to be searched 
2
Element 2 is present at positions :1

7.Write a program to find series of factorial number.

import java.io.*;
import java.lang.*;
import java.util.*;
import java.math.*;
class factorialseries {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("\n Enter a number to which you want to print factorials of every number");
int n = sc.nextInt();
BigInteger fact = BigInteger.ONE;
System.out.println("\n");
for (int i = 1; i <= n; i++) {
fact = BigInteger.ONE;
for (int j = 1; j <= i; j++) {
fact = fact.multiply(BigInteger.valueOf(j));
}
System.out.println(fact);
}
}
}
Output:
Enter a number to which you want to print factorials of every number
5
1
2
6
24
120

8.Write a program to sort an array in descending order.

import java.io.*;
import java.lang.*;
import java.util.*;
class descending {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("\n Enter size of array");
int n = sc.nextInt();
int a[] = new int[n];
System.out.println("\n Enter array elements \n");
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (a[i] < a[j]) {
int t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
System.out.println("\n Array elements in descending order: \n");
for (int i = 0; i < n; i++) {
System.out.println(a[i]);
}
}
}
Output:
Enter size of array
4
Enter array elements 
5
9
8
7
Array elements in descending order: 
9
8
7
5

9.Write a program to sort an array in ascending order.

import java.io.*;
import java.lang.*;
import java.util.*;
class ascending {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("\n Enter size of array");
int n = sc.nextInt();
int a[] = new int[n];
System.out.println("\n Enter array elements \n");
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (a[i] > a[j]) {
int t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
System.out.println("\n Array elements in ascending order: \n");
for (int i = 0; i < n; i++) {
System.out.println(a[i]);
}
}
}
Output:
Enter size of array
4
Enter array elements 
5
1
9
8
Array elements in ascending order: 
1
5
8
9

Leave a Reply

Your email address will not be published. Required fields are marked *