Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DSA Tutorial
  • Data Structures
  • Algorithms
  • Array
  • Strings
  • Linked List
  • Stack
  • Queue
  • Tree
  • Graph
  • Searching
  • Sorting
  • Recursion
  • Dynamic Programming
  • Binary Tree
  • Binary Search Tree
  • Heap
  • Hashing
  • Divide & Conquer
  • Mathematical
  • Geometric
  • Bitwise
  • Greedy
  • Backtracking
  • Branch and Bound
  • Matrix
  • Pattern Searching
  • Randomized
Open In App
Next Article:
C Program to Traverse an Array in Reverse
Next article icon

C Program to Traverse an Array in Reverse

Last Updated : 26 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Write a C program to traverse a given array in reverse order that contains N elements.

Examples

Input: arr[] = {2, -1, 5, 6, 0, -3}
Output: -3 0 6 5 -1 2

Input: arr[] = {4, 0, -2, -9, -7, 1}
Output: 1 -7 -9 -2 0 4

Different Ways to Traverse an Array in Reverse Order in C

We can traverse/print the array in the reverse direction using the following different methods in C:

1. Using a Loop

The most straightforward method to traverse an array in reverse is by using a loop. This involves iterating from the last index (N - 1) to the first index (0).

C Program to Traverse an Array in Reverse Order Using a Loop

C
// C Program to Traverse an Array in Reverse Order Using a Loop #include <stdio.h>  int main() {     int arr[] = {2, -1, 5, 6, 0, -3};     int N = sizeof(arr) / sizeof(arr[0]);      // Loop that goes from N - 1 to 0     for (int i = N - 1; i >= 0; i--)     {         printf("%d ", arr[i]);     }      return 0; } 

Output
-3 0 6 5 -1 2 

Time Complexity: O(N)
Auxiliary Space: O(1)

2. Using Recursion

Recursion can also be used to traverse an array in reverse order. It is less efficient than looping due to the overhead of function calls and additional memory usage.

Below is the approach to use recursion to traverse the array in revese:

  • Define a recursive function that takes the array and the current index as arguments.
  • The base case is when all elements are traversed.
  • Recursively call the function for the next element until the first is reached.
  • Print the current element after the recursive call.

C Program to Traverse an Array in Reverse Order Using Recursion

C
// C Program to Traverse an Array in Reverse Order Using Recursion #include <stdio.h>  void traverseReverseRecursive(int arr[], int N) {     if (N <= 0) {         return;     }      // Print the current element after recursive call     printf("%d ", arr[N - 1]);     traverseReverseRecursive(arr, N - 1); }  int main() {     int arr[] = {2, -1, 5, 6, 0, -3};     int N = sizeof(arr) / sizeof(arr[0]);      // Traverse and print the array in reverse order using recursion     traverseReverseRecursive(arr, N);      return 0; } 

Output
-3 0 6 5 -1 2 

Time Complexity: O(N)
Auxiliary Space: O(N), due to the recursive stack usage.



Next Article
C Program to Traverse an Array in Reverse

A

abhishekcpp
Improve
Article Tags :
  • C Programs
  • C Language
  • DSA
  • C-Arrays
  • C Basic Programs

Similar Reads

    C Program to Traverse an Array
    Write a C program to traverse the given array that contains N number of elements.ExamplesInput: arr[] = {2, -1, 5, 6, 0, -3} Output: 2 -1 5 6 0 -3Input: arr[] = {4, 0, -2, -9, -7, 1} Output: 4 0 -2 -9 -7 1Different Ways to Traverse an Array in CArrays are versatile data structures and C language pro
    3 min read
    C Program to Traverse a Multi-Dimensional Array
    Write a C program to traverse a given multi-dimensional array that contains N elements.ExamplesInput: arr[2][3] = {{1, 2, 3}, {4, 5, 6}}Output: 1 2 3 4 5 6Input: arr[3][2] = {{-1, -2}, {0, 3}, {5, 7}}Output: -1 -2 0 3 5 7Different Ways to Traverse a Multi-Dimensional Array in CThe most common method
    3 min read
    C Program for Reversal algorithm for array rotation
    Write a function rotate(arr[], d, n) that rotates arr[] of size n by d elements. Example: Input: arr[] = [1, 2, 3, 4, 5, 6, 7] d = 2 Output: arr[] = [3, 4, 5, 6, 7, 1, 2] Rotation of the above array by 2 will make array Algorithm : rotate(arr[], d, n) reverse(arr[], 1, d) ; reverse(arr[], d + 1, n);
    3 min read
    Reverse Array in C
    Reversing an array means the swapping last element and first element, second last element and second element, and so on. In this article, we will learn how to reverse an array in C.The simplest method to reverse an array in C program is by using two pointers: one starting at the beginning (left) and
    3 min read
    C Program to Sort an Array in Ascending Order
    Sorting an array in ascending order means arranging the elements in the order from smallest element to largest element.The easiest way to sort an array in C is by using qsort() function. This function needs a comparator to know how to compare the values of the array. Let's look at a simple example:C
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences