In the previous article, we have seen Java Program to Decrement Each Element of Array by a Specified Number
In this article we are going to see how increment array element by a specified number using Java programming language.
Java Program to Increment Each Element of Array by a Specified Number
Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.
In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.
Declaration of an array:
dataType[] arrayName; (or) //Declaring an array dataType []arrayName; (or) dataType arr[];
Instantiation of an Array:
arrayName = new datatype[size]; //Allocating memory to array
Combining both Statements in One:
dataType[] arrayName = new dataType[size] //Declaring and Instantiating array
Initialization of an Array:
arrayName[index-0]= arrayElement1 //Initializing the array ... arrayName[index-s]= arrayElementS
Combining all Statements in One:
dataType arrayName[ ]={e1,e2,e3}; //declaration, instantiation and initialization
Let’s see different ways to increment each element of array by a specified number and print the incremented array.
Method-1: Java Program to Increment Each Element of Array by a Specified Number and Print the Incremented Array By Static Initialization of Array Elements
Approach:
- Declare and initialize an array.
- Iterate over the array.
- Increment each element by a specified number.
- Print the array.
Program:
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { int arr[]={10,20,30,40,50}; int n=5; System.out.println("Original array: " + Arrays.toString(arr)); Increment(arr,n); } private static void Increment(int[] arr, int n) { for (int i = 0; i < arr.length; i++) { arr[i]=arr[i]+n; } System.out.println("Incremented array: " + Arrays.toString(arr)); } }
Output: Original array: [10, 20, 30, 40, 50] Incremented array: [15, 25, 35, 45, 55]
Method-2: Java Program to Increment Each Element of Array by a Specified Number and Print the Incremented Array By Dynamic Initialization of Array Elements
Approach:
- Take the array size from user.
- Take input of array elements as user input.
- Iterate over the array.
- Increment each element by a specified number.
- Print the array.
Program:
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { // create scanner class object Scanner sc = new Scanner(System.in); // take input from user for array size System.out.print("Enter the size of array: "); int n = sc.nextInt(); // initialize array with size n int[] arr = new int[n]; // take input from user for array elements System.out.print("Enter array elements: "); for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } System.out.print("Enter the specified number: "); int num=sc.nextInt(); System.out.println("Original array: " + Arrays.toString(arr)); Increment(arr,num); } private static void Increment(int[] arr,int num) { for (int i = 0; i < arr.length; i++) { arr[i]=arr[i]+num; } System.out.println("Incremented array: " + Arrays.toString(arr)); } }
Output: Enter the size of array: 5 Enter array elements: 10 20 30 40 50 Original array: [10, 20, 30, 40, 50] Incremented array: [15, 25, 35, 45, 55]
Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.
Related Java Programs: