31.Write a java program to number is Armstrong or not.
import java.util.Scanner;
import java.lang.Math;
public class ArmstsrongNumberExample
static boolean isArmstrong(int n)
int temp, digits=0, last=0, sum=0;
temp=n;
while(temp>0)
temp = temp/10;
digits++;
temp = n;
while(temp>0)
last = temp % 10;
sum += (Math.pow(last, digits));
temp = temp/10;
if(n==sum)
return true;
else return false;
public static void main(String args[])
int num;
Scanner sc= new Scanner(System.in);
System.out.print("Enter the limit: ");
num=sc.nextInt();
System.out.println("Armstrong Number up to "+ num + " are: ");
for(int i=0; i<=num; i++)
if(isArmstrong(i))
System.out.print(i+ ", ");
Output:
Enter the limit: 999
Armstrong Number up to 999 are:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407
32. Write a java program to find square root of a number
import java.util.Scanner;
public class FindSquareRootExample1
public static void main(String[] args)
System.out.print("Enter a number: ");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
System.out.println("The square root of "+ n+ " is: "+squareRoot(n));
public static double squareRoot(int num)
double t;
double sqrtroot=num/2;
do
t=sqrtroot;
sqrtroot=(t+(num/t))/2;
while((t-sqrtroot)!= 0);
return sqrtroot;
Output 1:
Enter a number: 12
The square root of 12 is: 3.4641016151377544
33. Write a java program to check number is prime or not.
public class PrimeExample{
public static void main(String args[]){
int i,m=0,flag=0;
int n=3;
m=n/2;
if(n==0||n==1){
System.out.println(n+" is not prime number");
}else{
for(i=2;i<=m;i++){
if(n%i==0){
System.out.println(n+" is not prime number");
flag=1;
break;
if(flag==0) { System.out.println(n+" is prime number"); }
}
Output:
3 is prime number
34. Write a java program to print all prime numbers between two numbers.
public class Prime {
public static void main(String[] args) {
int low = 20, high = 50;
while (low < high) {
boolean flag = false;
for(int i = 2; i <= low/2; ++i) {
// condition for nonprime number
if(low % i == 0) {
flag = true;
break;
if (!flag && low != 0 && low != 1)
System.out.print(low + " ");
++low;
Output
23 29 31 37 41 43 47
35. Write a java program to print array elements using array class methods.
public class Array {
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5};
for (int element: array) {
System.out.println(element);
Output
36. Write a java program to print array elements in reverse order.
public class reverseArray {
static void reverse(int a[], int n)
int[] b = new int[n];
int j = n;
for (int i = 0; i < n; i++) {
b[j - 1] = a[i];
j = j - 1;
System.out.println("Reversed array is: \n");
for (int k = 0; k < n; k++) {
System.out.println(b[k]);
public static void main(String[] args)
int [] arr = {10, 20, 30, 40, 50};
reverse(arr, arr.length);
Output
Reversed array is:
50
40
30
20
10
37. Write a java program to find the length of array.
public class Test {
public static void main(String[] args)
int[] array = new int[4];
System.out.println("The size of "
+ "the array is "
+ array.length);
Output:
The size of the array is 4
38. Write a java program to copy all elements from of one array to another.
public class CopyArray {
public static void main(String[] args) {
int [] arr1 = new int [] {1, 2, 3, 4, 5};
int arr2[] = new int[arr1.length];
for (int i = 0; i < arr1.length; i++) {
arr2[i] = arr1[i];
System.out.println("Elements of original array: ");
for (int i = 0; i < arr1.length; i++) {
System.out.print(arr1[i] + " ");
System.out.println();
System.out.println("Elements of new array: ");
for (int i = 0; i < arr2.length; i++) {
System.out.print(arr2[i] + " ");
Output:
Elements of original array
12345
Elements of new array:
12345
39. Write a java program to add array elements.
class Test {
static int arr[] = { 12, 3, 4, 15 };
static int sum()
{
int sum = 0;
int i;
for (i = 0; i < arr.length; i++)
sum += arr[i];
return sum;
public static void main(String[] args)
System.out.println("Sum of given array is "
+ sum());
Output
Sum of given array is 34
40. Write a java program to search an element in array.
public class LinearSearchExample{
public static int linearSearch(int[] arr, int key){
for(int i=0;i<arr.length;i++){
if(arr[i] == key){
return i;
return -1;
public static void main(String a[]){
int[] a1= {10,20,30,50,70,90};
int key = 50;
System.out.println(key+" is found at index: "+linearSearch(a1, key));
}
}
Output:
50 is found at index: 3
41. Write a java program to arrange array elements in ascending order.
public class SortAsc {
public static void main(String[] args) {
int [] arr = new int [] {5, 2, 8, 7, 1};
int temp = 0;
System.out.println("Elements of original array: ");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
for (int i = 0; i < arr.length; i++) {
for (int j = i+1; j < arr.length; j++) {
if(arr[i] > arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
System.out.println();
System.out.println("Elements of array sorted in ascending order: ");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
Output:
Elements of original array:
52871
Elements of array sorted in ascending order:
12578
42. Write a java program to arrange array elements in descending order.
public class SortDsc {
public static void main(String[] args) {
int [] arr = new int [] {5, 2, 8, 7, 1};
int temp = 0;
System.out.println("Elements of original array: ");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
for (int i = 0; i < arr.length; i++) {
for (int j = i+1; j < arr.length; j++) {
if(arr[i] < arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
System.out.println();
System.out.println("Elements of array sorted in descending order: ");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
Output:
Elements of original array:
52871
Elements of array sorted in descending order:
87521
43. Write a java program to find biggest element in array.
public class LargestElement_array {
public static void main(String[] args) {
int [] arr = new int [] {25, 11, 7, 75, 56};
int max = arr[0];
for (int i = 0; i < arr.length; i++) {
if(arr[i] > max)
max = arr[i];
System.out.println("Largest element present in given array: " + max);
Output:
Largest element present in given array: 75
44. Write a java program to find smallest element in array.
public class SmallestElement_array {
public static void main(String[] args) {
int [] arr = new int [] {25, 11, 7, 75, 56};
int min = arr[0];
for (int i = 0; i < arr.length; i++) {
if(arr[i] <min)
min = arr[i];
System.out.println("Smallest element present in given array: " + min);
Output:
Smallest element present in given array: 7
45. Write a java program to insert element in an array
import java.util.ArrayList;
import java.util.Arrays;
public class JavaAddElementUsingList {
public static void main(String[] args) {
Integer arr[] = {1,2,3,4,5,6};
System.out.println("Array:"+Arrays.toString(arr));
ArrayList<Integer> arrayList = new ArrayList<Integer>(Arrays.asList(arr));
arrayList.add(7);
arr = arrayList.toArray(arr);
System.out.println("Array after adding element: "+Arrays.toString(arr));
Output:
Array:[1, 2, 3, 4, 5, 6]
Array after adding element: [1, 2, 3, 4, 5, 6, 7]
46. Write a java program to delete element in an array.
import java.util.Arrays;
class GFG {
public static int[] removeTheElement(int[] arr, int index)
if (arr == null || index < 0
|| index >= arr.length) {
return arr;
int[] anotherArray = new int[arr.length - 1];
for (int i = 0, k = 0; i < arr.length; i++) {
if (i == index) {
continue;
anotherArray[k++] = arr[i];
return anotherArray;
public static void main(String[] args)
int[] arr = { 1, 2, 3, 4, 5 };
System.out.println("Original Array: "
+ Arrays.toString(arr));
int index = 2;
System.out.println("Index to be removed: " + index);
arr = removeTheElement(arr, index);
System.out.println("Resultant Array: "
+ Arrays.toString(arr));
Output
Original Array: [1, 2, 3, 4, 5]
Index to be removed: 2
Resultant Array: [1, 2, 4, 5]
48.Write a java program to print Fibonacci Series
class Main {
public static void main(String[] args) {
int n = 10, firstTerm = 0, secondTerm = 1;
System.out.println("Fibonacci Series till " + n + " terms:");
for (int i = 1; i <= n; ++i) {
System.out.print(firstTerm + ", ");
int nextTerm = firstTerm + secondTerm;
firstTerm = secondTerm;
secondTerm = nextTerm;
Output
Fibonacci Series till 10 terms:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34,
49. Write a java program to Addition of two matrixes
public class AddMatrices {
public static void main(String[] args) {
int rows = 2, columns = 3;
int[][] firstMatrix = { {2, 3, 4}, {5, 2, 3} };
int[][] secondMatrix = { {-4, 5, 3}, {5, 6, 3} };
int[][] sum = new int[rows][columns];
for(int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
sum[i][j] = firstMatrix[i][j] + secondMatrix[i][j];
}
}
System.out.println("Sum of two matrices is: ");
for(int[] row : sum) {
for (int column : row) {
System.out.print(column + " ");
System.out.println();
Output
Sum of two matrices is:
-2 8 7
10 8 6
50. Write a java program to Subtraction of two matrixes
public class Sub_Matrix
public static void main(String[] args) {
int rows, cols;
int a[][] = {
{4, 5, 6},
{3, 4, 1},
{1, 2, 3}
};
int b[][] = {
{2, 0, 3},
{2, 3, 1},
{1, 1, 1}
};
rows = a.length;
cols = a[0].length;
int diff[][] = new int[rows][cols];
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){
diff[i][j] = a[i][j] - b[i][j];
System.out.println("Subtraction of two matrices: ");
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){
System.out.print(diff[i][j] + " ");
System.out.println();
Output:
Subtraction of two matrices:
1 5 3
1 1 0
0 1 2
51. Write a java program to Multiplication of two matrixes.
public class MatrixMultiplicationExample{
public static void main(String args[]){
int a[][]={{1,1,1},{2,2,2},{3,3,3}};
int b[][]={{1,1,1},{2,2,2},{3,3,3}};
int c[][]=new int[3][3]; //3 rows and 3 columns
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
c[i][j]=0;
for(int k=0;k<3;k++)
c[i][j]+=a[i][k]*b[k][j];
}//end of k loop
System.out.print(c[i][j]+" ");
}System.out.println();//new line
}}
Output:
666
12 12 12
18 18 18
52. Write a java program to transpose a matrix.
public class Transpose {
public static void main(String[] args) {
int row = 2, column = 3;
int[][] matrix = { {2, 3, 4}, {5, 6, 4} };
display(matrix);
int[][] transpose = new int[column][row];
for(int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
transpose[j][i] = matrix[i][j];
display(transpose);
}
public static void display(int[][] matrix) {
System.out.println("The matrix is: ");
for(int[] row : matrix) {
for (int column : row) {
System.out.print(column + " ");
System.out.println();
Output
The matrix is:
2 3 4
5 6 4
The matrix is:
2 5
3 6
4 4
53. Write a recursion program in Java.
public class RecursionExample3 {
static int factorial(int n){
if (n == 1)
return 1;
else
return(n * factorial(n-1));
public static void main(String[] args) {
System.out.println("Factorial of 5 is: "+factorial(5));
}
}
Output:
Factorial of 5 is: 120
54. Write a java program to compare to strings.
public class CompareStrings {
public static void main(String[] args) {
String style = "Bold";
String style2 = "Bold";
if(style == style2)
System.out.println("Equal");
else
System.out.println("Not Equal");
Output
Equal
55. Write a java program to reverse a string.
import java.io.*;
import java.util.Scanner;
class GFG {
public static void main (String[] args) {
String str= "Geeks", nstr="";
char ch;
System.out.print("Original word: ");
System.out.println("Geeks");
for (int i=0; i<str.length(); i++)
{
ch= str.charAt(i);
nstr= ch+nstr; }
System.out.println("Reversed word: "+ nstr);
Output
Original word: Geeks
Reversed word: skeeG