C Program to Reverse Array


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.

#include<stdio.h>
#include<conio.h>
void main()
{
    clrscr();
    int array[50], size, i, j, temp;
    printf("Enter array size : ");
    scanf("%d",&size);
    printf("Enter array elements : ");
    for(i=0; i<size; i++)
    {
        scanf("%d",&array[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=array[i];
        array[i]=array[j];
        array[j]=temp;
        i++;
        j--;
    }
    printf("Now the Reverse of the Array is : \n");
    for(i=0; i<size; i++)
    {
        printf("%d  ",array[i]);
    }
    getch();
}

Run:
 

No comments:

Post a Comment

100 C Programs with Code and Output

Program 1: C Program To Read Two Numbers And Print The Sum Of Given Two Numbers. Program 2: C Program To Read Three Numbers And Prin...