C Program to Reverse Elements of a 1D Array (1D Array के Elements को Reverse करने का C प्रोग्राम)

Reversing an array means changing the order of its elements so that the first element becomes the last and the last element becomes the first.

Array को Reverse करने का अर्थ है उसके Elements का क्रम उलट देना। इसमें पहला Element अंतिम स्थान पर और अंतिम Element पहले स्थान पर आ जाता है।

C Program

#include <stdio.h>

#define N 10

int main()
{
    int list[N], i, j, temp;

    printf("Enter elements of List\n");

    for (i = 0; i < N; i++)
    {
        printf("%d element = ", i + 1);
        scanf("%d", &list[i]);
    }

    printf("List Array is:\n");

    for (i = 0; i < N; i++)
    {
        printf("%d ", list[i]);
    }

    /* Reverse the array */
    for (i = 0, j = N - 1; i < j; i++, j--)
    {
        temp = list[i];
        list[i] = list[j];
        list[j] = temp;
    }

    printf("\nReversal of List Array is:\n");

    for (i = 0; i < N; i++)
    {
        printf("%d ", list[i]);
    }

    return 0;
}

Sample Output (उदाहरण आउटपुट)

Enter elements of List
1 element = 10
2 element = 20
3 element = 30
4 element = 40
5 element = 50
6 element = 60
7 element = 70
8 element = 80
9 element = 90
10 element = 100

List Array is:
10 20 30 40 50 60 70 80 90 100

Reversal of List Array is:
100 90 80 70 60 50 40 30 20 10

Explanation (व्याख्या)

1. Swapping Elements (Elements की अदला-बदली)

temp = list[i];
list[i] = list[j];
list[j] = temp;

The temp variable temporarily stores an element while the first and last elements are swapped.

temp Variable एक Element को अस्थायी रूप से Store करता है। इसकी सहायता से Array के पहले और अंतिम Elements की अदला-बदली की जाती है।

2. Reverse Logic (Reverse करने का Logic)

for (i = 0, j = N - 1; i < j; i++, j--)

The variable i moves forward from the beginning, while j moves backward from the end. The loop stops when both meet.

i शुरुआत से आगे बढ़ता है और j अंत से पीछे आता है। जब दोनों बीच में मिल जाते हैं, तो Loop समाप्त हो जाता है।

This program reverses the elements of a 1D array by swapping elements from both ends.

यह प्रोग्राम दोनों सिरों के Elements की अदला-बदली करके 1D Array को Reverse करता है।

Comments

Popular posts from this blog

C Language Topics in Hindi and English

Complete C Programming Syllabus | Advanced C Roadmap for Students

Top High-Paying Tech Skills to Learn in 2025