﻿ C : Read n values of an array, display them in reverse order

# C Exercises: Read n number of values in an array and display it in reverse order

## C Array: Exercise-2 with Solution

Write a program in C to read n number of values in an array and display them in reverse order.

Visual Presentation: Pseudo code:

```Start
Declare n, i as integer variables
Declare nums as an integer array of size n
Read values into the array nums from the user in a loop from i=0 to n-1
Print "Array in reverse order: "
For i=n-1 to 0 Step -1
Print nums [i]
End For
End
```

Sample Solution:

C Code:

``````#include <stdio.h>

void main()
{
int i,n,a;

printf("\n\nRead n number of values in an array and display it in reverse order:\n");
printf("------------------------------------------------------------------------\n");

printf("Input the number of elements to store in the array :");
scanf("%d",&n);

printf("Input %d number of elements in the array :\n",n);
for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&a[i]);
}

printf("\nThe values store into the array are : \n");
for(i=0;i<n;i++)
{
printf("% 5d",a[i]);
}

printf("\n\nThe values store into the array in reverse are :\n");
for(i=n-1;i>=0;i--)
{
printf("% 5d",a[i]);
}
printf("\n\n");
}
```
```

Sample Output:

```Read n number of values in an array and display it in reverse order:
------------------------------------------------------------------------
Input the number of elements to store in the array :3
Input 3 number of elements in the array :
element - 0 : 2
element - 1 : 5
element - 2 : 7

The values store into the array are :
2    5    7

The values store into the array in reverse are :
7    5    2
```

Explanation:

```  printf("Input the number of elements to store in the array :");
scanf("%d",&n);

printf("Input %d number of elements in the array :\n",n);
for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&a[i]);
}

printf("\nThe values store into the array are : \n");
for(i=0;i<n;i++)
{
printf("% 5d",a[i]);
}

printf("\n\nThe values store into the array in reverse are :\n");
for(i=n-1;i>=0;i--)
{
printf("% 5d",a[i]);
}
printf("\n\n");

```

In the above code -

• The first printf statement prompts the user to input the number of elements they want to store in the array and stores it in the variable n using scanf.
• The second printf statement asks the user to input n number of elements into the array using a for loop, and stores each input in the corresponding index of the array a[i].
• The third printf statement then prints out the contents of the array in order using another for loop.
• The fourth printf statement then prints out the contents of the array in reverse order using yet another for loop, which iterates over the elements of a starting from the last element and printing each element out in reverse order.

Flowchart: C Programming Code Editor:

Improve this sample solution and post your code through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

﻿

## C Programming: Tips of the Day

What is the static function in C ?

Making a function static hides it from other translation units, which helps provide encapsulation.

helper_file.c

```int f1(int);        /* prototype */
static int f2(int); /* prototype */

int f1(int foo) {
return f2(foo); /* ok, f2 is in the same translation unit */
/* (basically same .c file) as f1         */
}

int f2(int foo) {
return 42 + foo;
}
```

main.c:

```int f1(int); /* prototype */
int f2(int); /* prototype */

int main(void) {
f1(10); /* ok, f1 is visible to the linker */
f2(12); /* nope, f2 is not visible to the linker */
return 0;
}
```

Ref : https://bit.ly/3CI9ebJ

We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook