Program to Reverse print array in C language

reverse-print-array-elements-in-c-language

Program Description:

Write a C Program to Reverse print array in C programming language. Please note we are not reversing the array, We are just printing it in reverse order. The program will accept all elements in the array from the user and print them in reverse order.

Example Input and Output:

Input:

Output:

Prerequisites:

It is recommended to go through the following articles to better understand the reverse array program

Reverse print array in C Program Explanation:

  1. Create a global constant named SIZE and initialize it with 10. (we can also use the macro)
  2. Declare a array named data with the size of SIZE variable. i.e 10.
  3. Let’s utilize a for loop to capture user input and store it in array elements. . To do that we need to loop through the array elements and update each array element using the scanf function – scanf("%d", &data[i]);
  4. Use another for loop to print the array in given order.
  5. To print the array in reverse order, Start the loop from the SIZE-1(i=SIZE-1) and decrement the i until it reaches zero(0). – for(i=SIZE-1; i>=0; i–)
    • At each iteration, Print the value of the data[i]

Reverse Print Array Program Algorithm:

  1. Declare an integer array data of size SIZE and an integer variable i
  2. Prompt the user to input the values for the array
  3. FOR i = 0 to SIZE-1 ( Loop )
    • Read the value for arr[i]
  4. FOR i = 0 to SIZE-1 ( Loop )
    • Print the value of arr[i]
  5. FOR i = SIZE-1 to Zero(0) ( Loop )
    • Print the value of arr[i]

Reverse print array in C Program:

Program Output:

Compile and Run the program.

reverse-print-array-in-c-program-output

Reverse Print using Array using functions in C:

In the above program, We have written all our code in the main() function. Let’s use the functions to read and print the array.

Reverse print array using Program Explanation:

  1. Create a global variable SIZE and assign the integer 5.
  2. Create a read function to read input numbers from the user. The read() function takes an integer pointer data and its size as formal arguments – void read(int *data, int size)
    • The read function uses a for loop to prompt the user for input and updates the array elements.
  3. Define print function to print the array elements in given order. The print function takes *data and size as formal arguments – void print(int *data, int size)
    • Iterate over each array element using for Loop and print it using the data[i]
  4. Define another function reversePrint to print the array in reverse order. This function also takes an integer array and it’s size as arguments.
    • The reversePrint function iterates over the array in reverse order. Starts from SIZE-1 and comes to zero(0).
    • At each iteration, Print the data[i] element.
  5. Call the read(), print(), and reversePrint() functions from the main() function.

Program Output:

Compile and run the program.

reverse-print-array-using-functions-in-c

As we can see from the above output, We are getting the expected output.

Related Programs:

  1. C Program to Calculate the LCM of two Numbers
  2. C Program to Check Palindrome Number
  3. C Program to Check 3-Digit and N-Digit Armstrong Number
  4. C Program to Generate Armstrong Numbers upto N ( User-Provided Number)
  5. C Program to Generate Armstrong Numbers between two Intervals
  6. C Program to Generate Nth Armstrong Number
  7. C Program to Generate Multiplication Tables
  8. Star and Number Pattern Programs [ Triangles, Pyramids, Rhombus, etc ]

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

1 Response

  1. […] Reverse Print Array Program in C […]

Leave a Reply