Write a java program to increment every element of the array by one

Write a java program to increment every element of the array by one. This is a Java program that prints the incremented array and increments each element of the array by one. Enter the array’s size, followed by all of the array’s elements. Using the for loop, we can now increment all of the array’s elements. Here is the Java source code for incrementing every element of an array by one and printing the incremented array.

import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
int n, i = 0;
Scanner s = new Scanner(System.in);
System.out.print("Enter no of elements you want in array");
n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter all the elements");
for(i = 0; i < n; i++)
{
a[i] = s.nextInt();
a[i]++;
}
System.out.print("Elements of array after increment by 1");
for(i = 0; i < n - 1; i++)
{
System.out.print(a[i]+",");
}
System.out.print(a[n-1]);
}
}

 

Output:

Enter no of elements you want in array2
Enter all the elements
45
34
Elements of array after increment by 146,35

How to increment the array in java program

To modify the size, build a new array of the appropriate size, copy elements from the old array to the new array, then utilise the new array. Arr can only hold int values in our case. In contrast to ArrayList, which can only carry object values, arrays can hold primitive values.