This is a Java Program to Increment Every Element of the Array by One & Print Incremented Array.
Enter size of array and then enter all the elements of that array. Now using for loop we increment all the elements of the array.
Here is the source code of the Java Program to Increment Every Element of the Array by One & Print Incremented Array. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
import java.util.Scanner;
public class Increment_Array
{
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:
$ javac Increment_Array.java $ java Increment_Array Enter no. of elements you want in array:5 Enter all the elements: 2 5 8 6 9 Elements of array after increment by 1:3,6,9,7,10
Sanfoundry Global Education & Learning Series – 1000 Java Programs.
advertisement
advertisement
Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.
Related Posts:
- Check Java Books
- Practice BCA MCQs
- Practice Programming MCQs
- Apply for Java Internship
- Practice Information Technology MCQs