Reverse an element using One Dimensional Array in C


This program is a simple C program that demonstrates the concept of reversing an array. The program starts by asking the user to enter the limit of the array. The user is then prompted to enter the values of the array, which are stored in the variable 'a' using a for loop.

The program then prints out the given values of the array using another for loop. After this, the program uses another for loop to reverse the array. The loop starts by initializing the variable 'i' to 0, and it continues running until the value of 'i' is less than the limit of the array.

In each iteration, the value of a[n-i-1] is stored in the variable 'b' at the same position as 'i'. This effectively reverses the order of the elements in the array. Finally, the program prints out the updated values of the array using another for loop, which shows the reversed array.

Source Code

#include<stdio.h>
int main()
{
  int i,j,n,a[10],m,o,b[10];
  printf("\nEnter the Limit:");
  scanf("%d",&n);
  printf("\nEnter the values:");
  for(i=0;i<n;i++)
  {
    scanf("%d",&a[i]);
  }
  printf("\nGiven values are:");
  for(i=0;i<n;i++)
  {
    printf("\na[%d]=%d",i,a[i]);
  }
  for(i=0;i<n;i++)
  {
    b[i]=a[n-i-1];
  }
  printf("\nUpdated values are :");
  for(i=0;i<n;i++)
  {
    printf("\na[%d]=%d",i,b[i]);
  }
  return 0;
}
         
To download raw file Click Here

Output

Enter the Limit:5
Enter the values:1
2
3
4
5
Given values are:
a[0]=1
a[1]=2
a[2]=3
a[3]=4
a[4]=5
Updated values are:
a[0]=5
a[1]=4
a[2]=3
a[3]=2
a[4]=1

List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C