Reversing an Array Elements : C Program

Computer Science » C Program » Reversing an Array Elements

Program :

#include<stdio.h>
 
int main() 
{
   int arr[30], i, j, num, temp;
 
   printf("nEnter no of elements : ");
   scanf("%d", &num);
 
   //Read elements in an array
   for (i = 0; i < num; i++) {
      scanf("%d", &arr[i]);
   }
 
   j = i - 1;   // j will Point to last Element
   i = 0;       // i will be pointing to first element
 
   while (i < j) {
      temp = arr[i];
      arr[i] = arr[j];
      arr[j] = temp;
      i++;             // increment i
      j--;          // decrement j
   }
 
   //Print out the Result of Insertion
   printf("nArray after reversal : ");
   for (i = 0; i < num; i++) {
      printf("%d t", arr[i]);
   }
 
   return (0);
}

Output :

Enter no of elements : 5
10 20 30 40 50
Array after reversal : 50 	40 	30 	20 	10 
Subject Name : Computer Science
Exam Name : IIT GATE, UPSC ESE, RRB, SSC, DMRC, NMRC, BSNL, DRDO, ISRO, BARC, NIELIT
Posts Name : Assistant Engineer, Management Trainee, Junior Engineer, Technical Assistant
Computer Science Books

GATE 2023 Total InfoENGG DIPLOMAUGC NET Total Info
IES 2023 Total InfoPSUs 2022 Total InfoCSIR UGC NET Total Info
JAM 2023 Total InfoM TECH 2023 Total InfoRAILWAY 2022 Total Info

Related Posts

  • Find the Number of Elements in An Array : C ProgramComputer Science » C Program » Find the Number of Elements in An Array Program : [php]#include <stdio.h> int main() { int array[] = {10, 20, 30, 40, 50, 60, 70,80,90,100}; int n,m; n = sizeof(array); m = n/sizeof(int); printf("Size of the given array is : %d", m); return 0; }[/php] Output : [php]Size of the given array is : 10[/php]
    Tags: program, elements, array, computer, science, output, programs
  • Rectangle using Line and Special Symbols : C ProgramComputer Science » C Program » Rectangle using Line and Special Symbols Program : [php]#include<stdio.h> int main() { int i, j; for (i = 0; i < 10; i++) { printf("n"); for (j = 0; j < 10; j++) { if (i == 0 || i == 9 || j == 0 || j == 9) printf("▲"); else printf("-"); } } return (0); }[/php] Output : [php]▲▲▲▲▲▲▲▲▲▲ ▲--------▲ ▲--------▲ ▲--------▲ ▲--------▲ ▲--------▲ ▲--------▲ ▲--------▲ ▲--------▲ ▲▲▲▲▲▲▲▲▲▲[/php]
    Tags: program, computer, science, output, programs
  • Delete Duplicate Elements in an Array : C ProgramComputer Science » C Program » Delete Duplicate Elements in an Array  Program : [php]#include<stdio.h> int main() { int arr[20], i, j, k, size; printf("nEnter array size : "); scanf("%d", &size); printf("nAccept numbers : "); for (i = 0; i < size; i++) scanf("%d", &arr[i]); printf("nArray with unique list : "); for (i = 0; i < size; i++) { for (j = i + 1; j < size;) { if (arr[j] == arr[i]) { for (k = j; k < size; k++) { arr[k] = arr[k + 1]; } size--; } else j++; } } for (i = 0; i < size; i++) { printf("%d ", arr[i]); } return (0); }[/php] Output : [php]Enter array size : 5 Accept numbers : 1 2 3 4 3 Array with unique list : 1 2 3 4 [/php]
    Tags: program, elements, array, computer, science, output, programs
  • Calculate Addition of All Elements in Array : C ProgramComputer Science » C Program » Calculate Addition of All Elements in Array Program : [php]#include&lt;stdio.h&gt; int main() { int i, arr[50], sum, num; printf("nEnter number of elements :"); scanf("%d", &amp;num); //Reading values into Array printf("nEnter the values :"); for (i = 0; i &lt; num; i++) scanf("%d", &amp;arr[i]); //Computation of total sum = 0; for (i = 0; i &lt; num; i++) sum = sum + arr[i]; //Printing of all elements of array for (i = 0; i &lt; num; i++) printf("na[%d]=%d", i, arr[i]); //Printing of total printf("nSum of all elements : %d", sum); return (0); }[/php] Output : [php]Enter number of elements : 4 Enter the values : 10 20 30 40 a[0]=10 a[1]=20 a[2]=30 a[3]=40 Sum of all elements : 100[/php]
    Tags: program, elements, array, computer, science, output, programs
  • Calculate Sum and Average of an Array : C ProgramComputer Science » C Program » Calculate Sum and Average of an Array Program : [php]#include <stdio.h> int main() { int Arr[50]; int n, i, sum = 0; float average; printf("Enter the number of elements in Array : "); scanf("%d", &n); for (i = 0; i < n; i++) { printf("nEnter element %d : ", i + 1); scanf("%d", &Arr[i]); sum = sum + Arr[i]; } average = (float)sum/n; printf("nThe sum of the elements array is : %d", sum); printf("nThe average of the elements array is : %0.2f", average); return 0; }[/php] Output : [php]Enter the number of elements in Array : 5 Enter element 1 : 20 Enter element 2 : 30 Enter element 3 : 40 Enter element 4 : 50 Enter element 5 : 60 The sum of the elements array is : 200 The average of the elements array is : 40.00[/php]
    Tags: program, array, computer, science, output, programs

LEAVE A REPLY

Please enter your comment!
Please enter your name here