Saturday, 10 January 2015

Java Program to decrement all the elements in an array


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

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

PROGRAM :
package codingcorner.in;

import java.util.Scanner;

public class DecrementElementsArray {
 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) + "\t");
   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\nDecremented Array is:\t");
  for (i = 0; i < n; i++) {
   a[i]--;
   System.out.print(a[i] + "\t");
  }
 }
}

OUTPUT :
Java - Decrement all the elements in an array

No comments:

Post a Comment