Saturday 10 January 2015

Java Program to increment all the elements in an array

This is Java Program which increments all the elements  in the array by 1.

Here we read all the elements in the array and then increment its value by 1 and then print it again.

PROGRAM :
package codingcorner.in;

import java.util.Scanner;

public class IncrementElementsArray {
public static void main(String[] args) {
int i, n;
Scanner scan = new Scanner(System.in);
System.out.print("How many numbers? \t");
n = scan.nextInt();
int a[] = new int[n];
for (i = 0; i < n; i++) {
System.out.print("Enter number " + (i + 1));
a[i] = scan.nextInt();
}
scan.close();
System.out.print("\nOriginal array is :\t");
for (i = 0; i < n; i++)
System.out.print(a[i] + "\t");

System.out.print("\n\nIncremented Array is:\t");
for (i = 0; i < n; i++) {
a[i]++;
System.out.print(a[i] + "\t");
}
}
}

OUTPUT :
Java - Increment all the elements in an array

No comments:

Post a Comment