C Programming Tutorial

Advertisements

Write a program to reverse an array in C



       #include<stdio.h>
       #include<conio.h>
       void main()
       {
              int arr[10]; 
              int i,size=10,temp;
              clrscr();

              for(i=0;i<size;i++)
              {
                 printf("Enter any number : "); 
                 scanf("%d",&arr[i]); 
              }
              
              printf("\nValues before reverse : "); 
              for(i=0;i<size;i++)
              printf("%d, ",arr[i]); 

              for(i=0;i<=size/2;i++)
              {
                     temp = arr[i];
                     arr[i] = arr[size-1-i];
                     arr[size-1-i] = temp;                     
              }
                            
              printf("\nValues after reverse  : "); 
              for(i=0;i<size;i++)
              printf("%d, ",arr[i]); 

              getch();              
       }

   Output : 

              Enter any number : 78
              Enter any number : 45
              Enter any number : 12
              Enter any number : 89
              Enter any number : 56
              Enter any number : 23
              Enter any number : 79
              Enter any number : 46
              Enter any number : 13
              Enter any number : 82
              
              Values before reverse : 78, 45, 12, 89, 56, 23, 79, 46, 13, 82,
              Values after reverse  : 82, 13, 46, 79, 23, 56, 89, 12, 45, 78,




Advertisement