codescracker


c

C Program to Reverse Array



« Previous Program Next Program »


Reverse Array in C

To reverse an array in C programming, you have to ask to the user to enter the array size then array elements. Now start swapping the array elements. Make a temp variable of same type. Place first element in the temp, then last element in the first, then temp in the last and so on.

C Programming Code to Reverse an Array

Following C program ask to the user to enter the size of the array then ask to enter the array elements to make the array reverse, then display the reverse of the array on the screen:

/* C Program - Reverse an Array */
		
#include<stdio.h>
#include<conio.h>
void main()
{
	clrscr();
	int arr[50], size, i, j, temp;
	printf("Enter array size : ");
	scanf("%d",&size);
	printf("Enter array elements : ");
	for(i=0; i<size; i++)
	{
		scanf("%d",&arr[i]);
	}
	j=i-1;  // now j will point to the last element
	i=0;   //  and i will be point to the first element
	while(i<j)
	{
		temp=arr[i];
		arr[i]=arr[j];
		arr[j]=temp;
		i++;
		j--;
	}
	printf("Now the Reverse of the Array is : \n");
	for(i=0; i<size; i++)
	{
		printf("%d  ",arr[i]);
	}
	getch();
}

When the above c program is compile and executed, it will produce the following output:

c program to reverse array

Same Program in Other Programming Language

You may also like same program in other programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test