C program to reverse element in array.

Posted by Mangesh on March 14, 2018

/* C program to reverse element of array. */

Description :

Suppose we have an integer array containing data 12, 65, 54, 34, 71 at location 0, 1, 2, 3, 4 then we need to reverse the array such as 71, 34, 54, 65, 12 are at location 0, 1, 2, ,3 ,4 respestively.

Logic :

To get required result we need to transfer element at first location to last location, element at second location to second last location and so on. Therefore the loop should run for n/2 times only.

Program :

#include<stdio.h>
#include<conio.h>
void main()
{
    int arr[20];
    int n,i,temp;
    printf("Enter total number of elements in array: ");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        printf("Enter element at %d : ",i);
        scanf("%d",&arr[i]);
    }

    for(i=0;i<n/2;i++)
    {
        temp=arr[i];
        arr[i]=arr[n-i-1];
        arr[n-i-1]=temp;
    }
    printf("\nReverse of array :\n");
    for(i=0;i<n;i++)
        printf("%d\t",arr[i]);
    getch();
}

Output :

array reverse c program output` tested in turbo c 3.2

Skills Learned :

Conditional operation
If-Else statements
Loop

Written with from Mangesh.

Related Post
1 C program to reverse element in array.
2 C program to find the smallest number in array.
3 C program to find the largest number in array.
4 C program for addition of 2D matrix.
5 C program to calculate sum of all elements in array.
Latest Post
1 C program to implement Queue using linked list.
2 C program for binary search tree (BST).
3 C program to search an element in linked list.
4 C program for postorder traversal in binary tree.
5 C program for preorder traversal in binary tree.