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 Find Size of Pointer Variables

Lets write a C program to find out the size or the number of bytes occupied by pointer variables of different data type in your computers memory.

Related Read:
Sizeof Operator in C Programming Language
Basics of Pointers In C Programming Language

Video Tutorial: C Program To Find Size of Pointer Variables


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

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


Source Code: C Program To Find Size of Pointer Variables

#include<stdio.h>

int main()
{
    printf("Size of int pointer = %d bytes.\n", sizeof(int*));
    printf("Size of char pointer = %d bytes.\n", sizeof(char*));
    printf("Size of float pointer = %d bytes.\n", sizeof(float*));
    printf("Size of double pointer = %d bytes.\n", sizeof(double*));
    printf("Size of long int pointer = %d bytes.\n", sizeof(long*));
    printf("Size of short int pointer = %d bytes.\n", sizeof(short*));

    return 0;
}

Output:
Size of int pointer = 4 bytes.
Size of char pointer = 4 bytes.
Size of float pointer = 4 bytes.
Size of double pointer = 4 bytes.
Size of long int pointer = 4 bytes.
Size of short int pointer = 4 bytes.

If you observe the output of above C program you can see that pointer variables, irrespective of their data type, consume 4 bytes of data in the memory.

Note: “But the number of bytes allocated for different data types and pointer variables are machine dependent. 16-bit, 32-bit, 64-bit computers allocate different bytes of memory. But pointer variables of any data type will always have same number of bytes occupied in the memory. For Example, if the computer allocates 2 bytes for pointer variable, then all the pointer variables, irrespective of their data type, will occupy 2 bytes in the memory.”

A pointer variable of type float holds only the address of floating point variable. A char pointer variable holds only the address of char type variable. But still the address of all these type of pointer variable is number. Addresses are always numbers and it can’t be a character, a string or real/floating/double numbers.

Also note that addresses are unique. There can’t be 2 location in your computers memory with same address.

Note: Observe that I’m using %d as format specifier while printing the size of data type, that’s because sizeof() function/method returns integer type data i.e., number of bytes occupied in the computers memory.

Note: sizeof() method takes a single argument and it is not a function whose value is determined at run time, but rather an operator whose value is determined by compiler – so it’s called as compile time unary operator.

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 Area and Circumference of Circle using Pointer

Lets write a C program to calculate area and circumference or perimeter of a Circle using pointer and function.

Related Read:
Function / Methods In C Programming Language
Basics of Pointers In C Programming Language

Video Tutorial: C Program To Find Area and Circumference of Circle using Pointer


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

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


Source Code: C Program To Find Area and Circumference of Circle using Pointer

#include<stdio.h>

void area_peri(float, float*, float*);

int main()
{
    float radius, area, perimeter;

    printf("Enter radius of Circle\n");
    scanf("%f", &radius);

    area_peri(radius, &area, &perimeter);

    printf("\nArea of Circle = %0.2f\n", area);
    printf("Perimeter of Circle = %0.2f\n", perimeter);

    return 0;
}

void area_peri(float r, float *a, float *p)
{
    *a = 3.14 * r * r;
    *p = 2 * 3.14 * r;
}

Output 1:
Enter radius of Circle
5

Area of Circle = 78.50
Perimeter of Circle = 31.40

Output 2:
Enter radius of Circle
14

Area of Circle = 615.44
Perimeter of Circle = 87.92

Logic To Find Area and Circumference of Circle using Pointer

We ask the user to enter value for radius of a Circle. We pass this value along with address of variables area and perimeter to the function area_peri().

area_peri(radius, &area, &perimeter);

We copy the value of radius to a local variable r and then we take 2 floating point pointer variables *a and *p. *a represents the value present at address a or &area. *p has value present at address p or &perimeter.

Inside area_peri() function we calculate the area and circumference / perimeter of Circle and store it as value present at addresses a and p. Since a points to address of variable area and p points to address of variable perimeter, the values of variable area and perimeter changes too.

*a = 3.14 * r * r;

*p = 2 * 3.14 * r;

Area and Circumference of Circle

We’ve separate video tutorials to calculate area and circumference of a Circle using radius, and without using pointer and function. You can check them out at these links:

Calculate Area of a Circle without using math.h library: C
C Program To Calculate Circumference of Circle

Note: When * is precedes any address, it fetches the value present at that address or memory location.

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 Add Two Numbers using Pointers

Lets write a C program to add 2 numbers using pointer and function.

In this video tutorial we will show both ways of adding 2 numbers using pointers: 1. Using function 2. Without using function.

Related Read:
Basics of Pointers In C Programming Language
Function / Methods In C Programming Language

Video Tutorial: C Program To Add Two Numbers using Pointers


[youtube https://www.youtube.com/watch?v=0wBPwxsr6-U]

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


Source Code: C Program To Add Two Numbers using Pointers and Without using Function

#include<stdio.h>

int main()
{
    int a, b, c, *ptr1, *ptr2;

    ptr1 = &a;
    ptr2 = &b;

    printf("Enter 2 numbers\n");
    scanf("%d%d", &a, &b);

    c = *ptr1 + *ptr2;

    printf("Using *ptr1 + *ptr2 : %d + %d = %d\n", a, b, c);

    c = *(&a) + *(&b);

    printf("Using  *(&a) + *(&b) : %d + %d = %d\n", a, b, c);

    return 0;
}

Output:
Enter 2 numbers
25
25
Using *ptr1 + *ptr2 : 25 + 25 = 50
Using (&a) + *(&b) : 25 + 25 = 50

Logic To Add Two Numbers using Pointers

Here we are storing the address of variable a in pointer variable ptr1 and address of variable b in pointer variable ptr2.

We know that any address preceded by * would fetch the value present at that address. So we use following code to get the addition of 2 numbers result using pointers.

c = *ptr1 + *ptr2;

Source Code: C Program To Add Two Numbers using Pointer and Function

#include<stdio.h>

void addition(int, int, int*);

int main()
{
    int a, b, c;

    printf("Enter 2 numbers\n");
    scanf("%d%d", &a, &b);

    addition(a, b, &c);

    printf("%d + %d = %d\n", a, b, c);

    return 0;
}

void addition(int x, int y, int *z)
{
    *z = x + y;
}

Output:
Enter 2 numbers
10
40
10 + 40 = 50

Logic To Add Two Numbers using Pointers and Function

We ask the user to enter 2 numbers and store it inside address of variables a and b. Next we pass the values of a and b and address of variable c to a function called addition(). And then print the result in main method itself.

The motive is to change the value present at address of variable c. That way we can print the result directly inside main method itself, without our function addition() returning any value back to calling function(main()).

Inside addition() method, we add the values passed in first 2 arguments and then store the result back in the address location sent to addition() function as third argument. Function addition() doesn’t return any value, so it has void as its return type.

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