q2. wap in c to input 5 numbers in an array and display in reverse order.
Code Explanation:
Here is a C program that inputs 5 numbers into an array and displays them in reverse order:
#include <stdio.h>
int main() {
int arr[5];
int i;
// Input 5 numbers into the array
printf("Enter 5 numbers:\n");
for (i = 0; i < 5; i++) {
scanf("%d", &arr[i]);
}
// Display the numbers in reverse order
printf("Numbers in reverse order:\n");
for (i = 4; i >= 0; i--) {
printf("%d\n", arr[i]);
}
return 0;
}
Explanation of each step:
- Declare an integer array
arr
of size 5 to store the numbers. - Declare an integer variable
i
for looping. - Prompt the user to enter 5 numbers using
printf
. - Use a
for
loop to input the numbers into the array usingscanf
. The loop runs fromi = 0
toi < 5
, incrementingi
by 1 in each iteration. - After the loop, display the numbers in reverse order using another
for
loop. The loop runs fromi = 4
toi >= 0
, decrementingi
by 1 in each iteration. - Inside the loop, use
printf
to display each number on a new line. - Finally,
return 0
to indicate successful execution of the program.
This program uses a simple approach to input 5 numbers into an array and display them in reverse order.