Skip to main content

Featured

C Program to Check Prime Number Using Efficient Logic

  Introduction A prime number is a number that has exactly two distinct positive divisors: 1 and itself. In this program, we check whether a given number is prime or not using a simple and efficient logic. This type of program is commonly used in mathematics, competitive programming, and basic algorithm learning for beginners in C programming. Problem Statement The task is to write a C program that determines whether a given integer is a prime number or not. The program takes a single integer input from the user and analyzes its divisibility. If the number has no divisors other than 1 and itself, it should be identified as a prime number; otherwise, it is not prime. This problem is important in number theory and has practical relevance in areas such as cryptography, data validation, and algorithm design.  Algorithm / Logic Explanation To check whether a number is prime, we need to verify that it is not divisible by any number other than 1 and itself. The algorithm follows a si...

How to Reverse an Array in C Programming (With Example)

 Intro

Reversing an array is one of the most basic and important problems in C programming. In this post, we will learn how to reverse array elements using a simple two pointer approach. This example is easy to understand and helps beginners improve their understanding of arrays and logical thinking.

Problem Statement 

You are given an integer value that represents the size of an array, followed by the elements of the array. The task is to reverse the order of the array elements using C programming. After reversing, the program should display the updated array. This problem helps beginners understand array traversal, swapping elements, and the use of loops in C programming.

Algorithm / Logic Explanation 

To reverse an array efficiently, we use the two-pointer technique. This approach avoids using extra memory and works directly on the given array.

Step-by-step logic:

  • First, read the size of the array (n) from the user.

  • Declare an array of size n and read all elements into it.

  • Initialize two variables:

    • left starting from index 0

    • right starting from index n - 1

  • While left is less than right:

    • Swap the elements at positions left and right.

    • Move left one step forward.

    • Move right one step backward.

  • Continue this process until both pointers meet.

  • Finally, print the reversed array.

Why this algorithm works:

  • Each swap places two elements in their correct reversed positions.

  • The loop runs only n/2 times, making it efficient.

  • No extra array is used, so memory usage is minimal.

Pseudocode:

Read n

Read array elements

left = 0, right = n - 1

while left < right

    swap(arr[left], arr[right])

    left++, right--

print array


#include <stdio.h>

int main()
{
    int n;
    printf("Enter the size of the array:\n");
    scanf("%d", &n);

    int arr[n];
    printf("Enter %d elements:\n", n);
    for(int i = 0; i < n; i++)
    {
        scanf("%d", &arr[i]);
    }

    int left = 0, right = n - 1;
    while(left < right)
    {
        int temp = arr[left];
        arr[left] = arr[right];
        arr[right] = temp;

        left++;
        right--;
    }

    printf("Array after reversing:\n");
    for(int i = 0; i < n; i++)
    {
        printf("%d ", arr[i]);
    }

    return 0;
}


Sample Input / Output

Sample Input

5
10 20 30 40 50

Sample Output
50 40 30 20 10

Explanation:
The array elements are printed in reverse order by swapping the first and last elements repeatedly using the two-pointer approach.

 Explanation of Code 

This C program reverses the elements of an array using the two-pointer technique. First, the program reads the size of the array and stores it in the variable n. Based on this size, an integer array arr is declared to hold the elements. The user then enters n elements, which are stored sequentially in the array.

Two integer variables, left and right, are used to control the reversing process. The left pointer starts from the first index of the array (0), while the right pointer starts from the last index (n - 1). These pointers move toward each other.

Inside the while loop, the condition left < right ensures that swapping continues only until the middle of the array is reached. A temporary variable temp is used to swap the elements at the left and right positions safely. After each swap, left is incremented and right is decremented, moving both pointers closer.

This process continues until all elements are placed in reverse order. Finally, a for loop is used to print the reversed array elements. This approach is efficient because it reverses the array in place without using extra memory. The time complexity is O(n), and the space complexity is O(1), making this solution optimal and beginner-friendly.

Edge Cases / Notes

  • If the array size is 0 or 1, no reversal is needed because the array remains the same.
  • Very large array sizes may cause memory issues if system limits are exceeded.
  • The program assumes valid integer input from the user.
  • This logic works only for one-dimensional arrays.
  • Input validation can be added for better robustness.

Conclusion

In this post, we learned how to reverse an array in C programming using a simple two-pointer technique. This method is efficient, easy to understand, and does not require extra memory. It is widely used in real-world applications and coding interviews.


FAQs

1. What does reversing an array mean?
Reversing an array means changing the order of elements so that the first element becomes the last, the last becomes the first, and so on.

2. What is the two-pointer technique?
The two-pointer technique uses two indices that move toward each other to solve problems efficiently, commonly used in arrays and strings.

3. Can we reverse an array without using extra memory?
Yes, the two-pointer approach reverses the array in place without using any additional array.

4. What is the time complexity of this program?
The time complexity is O(n) because each element is accessed only once.

5. Is this method suitable for interviews?
Yes, this is a standard and efficient approach frequently asked in technical interviews.


 Keywords 

  • reverse array in c

  • c program to reverse array

  • array reversal in c programming

  • two pointer technique in c

  • c array example

  • array manipulation in c

  • beginner c programming

  • c programming interview questions

  • reverse array using pointers

  • hackerank c array problems


Hashtags

#cprogramming
#clanguage
#array
#coding
#programming
#datastructures
#codingforbeginners
#learnc
#codinginterview
#1printf


Related Posts
  • Related: C Program to Reverse an Array Using Recursion

  • Related: C Program to Find the Largest Element in an Array

  • Related: C Program to Check Palindrome Number

Comments

Popular Posts

🌙