Sunday, 30 August 2015

Write a C Program using pointers to read an array of integers and print its elements in reverse order

Write a C Program using pointers to read an array of integers and print its elements in reverse order
1:  #include<conio.h>  
2:  #include<stdio.h>  
3:  #include<stdlib.h>  
4:  int main()  
5:  {  
6:        int *ptr,i,n;  
7:        clrscr();  
8:        printf("Enter the no of elements:");  
9:        scanf("%d",&n);  
10:        ptr=(int *)malloc(sizeof(int)*n);  
11:        if(ptr==NULL)  
12:        {  
13:              printf("Not enough memory");  
14:              exit(1);  
15:        }  
16:        for(i=0; i<n; i++)  
17:        {  
18:              printf("Enter %d element : ",i+1);  
19:              scanf("%d",&ptr[i]);  
20:        }  
21:        printf("Array in original order\n");  
22:        for(i=0; i<n; i++)  
23:        {  
24:              printf("%d\n",ptr[i]);  
25:        }  
26:        printf("Array in reverse order\n");  
27:        for(i=n-1; i>=0; i--)  
28:        {  
29:              printf("%d\n",ptr[i]);  
30:        }  
31:        getch();  
32:        return 0;  
33:  }  

No comments:

Post a Comment