Q.) WAP to find reverse of an array With C program.
#include < stdio.h>
int main()
{
int arr[100], reverse[100];
int size, i, j;
printf("Enter size of the array: ");
scanf("%d", &size);
printf("\nEnter elements in array :: \n");
for(i=0; i< size; i++)
{
printf("\nEnter %d element in array :: ",i+1);
scanf("%d", &arr[i]);
}
j=0;
for(i=size-1; i>=0; i--)
{
reverse[j] = arr[i];
j++;
}
printf("\nReversed array : ");
for(i=0; i< size; i++)
{
printf(" %d ", reverse[i]);
}
getch();
}
Output
Enter size of the array: 3
Enter elements in array ::
Enter 1 element in array :: 1
Enter 2 element in array :: 2
Enter 3 element in array :: 3
Reversed array : 3 2 1
Program Explanation
Step 1 : Include header files (#include< stdio.h> and #include< conio.h>).
Step 2 : Start with main function with return type.
Step 3 : parenthesis to start and end the program { }.
Step 4 : declare variables with data type i.e, 'arr[100], reverse[100] , size, i, j' is an integer type so we use "int" data type.
Step 5 : Use output function printf() to print the output on the screen.
Step 6 : Use input function scanf() to get input from the user.
Step 7 : here, find reverse of an array , "Enter size of the array" and scanf use to scan the "size" then "Enter elements in an array" after enter element , it was excute loop and check the condition "i=size-1; i>=0; i--" the condition was true then it will enter the loop and perform expression "reverse[j] = arr[i]",after perform expression it will print "Reversed array" then excuted the next 'for loop'condition and check the condition and print the "reverse[i]." .
Step 8 : using getch() function to hold the screen.