C Program To Re-arrange Even and Odd Elements of An Array

Lets write a C program to re-arrange even and odd elements of an array.

Note: We need to arrange EVEN numbers at the top and ODD numbers to the bottom of the same array.

Example: Expected Input/Output

Enter 5 integer numbers
11
10
13
12
15

After re-arranging even and odd elements …
10
12
13
11
15

Visual Representation

Rearranging even and odd elements of an array

Video Tutorial: C Program To Re-arrange Even and Odd Elements of An Array


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

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

Source Code: C Program To Re-arrange Even and Odd Elements of An Array

#include<stdio.h>

#define N 10

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

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

    for(i = 0; i <= j; i++)
    {
        if(a[i] % 2 != 0)
        {
            while(j > i)
            {
                j--;
                if(a[j] % 2 == 0)
                {
                    temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                    break;
                }
            }
        }
    }

    printf("\nAfter re-arranging even and odd elements ...\n");
    for(i = 0; i < N; i++)
        printf("%d\n", a[i]);

    return 0;
}

Output:
Enter 10 integer numbers
1
2
3
4
5
6
7
8
9
10

After re-arranging even and odd elements …
10
2
8
4
6
5
7
3
9
1

Logic To Re-arrange Even and Odd Elements of An Array

First we accept N integer numbers from the user and store it inside a[N]. Using “for loop” we iterate through the array elements one by one.

Inside For Loop
We initialize i to 0, which is the first index of any array. We iterate through this “for loop” until i is less than or equal to j. j represents the number of elements already sorted from bottom(N – 1) of the array. i.e., from index j to (N – 1) all the elements are already odd numbers. For each iteration of the for loop we increment the value of i by 1.

First if condition – inside for loop
Aim of our C program is to move all the even elements to top and odd elements to the bottom. So if the “for loop” selected element, which is present in a[i] already has a even number then we let that number stay there itself, and increment the value of i by one. If the “for loop” selected number, which is present in a[i] has odd number, then we start searching for a even number from the bottom of the same array to swap it with a[i].

Inside While loop
Assume that a[i] has a odd number. Now lets initialize j to the size of array, which is N. “While loop” executes until j is greater than i. Here i represents the number of elements already sorted from the top. i.e., from index 0 to i all the elements are already even numbers.

This “while loop” executes until j is greater than i. The value of i is set by “for loop”. It’s the index of the element which is selected by the “for loop”, which has ODD number. i.e., if the control has entered “while loop” that means a[i] has ODD number. “While Loop” checks for EVEN element from the bottom of the array to swap it with the ODD number present at a[i].

Second if condition – inside while loop
As soon as control enters the while loop we reduce the value of j by 1. So now j is (N – 1), which is the last element of any array. Next, if the “while loop” selected element, which is present at a[j] has EVEN number, then we swap that number with the ODD number present in a[i], and break out of the while loop.

For each iteration of “while loop” we decrement the value of j by 1. And we do not reset the value of j for each iteration of “for loop”.

Printing re-arranged array elements
Once control exits “for loop” we print the array elements from 0 to N -1 and it’ll have all EVEN numbers at the top and ODD numbers at the bottom.

Explanation With Example

If int a[5] = {11, 10, 13, 12, 15};
ODD: a[i] % 2 != 0
EVEN: a[j] % 2 == 0

    j = N;
    for(i = 0; i <= j; i++)
    {
        if(a[i] % 2 != 0)
        {
            while(j > i)
            {
                j--;
                if(a[j] % 2 == 0)
                {
                    temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                    break;
                }
            }
        }
    }
ia[i]ODDja[j]EVENSwap
011TRUE415FALSENO
011TRUE312TRUEYES
110FALSE213FALSENO

As you can see from above table swapping occurs at a[0] and a[3]. a[0] has integer number 11 and a[3] has integer number 12. So after swapping these numbers a[5] will be: {12, 10, 13, 11, 15}; So a[0], a[1] has EVEN numbers and a[2], a[3], a[4] has ODD numbers.

Important Note: i always represents the number of elements sorted from top(EVEN numbers), and j represents the index from (N – 1) which are sorted from bottom(ODD numbers).

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 Copy Elements of One Array To Another In Reverse Order

Lets write a c program to copy elements of one array to another array in reverse order. Hint: Make use of macros to assign size of the array. And both the arrays must have same size.

Related Read:
Basics of Arrays: C Program

Example: Expected Output

Enter 5 integer numbers
5
2
6
4
3

Copying elements from array a to b
In reverse Order

Original(a[5]) –> Copy(b[5])
5 –> 3
2 –> 4
6 –> 6
4 –> 2
3 –> 5
Copy array elements in reverse order

Video Tutorial: C Program To Copy Elements of One Array To Another In Reverse Order


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

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

Source Code: C Program To Copy Elements of One Array To Another In Reverse Order

#include<stdio.h>

#define N 5

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

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

    printf("\n\nCopying elements from array a to b, in reverse order\n");
    for(i = N - 1, j = 0; i >= 0; i--, j++)
        b[j] = a[i];

    printf("\nOriginal(a[%d])  -->  Copy(b[%d])\n", N, N);
    for(i = 0; i < N; i++)
        printf("%4d\t\t-->%6d\n", a[i], b[i]);

    return 0;
}

Output:
Enter 5 integer numbers
1
2
3
4
5

Copying elements from array a to b, in reverse order

Original(a[5]) –> Copy(b[5])
1 –> 5
2 –> 4
3 –> 3
4 –> 2
5 –> 1

Logic To Copy Elements of One Array To Another In Reverse Order

We ask the user to enter N integer numbers. N is macro which is used to define size of the array. We store the user entered numbers inside array variable a. We initialize the variable i to last index of array a, and we initialize the variable j to first index of array variable b. Now for each iteration of the for loop, we assign the value of a[i] to b[j]. For each iteration of for loop we decrement the value of i by 1 and increment the value of j by 1. For loop iterates until i value is greater than or equal to 0.

At the end we print / display the content of both original array(a[5]) and the array to which the elements are copied to(b[5]) in reverse order.

Explanation With Example

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

    for(i = N - 1, j = 0; i >= 0; i--, j++)
        b[j] = a[i];
ija[i]b[j]
40a[4] = 3b[0] = 3
31a[3] = 4b[1] = 4
22a[2] = 6b[2] = 6
13a[1] = 2b[3] = 2
04a[4] = 5b[0] = 5

a[5] = {5, 2, 6, 4, 3};
b[5] = {3, 4, 6, 2, 5};
Copy array elements in reverse order
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 Copy Elements of One Array To Another

Lets write a c program to copy all the elements of one array to another array of same size.

Related Read:
Basics of Arrays: C Program

Example: Expected Output

Enter 5 integer numbers
5
2
6
4
3

Copying elements of array a to b

Original(a[5]) –> Copy (b[5])
5 –> 5
2 –> 2
6 –> 6
4 –> 4
3 –> 3
Copy array elements from a to b

Video Tutorial: C Program To Copy Elements of One Array To Another


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

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

Source Code: C Program To Copy Elements of One Array To Another

#include<stdio.h>

#define N 5

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

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

    printf("\n\nCopying elements of array a to b\n");
    for(i = 0; i < N ; i++)
        b[i] = a[i];

    printf("\nOriginal(a[%d])  -->  Copy (b[%d])\n", N, N);
    for(i = 0; i < N; i++)
        printf("%4d\t\t-->%6d\n", a[i], b[i]);

    return 0;
}

Output:
Enter 5 integer numbers
5
4
3
2
1

Copying elements of array a to b

Original(a[5]) –> Copy (b[5])
5 –> 5
4 –> 4
3 –> 3
2 –> 2
1 –> 1

Logic To Copy Elements of One Array To Another

We ask the user to enter N integer numbers. N is macro which is used to define size of the array. We store the user entered numbers inside array variable a. Since both array variables a and b has same size we copy individual elements of array variable a to array variable b at same index position.

At the end we print / display the content of both original array(a[5]) and the array to which the elements are copied to(b[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

C Program To Find Smallest Element In An Array

Lets write a C program to find smallest element in an array, without sorting the elements. And also print the position at which the smallest number is present in the array.

Related Read:
C Program To Find Biggest Element of An Array

Example: Expected Output

Enter 5 integer numbers
5
2
6
4
3

Biggest of 5 numbers is 2, at position 2.
array with size 5

Video Tutorial: C Program To Find Smallest Element In An Array


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

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

Source Code: C Program To Find Smallest Element of An Array

Method 1

#include<stdio.h>

#define N 5

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

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

        if(i == 0 || small > a[i])
        {
            small = a[i];
            pos   = i + 1;
        }
    }

    printf("Smallest Number: %d, at position %d.\n", small, pos);

    return 0;
}

Output 1:
Enter 5 integer numbers
9
8
6
2
5
Smallest Number: 2, at position 4.

Output 2:
Enter 5 integer numbers
5
6
1
3
2
Smallest Number: 1, at position 3.

Logic To Find Smallest Element In An Array

We ask the user to input N integer numbers. N being Macro, used to assign array size. In above source code N is assigned a value of 5. So user enters 5 integer numbers. While the user inputs numbers we check if it’s the first number input by the user. If its the first number/element, then we assign that first number/element entered by the user to variable small and assign 1 to variable pos, indicating that its the first element in the array.

Next, for each consecutive iteration of the for loop we check if the new value entered by the user is smaller than the value present in variable small. If it’s true, then we assign the new value entered by the user to variable small and also update the value of pos accordingly.

Once i < N condition is false, control exits for loop and we print the value present inside variable small and pos which will have smallest of N numbers or the smallest element of the array and position of that element in the array.

Explanation With Example

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

    for(i = 0; i < N; i++)
    {
        scanf("%d", &a[i]);

        if(i == 0 || small > a[i])
        {
            small = a[i];
            pos   = i + 1;
        }
    }
indexa[index]smallpos = index + 1
0a[0] = 551
1a[1] = 222
2a[2] = 6
3a[3] = 4
4a[4] = 3
5

a[5] = {5, 2, 6, 4, 3}
small = 2;
pos = 2;

Source Code: C Program To Find Smallest Element of An Array

Method 2

 #include<stdio.h>

#define N 5

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

    printf("Enter %d integer numbers\n", N);

    for(i = 0; i < N; i++)
        scanf("%d", &a[i]);

    small = a[0];
    pos   = 1;

    for(i = 1; i < N ; i++)
    {
        if(small > a[i])
        {
            small = a[i];
            pos   = i + 1;
        }
    }

    printf("Smallest Number: %d, at position %d.\n", small, pos);

    return 0;
}

Output 1:
Enter 5 integer numbers
9
8
6
2
5
Smallest Number: 2, at position 4.

Output 2:
Enter 5 integer numbers
5
2
6
4
3
Smallest Number: 2, at position 2.

Method 2: LOGIC

Here we accept N integer numbers from the user. Next we assign the first element of the array to variable small and 1 to pos. Now we use another for loop to loop through the array.

Inside for loop
Inside for loop we check if the value present inside variable small is greater or bigger than the value present at a[i]. If it’s true, then we assign the value of a[i] to small and value of (i+1) to variable pos.

At the end of for loop, variable small and pos will have smallest element of the array and the position at which this number is present in the array.

Note: Since we assign the value of first element of the array(which is present at location a[0]) to variable small, while comparing with other elements of the array, we start comparing from a[1] till a[N-1]. That’s the reason we’ve assigned initial value of i to 1 in the second for loop.

Since a[0] is assigned to variable small, there is no point in comparing big with a[0]. So we start comparison from a[1].

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

Lets write a C program to find biggest or the largest element in an array, without sorting the elements. And also print the position at which the biggest number is present in the array.

Related Read:
Find Biggest In An Array, Without Sorting It: C

Example: Expected Output

Enter 5 integer numbers
5
2
6
4
3

Biggest of 5 numbers is 6, at position 3

array with size 5

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


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

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

Source Code: C Program To Find Biggest Element of An Array

Method 1

#include<stdio.h>

#define N 5

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

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

        if(i == 0 || big < a[i])
        {
            big = a[i];
            pos = i + 1;
        }
    }

    printf("\nBiggest of %d numbers is %d, at position %d.\n", N, big, pos);

    return 0;
}

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

Biggest of 5 numbers is 8, at position 5.

Output 2:
Enter 5 integer numbers
10
2
5
9
3

Biggest of 5 numbers is 10, at position 1.

Logic To Find Biggest Element In An Array

We ask the user to input N integer numbers. N being a Macro. In above source code N is 5. So user enters 5 integer numbers. While the user inputs numbers we check if it’s the first number input by the user. If its the first number, then we assign that first number entered by the user to variable big and assign 1 to variable pos, indicating that its the first element in the array.

Next, for each consecutive iteration of the for loop we check if the new value entered by the user is greater than the value present in variable big. If it’s true, then we assign the new value entered by the user to variable big and also update the value of pos accordingly.

Once i < N condition is false, control exits for loop and we print the value present inside variable big and pos which will have biggest of N numbers or the biggest element of the array and position of that element in the array.

Explanation With Example

If int a[5] = {2, 4, 3, 4, 5};

    for(i = 0; i < N; i++)
    {
        scanf("%d", &a[i]);

        if(i == 0 || big < a[i])
        {
            big = a[i];
            pos = i + 1;
        }
    }
indexa[index]bigpos = index + 1
0a[0] = 221
1a[1] = 442
2a[2] = 3
3a[3] = 4
4a[4] = 555
5

a[5] = {2, 4, 3, 4, 5}
big = 5;
pos = 5;

Source Code: C Program To Find Biggest Element of An Array

Method 2

#include<stdio.h>

#define N 5

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

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

    big = a[0];
    pos = 1;

    for(i = 1; i < N; i++)
    {
        if(big < a[i])
        {
            big = a[i];
            pos = i + 1;
        }
    }

    printf("\nBiggest of %d numbers is %d, at position %d.\n", N, big, pos);

    return 0;
}

Output 1:
Enter 5 integer numbers
1
5
6
3
0

Biggest of 5 numbers is 6, at position 3.

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

Biggest of 5 numbers is 5, at position 5

Method 2: LOGIC

Here we accept N integer numbers from the user. Next we assign the first element of the array to big and 1 to pos. Now we use another for loop to loop through the array. Inside for loop we check if the value present inside variable big is less than the value present at a[i]. If it’s true, then we assign the value of a[i] to big and value of (i+1) to variable pos.

At the end of for loop, variable big and pos will have biggest element of the array and the position at which this number is present in the array.

Note: Since we assign the value of first element of the array(which is present at location a[0]) to variable big, while comparing with other elements of the array, we start comparing from a[1] till a[N-1]. That’s the reason we’ve assigned initial value of i to 1 in the second for loop.

Since a[0] is assigned to variable big, there is no point in comparing big with a[0]. So we start comparison from a[1].

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