Monday, February 15, 2016

C Program to reverse the elements of an integer array.

/*=================================================================================
 **
 **  File Name     : reverseArray.c
 **  Creation Date : Mon 15 Feb 2016 07:48:58 AM IST
 **  Last Modified : Mon 15 Feb 2016 08:13:49 AM IST
 **  Compoler      : gcc
 **  Author        : Manoj Kumar Patra, manojpatra.in@gmail.com
 **  Organization  : SCIS, University of Hyderabad, India.
 **
 **===============================================================================*/

#include <stdio.h>
int main()
{
    printf("\n= = = = = = = = Reverse the Array Elements = = = = = = = = =");
    int arr[100], n, i, k, temp;
    printf("\nHow many elements you wanna insert in to array : ");
    scanf("%d", &n);
    printf("Enter the elements in to array : \n");
    for (i = 0; i < n ; i++)
    {
        scanf("%d", &arr[i]);
    }
    printf("\nThe original elements of the array are : \n");
    for (i = 0; i < n ; i++)
    {
        printf("%d\t", arr[i]);
    }
    printf("\n-----------------------------------------------------------\n");
    k = n-1;
    for(i=0; i<k; i++)
    {
        temp=arr[i];
        arr[i]=arr[k];
        arr[k]=temp;
        k--;
    }
    printf("\nThe Reversed array elements are : \n");
    for (i=0; i<n; i++)
    {
        printf("%d\t", arr[i]);
    }
    printf("\n\n= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n\n");
    return 0;
}

Output:

max@ubuntu:~/cprog$ gcc reverseArray.c -o reverseArray
max@ubuntu:~/cprog$ ./reverseArray

= = = = = = = = Reverse the Array Elements = = = = = = = = =
How many elements you wanna insert in to array : 8
Enter the elements in to array :
1
2
3
4
5
6
7
8

The original elements of the array are :
1    2    3    4    5    6    7    8   
-----------------------------------------------------------

The Reversed array elements are :
8    7    6    5    4    3    2    1   

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

No comments:

Post a Comment