Practical-21
Write a program using pointers to read an array of integers and print its elements in reverse order.
Introduction
In this program, we will learn to write a program using pointers to read an array of integers and print its elements in reverse order.
Algorithm
- Start
- Declare an array of size 100.
- Declare two integer variables.
- Input the size of the array.
- Scan the size of the array.
- Read the elements of the array.
- Scan the elements of the array.
- Print the elements of the array in reverse order.
- End
Code
Practical-21.c
#include <stdio.h> // Include the library to use printf() and scanf()
void main()
{
int arr[100]; // Declare an array of size 100
int i, size; // Declare two integer variables
printf("Enter the size of array: "); // This will ask the user to enter the size of the array
scanf("%d", &size); // Store the value in size
// read elements of the array
printf("Enter the elements of the array: ");
for (i = 0; i < size; i++)
{
scanf("%d", &arr[i]); // Store the values in the array
}
// print elements of the array in reverse
printf("The elements of the array in reversed order: \n");
for (i = size - 1; i >= 0 ; i--)
{
printf("%d\n", arr[i]); // print each element
}
}
Output
Enter the size of array: 5
Enter the elements of the array: 1
2
3
4
5
The elements of the array in reversed order:
5
4
3
2
1
Explanation
The program first asks the user to enter the size of the array. Then it asks the user to enter the elements of the array. Then it prints the elements of the array in reverse order.