LAST UPDATED: MARCH 5, 2021
Java Program To Increment Every Element by One and print the Array
In this tutorial, we will learn how to increment every element of an array by one and then print the incremented array. But before moving forward, if you are not familiar with the concepts of the array, then do check the article Arrays in Java.
Input: Enter the Array Elements: 34 52 67 87 98 12 79 54 89
Output: 35 53 68 88 99 13 80 55 90
Program 1: Increment every Element of an Array by One and then print the Incremented Array
In this case, we will see how to increment each element by one and then print the updated element when the values are pre-defined. Pre-defined means the values are already defined in the program.
Algorithm
- Start
- Declare the array.
- Initialize the array.
- Declare a variable that will store the size of the array.
- Display the original array first.
- Use a for loop to traverse through all the elements.
- Now, increment each value by one and store it in the array.
- Again, by using a for loop display all the elements of the updated array.
- Stop.
Below is the code for the same.
The below program demonstrates how to increment each element by one and then print the updated element when the values are pre-defined in the array.
/*Java program to increment the elements of an array by one and then print the updated array*/
import java.util.*;
public class Main
{
public static void main(String args[])
{
//Define the array and its elements
int arr[]={10 ,20, 30 ,50, 40 ,60, 70, 89,71};
//Define the length of the array
int n=arr.length;
//Display the original array
System.out.println("Initial Array is :");
for(int i=0;i<n;i++)
{
System.out.print(arr[i]+" ");
}
System.out.println("");
for(int i=0;i<n;i++)
{
arr[i]=arr[i]+1; //Increment the elements by one
}
//Display the updated array
System.out.println("Updated Array is ");
for(int i=0;i<n;i++)
{
System.out.print(arr[i]+" ");
}
System.out.println("");
}
}
Initial Array is :
10 20 30 50 40 60 70 89 71
Updated Array is
11 21 31 51 41 61 71 90 72
Program 2: Increment every Element of an Array by One and then print the Incremented Array
In this case, we will see how to increment each element by one and then print the updated element when the values are user-defined that is we will ask the user to input the elements of the array.
Algorithm
- Start
- Declare a variable that will store the size of the array.
- Ask the user to input the total number of elements.
- Declare the array.
- Ask the user to input the elements of the array.
- Display the original array first.
- Use a for loop to traverse through all the elements.
- Now, increment each value by one and store it in the array.
- Again, by using a for loop display all the elements of the updated array.
- Stop.
The below program demonstrates how to increment each element by one and then print the updated element when the values are user-defined.
/*Java program to increment the elements of an array by one and then print the updated array*/
import java.util.Arrays;
import java.util.Scanner;
import java.util.*;
public class Main
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n; //Declare array size
System.out.println("Enter the total number of elements ");
n=sc.nextInt(); //Initialize array size
int arr[]=new int[n]; //Declare array
System.out.println("Enter the elements of the array ");
for(int i=0; i<n ;i++) //Initialize array
{
arr[i]=sc.nextInt();
}
//Display the original array
System.out.println("Initial Array is :");
for(int i=0;i<n;i++)
{
System.out.print(arr[i]+" ");
}
System.out.println("");
for(int i=0;i<n;i++) //Traverse through the elements of the array
{
arr[i]=arr[i]+1; //Increment the elements by one
}
//Display the updated array
System.out.println("Updated Array is ");
for(int i=0;i<n;i++)
{
System.out.print(arr[i]+" ");
}
System.out.println("");
}
}
Enter the total number of elements 10
Enter the elements of the array 2 3 4 1 5 6 11 9 87 6
Initial Array is :
2 3 4 1 5 6 11 9 87 6
Updated Array is
3 4 5 2 6 7 12 10 88 7