Sunday, 9 March 2014

Program to print array element in reverse order.

Program to print array element in reverse order.

Code for Program to print array element in reverse order in C Programming

#include<conio.h>
#include<stdio.h>
#include<stdlib.h>

int main()
{
    int *ptr,i,n;
    clrscr();
    printf("Enter the no of elements:");
    scanf("%d",&n);
    ptr=(int *)malloc(sizeof(int)*n);
    if(ptr==NULL)
    {
        printf("Not enough memory");
        exit(1);
    }
    for(i=0; i<n; i++)
    {
        printf("Enter %d element : ",i+1);
        scanf("%d",&ptr[i]);
    }
    printf("Array in original order\n");
    for(i=0; i<n; i++)
    {
        printf("%d\n",ptr[i]);
    }
    printf("Array in reverse order\n");
    for(i=n-1; i>=0; i--)
    {
        printf("%d\n",ptr[i]);
    }
    getch();
    return 0;
}

No comments:

Post a Comment