Write a C program to increment all elements of an array by one using for loop. In this C example, we incremented each array element by one within the for loop that iterates array items.
#include <stdio.h> int main() { int Size, i; printf("Please Enter the Array size = "); scanf("%d", &Size); int arr[Size]; printf("Enter the Array %d elements : ", Size); for (i = 0; i < Size; i++) { scanf("%d", &arr[i]); } for (i = 0; i < Size; i++) { arr[i] = arr[i] + 1; } printf("\nThe Final Array After Incremented by One = "); for (i = 0; i < Size; i++) { printf("%d ", arr[i]); } printf("\n"); }
We incremented the array element in this C example while assigning the value and removed the extra for loop.
#include <stdio.h> int main() { int Size, i; printf("Please Enter the Array size = "); scanf("%d", &Size); int arr[Size]; printf("Enter the Array %d elements : ", Size); for (i = 0; i < Size; i++) { scanf("%d", &arr[i]); arr[i] = arr[i] + 1; } printf("\nThe Final Array After Incremented by One = "); for (i = 0; i < Size; i++) { printf("%d ", arr[i]); } printf("\n"); }
Please Enter the Array size = 8
Enter the Array 8 elements : 10 20 30 40 50 60 70 80
The Final Array After Incremented by One = 11 21 31 41 51 61 71 81
C program to increment all elements of an array by one using a while loop.
#include <stdio.h> int main() { int Size, i; printf("Please Enter the Array size = "); scanf("%d", &Size); int arr[Size]; printf("Enter the Array %d elements : ", Size); i = 0; while (i < Size) { scanf("%d", &arr[i]); arr[i] = arr[i] + 1; i++; } printf("\nThe Final Array After Incremented by One = "); i = 0; while (i < Size) { printf("%d ", arr[i]); i++; } printf("\n"); }
Please Enter the Array size = 5
Enter the Array 5 elements : 12 22 44 66 77
The Final Array After Incremented by One = 13 23 45 67 78