Write a program in C to read n number of values in an array and display it in reverse order.
Answers
Answer:
C Code:
#include <stdio.h>
void main()
{
int i,n,a[100];
printf("\n\nRead n number of values in an array and display it in reverse order:\n");
printf("---------------------------------------------------------\n");
printf("Input the number of elements to store in the array :");
scanf("%d",&n);
printf("Input %d number of elements in the array :\n",n);
for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&a[i]);
}
printf("\nThe values store into the array are : \n");
for(i=0;i<n;i++)
{
printf("% 5d",a[i]);
}
printf("\n\nThe values store into the array in reverse are :\n");
for(i=n-1;i>=0;i--)
{
printf("% 5d",a[i]);
}
printf("\n\n");
}
Copy
Sample Output:
Read n number of values in an array and display it in reverse order:
------------------------------------------------------------------------
Input the number of elements to store in the array :3
Input 3 number of elements in the array :
element - 0 : 2
element - 1 : 5
element - 2 : 7
The values store into the array are :
2 5 7
The values store into the array in reverse are :
7 5 2
Answer:
The C program is given below to read n number of values in an array.
Explanation:
C Programme Input:
#include <stdio.h>
int main()
{
//Initialize array
int arr[] = {1, 2, 3, 4, 5};
//Calculate length of array arr
int length = sizeof(arr)/sizeof(arr[0]);
printf("Original array: \n");
for (int i = 0; i < length; i++) {
printf("%d ", arr[i]);
}
printf("\n");
printf("Array in reverse order: \n");
//Loop through the array in reverse order
for (int i = length-1; i >= 0; i--) {
printf("%d ", arr[i]);
}
return 0;
}
Program Output:
Original array:
1 2 3 4 5
Array in reverse order:
5 4 3 2 1