C Program To Find Smallest Element in An Array using Recursion

Write a C program to find smallest element / number in an array using pointers and recursion.

We have covered both these logic in this video tutorial
1. Recursive function with no return type.
2. Recursive function with return type.

Related Read:
C Program To Find Smallest Element In An Array
Recursive Functions In C Programming Language
Basics of Pointers In C Programming Language
Introduction To Arrays: C Programming Language

Important Video Tutorial
C Programming: Arrays, Pointers and Functions

Example: Expected Output

Enter 5 integer numbers
5
2
6
4
3
Smallest Element In The Array: 2

Visual Representation

Smallest Element In An Array using Recursion

Video Tutorial: C Program To Find Smallest Element in An Array using Recursion


[youtube https://www.youtube.com/watch?v=sGGxml1TCBI]

YouTube Link: https://www.youtube.com/watch?v=sGGxml1TCBI [Watch the Video In Full Screen.]

Source Code: C Program To Find Smallest Element in An Array using Recursion

Method 1: With No Return Type

#include<stdio.h>

#define N 5

void smallest(int *num, int n, int small)
{
    if(n < 0)
        printf("Smallest Element In The Array: %d\n", small);
    else
    {
        if(small > *num)
            small = *num;

        smallest(++num, --n, small);
    }
}

int main()
{
    int a[N], i;

    printf("Enter %d integer numbers\n", N);
    for(i = 0; i < N; i++)
        scanf("%d", &a[i]);

    smallest(a, N - 1, a[0]);

    return 0;
}

Output:
Enter 5 integer numbers
2
1
3
4
5
Smallest Element In The Array: 1

Logic To Find Smallest Element In An Array using Recursion

We ask the user to enter N integer numbers and store it inside array variable a[N]. We pass base address(address of first element in the array) of the array, which is present in &a[0] or a, and last index of the array(indicating size of the array, from index 0), and first element of the array(assuming first element itself as smallest element).

Inside Recursive function
Base Condition: This is the condition to terminate the recursive call. Here we check if the size of the array is less than zero. If it’s less than zero, then we display the value present inside variable small, which holds the smallest element in the array.

void smallest(int *num, int n, int small)
{
    if(n < 0)
        printf("Smallest Element In The Array: %d\n", small);
    else
    {
        if(small > *num)
            small = *num;

        smallest(++num, --n, small);
    }
}

We need to take a pointer variable to accept the base address. Next we’ll have base condition. If base condition isn’t met – we check if the value present in variable small is greater than value present at *num. If it’s true, then we transfer the value of *num to small. Next we increment the address of num by 1 and decrement the value of n by 1 and pass them to the same function(recursive call) along with the value of small. This is called recursive function call.

Once value of n is less than 0, we display the value of variable small, which holds the smallest element of the array.

Important Note:

1. Array elements are always stored in contiguous memory location.
2. A pointer when incremented always points to an immediately next location of its own type.

Source Code: Find Smallest Element of An Array using Recursion: With Return Value

Method 2: With Return Type

#include<stdio.h>

#define N 5

int smallest(int num[], int n, int small)
{
    if(n < 1)
        return small;
    else
    {
        if(num[n] < small)
            small = num[n];

        return smallest(num, --n, small);
    }
}

int main()
{
    int a[N], i;

    printf("Enter %d integer numbers\n", N);
    for(i = 0; i < N; i++)
        scanf("%d", &a[i]);

    printf("Smallest Element In The Array: %d\n", smallest(a, N - 1, a[0]));

    return 0;
}

Output:
Enter 5 integer numbers
2
1
0
3
5
Smallest Element In The Array: 0

After repeatedly incrementing value of num and decrementing the value n, we’ll reach a point where value of n will be less than 0. That’s when all the comparisons end, and variable small will have smallest element of the array. This result will be returned to the calling function, which in turn returns the result to the calling function and so on ..until the result is returned to the first function call, which was from with in main method – where we print the value of variable small.

Source Code: Using Array Variable In Recursive Function

Find Smallest Element of An Array using Recursion

#include<stdio.h>

#define N 5

void smallest(int num[], int n, int small)
{
    if(n < 0)
        printf("Smallest Element In The Array: %d\n", small);
    else
    {
        if(small > num[n])
            small = num[n];

        smallest(num, --n, small);
    }
}

int main()
{
    int a[N], i;

    printf("Enter %d integer numbers\n", N);
    for(i = 0; i < N; i++)
        scanf("%d", &a[i]);

    smallest(a, N - 1, a[0]);

    return 0;
}

Output:
Enter 5 integer numbers
1
0
2
-5
4
Smallest Element In The Array: -5

Here we are taking array variable to receive the base address. We keep checking if num[n] is smaller than value present at variable small. If true, then we transfer num[n] value to variable small, and then recursively call the same function by decrementing the value of n by 1, and also pass the new value of small.

Once the value of n is less than 0, we return the value present in variable small, which holds the smallest element of the array.

Explanation With Example

N = 5;
a[N] = {5, 2, 6, 4, 3};
n = N – 1 = 5 – 1 = 4;
num = a[0] = 5;
big = a[0] = 5;

smallest(num, --n, small);
nnum[n]small
433
343
263
122
052
-12

Smallest Element in the array: 2

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Find Biggest Element of An Array using Recursion

Write a C program to find biggest element / number in an array using pointers and recursion.

We have covered both these logic in this video tutorial
1. Recursive function with no return type.
2. Recursive function with return type.

Related Read:
C Program To Find Biggest Element of An Array
Recursive Functions In C Programming Language
Basics of Pointers In C Programming Language
Introduction To Arrays: C Programming Language

Important Video Tutorial
C Programming: Arrays, Pointers and Functions

Example: Expected Output

Enter 5 integer number
5
2
6
4
3
Biggest Element in the array: 6

Visual Representation

Biggest Element In An Array using Recursion

Video Tutorial: C Program To Find Biggest Element In An Array using Recursion


[youtube https://www.youtube.com/watch?v=-Cdp8QanJv8]

YouTube Link: https://www.youtube.com/watch?v=-Cdp8QanJv8 [Watch the Video In Full Screen.]

Source Code: Find Biggest Element of An Array using Recursion: With No Return Type

Method 1: With No Return Type

#include<stdio.h>

#define N 5

void biggest(int *num, int n, int big)
{
    if(n < 0)
        printf("Biggest element is %d\n", big);
    else
    {
        if(*num > big)
            big = *num;

        biggest(++num, --n, big);
    }
}

int main()
{
    int a[N], i;

    printf("Enter %d integer numbers\n", N);
    for(i = 0; i < N; i++)
        scanf("%d", &a[i]);

    biggest(a, N - 1, a[0]);

    return 0;
}

Output:
Enter 5 integer number
1
2
3
4
5
Biggest Element in the array: 5

Logic To Find Biggest Element of An Array using Recursion

We ask the user to enter N integer numbers and store it inside array variable a[N]. We pass base address(address of first element in the array) of the array, which is present in &a[0] or a, and last index of the array(indicating size of the array, from index 0), and first element of the array(assuming first element itself as big).

Inside Recursive function
Base Condition: This is the condition to terminate the recursive call. Here we check if the size of the array is less than zero. If it’s less than zero, then we display the value present inside variable big, which holds the biggest element in the array.

void biggest(int *num, int n, int big)
{
    if(n < 0)
        printf("Biggest element is %d\n", big);
    else
    {
        if(*num > big)
            big = *num;

        biggest(++num, --n, big);
    }
}

We need to take a pointer variable to accept the base address. Next we’ll have base condition. If base condition isn’t met – we check if the value present in variable big is less than value present at *num. If it’s true, then we transfer the value of *num to big. Next we increment the address of num by 1 and decrement the value of n by 1 and pass them to the same function along with the value of big. This is called recursive function call.

Once value of n is less than 0, we display the value of variable big, which holds the biggest element of the array.

Important Note:

1. Array elements are always stored in contiguous memory location.
2. A pointer when incremented always points to an immediately next location of its own type.

Source Code: Find Biggest Element of An Array using Recursion: With Return Value

Method 2: With Return Type

#include<stdio.h>

#define N 5

int biggest(int *num, int n, int big)
{
    if(n < 0)
        return big;
    else
    {
        if(big < *num)
            big = *num;

        return biggest(++num, --n, big);
    }
}

int main()
{
    int a[N], i;

    printf("Enter %d integer number\n", N);
    for(i = 0; i < N; i++)
        scanf("%d", &a[i]);

    printf("Biggest Element in the array: %d\n", biggest(a, N - 1, a[0]));

    return 0;
}

Output:
Enter 5 integer number
1
2
5
3
4
Biggest Element in the array: 5

After repeatedly incrementing value of num and decrementing the value n, we’ll reach a point where value of n will be less than 0. That’s when all the comparisons end, and variable big will have biggest element of the array. This result will be returned to the calling function, which in turn returns the result to the calling function and so on ..until the result is returned to the first function call, which was from with in main method – where we print the value of variable big.

Source Code: Using Array Variable In Recursive Function

Find Biggest Element of An Array using Recursion

#include<stdio.h>

#define N 5

int biggest(int num[], int n, int big)
{
    if(n < 0)
        return big;
    else
    {
        if(big < num[n])
            big = num[n];

        return biggest(num, --n, big);
    }
}

int main()
{
    int a[N], i;

    printf("Enter %d integer number\n", N);
    for(i = 0; i < N; i++)
        scanf("%d", &a[i]);

    printf("Biggest Element in the array: %d\n", biggest(a, N - 1, a[0]));

    return 0;
}

Output:
Enter 5 integer number
10
56
83
978
4
Biggest Element in the array: 978

Here we are taking array variable to receive the base address. We keep checking if a[n] is biggest than value present at variable big. If true, then we transfer a[n] value to variable big, and then recursively call the same function by decrementing the value of n by 1, and also pass the new value of big.

Once the value of n is less than 0, we return the value present in variable big, which holds the biggest element of the array.

Explanation With Example

N = 5;
a[N] = {5, 2, 6, 4, 3};
n = N – 1 = 5 – 1 = 4;
num = a[0] = 5;
big = a[0] = 5;

biggest(num, --n, big);
nnum[n]big
435
345
266
126
056
-16

Biggest Element in the array: 6

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Count Positive, Negative, Even And Odd Numbers In An Array

Twenty-five numbers are entered from the keyboard into an array. Write a program to find out how many of them are positive, how many are negative, how many are even and how many odd.

Important Note
Most of the time 0 is considered as even number, as it has odd numbers on either side of it i.e., 1 and -1. And 0 is perfectly divisible by 2.

Also note that 0 is neither positive and nor negative.

number scale

Related Read:
C Program To Count Number of Even, Odd and Zeros In An Array
C Program To Count Number of Positive, Negative and Zeros In An Array

Example: Expected Output

Enter 10 integer numbers
-4
-3
-2
-1
0
1
2
3
4
5

Positive: 5
Negative: 4
Even: 4
Odd: 5
Zero: 1

Visual Representation

count positive negative even and odd elements in array

Video Tutorial: C Program To Count Positive, Negative, Even And Odd Numbers In An Array


[youtube https://www.youtube.com/watch?v=DwBgTJDWTE4]

YouTube Link: https://www.youtube.com/watch?v=DwBgTJDWTE4 [Watch the Video In Full Screen.]

Source Code: C Program To Count Positive, Negative, Even, Odd And Zero Numbers In An Array

#include<stdio.h>

#define N 10

int main()
{
    int a[N], i, pos = 0, neg = 0, even = 0, odd = 0, zero = 0;

    printf("Enter %d integer numbers\n", N);
    for(i = 0; i < N; i++)
    {
        scanf("%d", &a[i]);
        if(a[i] == 0)
        {
            zero++;
        }
        else if(a[i] > 0)
            pos++;
        else
            neg++;

        if(a[i] == 0)
        {

        }
        else if(a[i] % 2 == 0)
            even++;
        else
            odd++;
    }

    printf("\nPositive: %d\n", pos);
    printf("Negative: %d\n", neg);
    printf("Even: %d\n", even);
    printf("Odd: %d\n", odd);
    printf("Zero: %d\n", zero);

    return 0;
}

Output:
Enter 10 integer numbers
-4
-3
-2
-1
0
1
2
3
4
5

Positive: 5
Negative: 4
Even: 4
Odd: 5
Zero: 1

Here we are counting positive numbers, negative numbers, even numbers, odd numbers and zeros.

Source Code: C Program To Count Positive, Negative, Even And Odd Numbers In An Array

#include<stdio.h>

#define N 10

int main()
{
    int a[N], i, pos = 0, neg = 0, even = 0, odd = 0;

    printf("Enter %d integer numbers\n", N);
    for(i = 0; i < N; i++)
    {
        scanf("%d", &a[i]);
        if(a[i] == 0)
        {

        }
        else if(a[i] > 0)
            pos++;
        else
            neg++;

        if(a[i] % 2 == 0)
            even++;
        else
            odd++;
    }

    printf("\nPositive: %d\n", pos);
    printf("Negative: %d\n", neg);
    printf("Even: %d\n", even);
    printf("Odd: %d\n", odd);

    return 0;
}

Output:
Enter 10 integer numbers
-4
-3
-2
-1
0
1
2
3
4
5

Positive: 5
Negative: 4
Even: 5
Odd: 5

In above source code, since 0 is perfectly divisible by 2, it is considered as even number.

Logic To Count Positive, Negative, Even And Odd Numbers In An Array

We ask the user to enter N integer numbers and store it inside array variable a[N]. Next we loop through the array using for loop. For each iteration we check: if the selected number is greater than 0. If true, then its positive number, so we increment the value of pos by 1. If the number is less than 0, then its a negative number, so we increment the value of neg by 1.

Next we check if the selected number is perfectly divisible by 2. If true, then its even number, so we increment the value of variable even by 1. If the selected number is not perfectly divisible by 2, then its odd number.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Programming: Arrays, Pointers and Functions

In today’s video tutorial lets learn more about arrays and pointers, and how we can use them with functions.

Note:
This video tutorial is very important, so please make sure to watch the video till the end and make some notes. These are the basic concepts of arrays, pointers and functions. If you miss this, you’ll find other programs complicated. If you learn these basic concepts thoroughly all the programs related to using points and arrays with function will feel more straightforward. So please watch the video till the end and make note of important things I’m teaching in it. Happy Learning!

Related Read:
Basics of Pointers In C Programming Language
Introduction To Arrays: C Programming Language
Basics of Arrays: C Program
Function / Methods In C Programming Language

Important Topic
Please watch this short video without fail: C Program To Display Elements of Array In Reverse Order using Pointers

Visual Representation

arrays pointers and functions in C

Video Tutorial: C Programming: Arrays, Pointers & Functions


[youtube https://www.youtube.com/watch?v=2wdjJNfP7Hw]

YouTube Link: https://www.youtube.com/watch?v=2wdjJNfP7Hw [Watch the Video In Full Screen.]

Source Code: Simple Pointer Example

#include<stdio.h>
int main()
{
    int num = 5, *ptr;

    ptr = &num;

    printf("Value present at address %d is %d\n", ptr, *ptr);

    return 0;
}

Output:
Value present at address 6356728 is 5

Here we are assigning address of variable num to pointer variable ptr. Using ptr we display the address of num and the value present at that address.

Source Code: Simple Pointer and Array Example

#include<stdio.h>

int main()
{
    int num[5] = {1, 2, 3, 4, 5}, *ptr1, *ptr2;

    ptr1 = &num[0];
    ptr2 = num;

    printf("Base Address using &num[0]: %d\n", ptr1);
    printf("Base Address using num: %d\n", ptr2);

    printf("\n");

    printf("Value at num[0]: %d\n", *ptr1);
    printf("Value at *num: %d\n", *ptr2);
    printf("Value at *(num + 0): %d\n", *(ptr2 + 0));
    printf("Value at index 1: %d\n", *(ptr2 + 1));
    printf("Value at index 2: %d\n", *(ptr2 + 2));

    printf("\n");

    return 0;
}

Output:
Base Address using &num[0]: 6356708
Base Address using num: 6356708

Value at num[0]: 1
Value at *num: 1
Value at *(num + 0): 1
Value at index 1: 2
Value at index 2: 3

Above program proves that both &num[0] and num(array variable name) hold base address of the array. And if you add 1 to base address or any address, it’ll point to the immediately next address of its own type. Since num is of type integer all the addresses will have 4 bytes gap between them – as each address is allocated with 4 bytes of data space.

Important Note

1. Array elements are always stored in contiguous memory location.
2. A pointer when incremented always points to an immediately next location of its own type.
3. Whenever compiler finds num[i] it’ll convert it into *(num + i). Because it’s faster to work with addresses than with variables.

Source Code: Arrays and For Loop

#include<stdio.h>

int main()
{
    int num[5] = {1, 2, 3, 4, 5}, i;

    for(i = 0; i < 5; i++)
        printf("%d\n", num[i]);

    printf("\n");

    return 0;
}

Output:
1
2
3
4
5

This is how we access and print array elements – i.e., by using its index as subscript to array name.

Source Code: Address of Array Elements

#include<stdio.h>

int main()
{
    int num[5] = {1, 2, 3, 4, 5}, i;

    for(i = 0; i < 5; i++)
        printf("%d\n", num + i); // OR printf("%d\n", &num[i]);

    printf("\n");

    return 0;
}

Output:
6356712
6356716
6356720
6356724
6356728

Since array variable name num holds base address – if we add index value to it, it’ll fetch all the consecutive addresses.

Source Code: Values Associated with Address of Array Elements

#include<stdio.h>

int main()
{
    int num[5] = {1, 2, 3, 4, 5}, i;

    for(i = 0; i < 5; i++)
        printf("%d\n", *(num + i));

    printf("\n");

    return 0;
}

Output:
1
2
3
4
5

If we append * infront of a pointer variable it’ll print the value present at that address or memory location.

Note:
If we know the data type of the array and the base address of the array, we can fetch all the remaining addresses and values associated with those addresses.

Source Code: Using num[i] and [i]num Interchangeably

#include<stdio.h>

int main()
{
    int num[5] = {1, 2, 3, 4, 5}, i;

    for(i = 0; i < 5; i++)
        printf("%d\n", i[num]);

    printf("\n");

    return 0;
}

Output:
1
2
3
4
5

We can access array elements by adding index value to the base address and appending * to it. i.e., *(num + i). We can write the same thing like this *(i + num) as well. Similarly, we can write num[i] as i[num] too and it outputs the same results.

Source Code: Print Array Elements Using Pointer Variable

#include<stdio.h>
int main()
{
    int num[5] = {1, 2, 3, 4, 5}, i, *ptr;

    ptr = num; // OR ptr = &num[0];

    for(i = 0; i < 5; i++)
        printf("%d\n", *ptr++);

    printf("\n");

    return 0;
}

Output:
1
2
3
4
5

Here we’ve assigned base address to pointer variable ptr. Inside for loop, we print the value present at the current address of ptr and then increment the address by 1 – doing which it points to the next address in the array.

Source Code: Print Array Elements In Reverse Order Using Pointer Variable

#include<stdio.h>

#define N 5

int main()
{
    int num[N] = {1, 2, 3, 4, 5}, i, *ptr;

    ptr = &num[N - 1]; // OR ptr = num;

    for(i = 0; i < N; i++)
        printf("%d\n", *ptr--);

    printf("\n");

    return 0;
}

Output:
5
4
3
2
1

Here we are assigning last index elements address to the pointer variable. Inside for loop we print the value present at address ptr and then decrement the value of ptr by 1 for each iteration of for loop – doing which it points to the previous address in the array.

Source Code: Arrays, Pointers & Functions: Call By Value

#include<stdio.h>

#define N 5

void display(int x)
{
    printf("%d\n", x);
}

int main()
{
    int num[N] = {1, 2, 3, 4, 5}, i;

    for(i = 0; i < N; i++)
       display(num[i]);

    return 0;
}

Output:
1
2
3
4
5

Inside for loop we are passing value/element of array to display() method one by one for each iteration. And inside display() method we’re printing the values. This is called Call by value method.

Source Code: Arrays, Pointers & Functions: Call By Reference

#include<stdio.h>

#define N 5

void display(int *x)
{
    printf("%d\n", *x);
}

int main()
{
    int num[N] = {1, 2, 3, 4, 5}, i;

    for(i = 0; i < N; i++)
       display(&num[i]);

    return 0;
}

Output:
1
2
3
4
5

Inside for loop we are passing address of each element of array to display() method one by one for each iteration. Since we are passing address to display method, we need to have a pointer variable to receive it. Inside display() method we display the value present at the address being passed.

Source Code: Arrays, Pointers & Functions: Printing address of array elements

#include<stdio.h>

#define N 5

void display(int *x)
{
    printf("%d\n", x);
}

int main()
{
    int num[N] = {1, 2, 3, 4, 5}, i;

    for(i = 0; i < N; i++)
       display(&num[i]);

    return 0;
}

Output:
6356712
6356716
6356720
6356724
6356728

Inside display() method we’re accepting address using pointer variable, and then printing the addresses to the console window. If we want to print the elements present at the address, we need to append * in front of the pointer variable i.e., *x

Source Code: Add 1 to all the elements of array, using pointers and function

#include<stdio.h>

#define N 5

void oneplus(int *x)
{
    *x = *x + 1;
}

int main()
{
    int num[N] = {1, 2, 3, 4, 5}, i;

    for(i = 0; i < N; i++)
       oneplus(&num[i]);

    printf("\nArray elements after Oneplus operation!\n");
    for(i = 0; i < N; i++)
       printf("%d\n", num[i]);

    return 0;
}

Output:
Array elements after Oneplus operation!
2
3
4
5
6

Here we are passing address of each element to oneplus() method and inside oneplus() method we’re adding 1 to the value present at the address. We’re printing modified array element values inside main() method.

Source Code: Square all the elements of array – use pointers and function

#include<stdio.h>

#define N 5

void oneplus(int *x)
{
    *x = (*x) * (*x);
}

int main()
{
    int num[N] = {1, 2, 3, 4, 5}, i;

    for(i = 0; i < N; i++)
       oneplus(&num[i]);

    printf("\nArray elements after Oneplus operation!\n");
    for(i = 0; i < N; i++)
       printf("%d\n", num[i]);

    return 0;
}

Output:
Array elements after Oneplus operation!
1
4
9
16
25

Here we are passing address of each element to oneplus() method and inside oneplus() method we square the value present at the address. We’re printing modified array element values inside main() method.

Source Code: Arrays are pointers in disguise!

#include<stdio.h>

#define N 5

void oneplus5(int x[], int n)
{
    int i;

    for(i = 0 ; i < n; i++)
        x[i] = x[i] + 5;

}

int main()
{
    int num[N] = {1, 2, 3, 4, 5}, i;

    oneplus5(num, N);

    printf("\nArray elements after oneplus5 operation!\n");
    for(i = 0; i < N; i++)
       printf("%d\n", num[i]);

    return 0;
}

Output:
Array elements after oneplus5 operation!
6
7
8
9
10

Here we are passing base address and the size of array to oneplus5() method. Inside oneplus5() method, we are adding 5 to each element of the array. Once the execution of oneplus5() method completes, control comes back to main() method and here we print all the elements of the array. And the modifications made inside oneplus5() method reflects in main() method when we print the elements of array. Internally, x[i] will be converted into *(x + i) and hence the operation takes place at address level. So the manipulation reflects everywhere in the program.

void oneplus5(int x[], int n)
{
    int i;

    for(i = 0 ; i < n; i++)
        x[i] = x[i] + 5;
}

will be converted to

void oneplus5(int x[], int n)
{
    int i;

    for(i = 0 ; i < n; i++)
        *(x + i) = *(x + i) + 5;
}

Word of Caution!

Whenever you pass base address to a function and operate on the value present at that address, the array element/value gets altered. If you want to avoid it, make sure to pass a duplicate copy of the array to the function and not the base address of the original array.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Display Elements of Array In Reverse Order using Pointers

Write a c program to display/print elements of an array in reverse order, using pointers.

Pointer Variable: Pointer variable is a variable which holds the address of another variable of same type.

Related Read:
C Program To Print Elements of Array In Reverse Order
Basics of Pointers In C Programming Language

Important Topic
Please watch this important video without fail: C Programming: Arrays, Pointers and Functions

Example: Expected Output

Enter 5 integer numbers
1
2
3
4
5

Elements of array in reverse order …
5
4
3
2
1

Visual Representation

print array elements in reverse order using pointers

Video Tutorial: C Program To Display Elements of Array In Reverse Order using Pointers


[youtube https://www.youtube.com/watch?v=CYf-rVrKNTI]

YouTube Link: https://www.youtube.com/watch?v=CYf-rVrKNTI [Watch the Video In Full Screen.]

Source Code: C Program To Display Elements of Array In Reverse Order using Pointers

#include<stdio.h>

#define N 5

int main()
{
    int a[N], i, *ptr;

    printf("Enter %d integer numbers\n", N);
    for(i = 0; i < N; i++)
        scanf("%d", &a[i]);

    ptr = &a[N - 1];

    printf("\nElements of array in reverse order ...\n");
    for(i = 0; i < N; i++)
        printf("%d\n", *ptr--);

    return 0;
}

Output 1:
Enter 5 integer numbers
1
2
3
4
5

Elements of array in reverse order …
5
4
3
2
1
Output 2:
Enter 5 integer numbers
10
9
8
7
6

Elements of array in reverse order …
6
7
8
9
10

Logic To Display Elements of Array In Reverse Order using Pointers

We ask the user to input N integer numbers and store it inside array variable a[N]. We assign address of (N – 1)th element(last element of any array) to pointer variable ptr.

Inside for loop
We iterate through the for loop and for each iteration we print the value present at the address ptr. And for each iteration we decrement the value of ptr by 1.

This prints the array elements in reverse order.

Important Things To Note

1. Array elements are always stored in contiguous memory location.
2. A pointer when incremented always points to an immediately next location of its own type.

Explanation With Example

int a[5] = {5, 2, 6, 4, 3};
ptr = &a[4];

iptr*ptr–
010173
110134
210096
310052
410015

Output
3
4
6
2
5

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert