w3resource

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.

Pictorial Presentation:

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

Sample Solution:

C Code:

#include <stdio.h>

void main()
{
   int i,n,a[100];
   
       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

Flowchart:

Flowchart: Read n number of values in an array and display it in reverse order.

C Programming Code Editor:

Improve this sample solution and post your code through Disqus.

Previous: Write a program in C to store elements in an array and print it.
Next: Write a program in C to find the sum of all elements of an array.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.

C Programming: Tips of the Day

C Programming - What is the argument for printf that formats a long?

Put an l (lowercased letter L) directly before the specifier.

unsigned long n;
long m;

printf("%lu %ld", n, m);

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