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

Swap 2 Numbers Using Macros: C Program

Today lets learn how to swap two integer numbers(using a temporary variable) using Macros in C.

Video Tutorial: Swap 2 Numbers Using Macros: C Program


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

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

Source Code: Swap 2 Numbers Using Macros: C Program

#include<stdio.h>

#define SWAP(x, y, temp) temp = x; x = y; y = temp;

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

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

    printf("Before swapping: a = %d and b = %d\n", a, b);

    SWAP(a, b, temp);

    printf("After swapping: a = %d and b = %d\n", a, b);

    return 0;
}

Output:
Enter 2 integer numbers
20
50
Before swapping: a = 20 and b = 50
After swapping: a = 50 and b = 20

Logic To Swap Two Numbers

First value of a is transferred to temp;
Next value of b is transferred to a.
Next value of temp is transferred to b.



That’s how value of a and b are swapped using a temporary variable.

Note: Preprocessor replaces the macro template(SWAP(a, b, temp)) with its corresponding macro expansion(temp = x; x = y; y = temp;) before passing the source code to the compiler.

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 Shift Variable Values Circularly To Right

Given three variables x, y, z write a function to circularly shift their values to right. In other words if x = 5, y = 8, z = 10, after circular shift y = 5, z = 8, x = 10. Call the function with variables a, b, c to circularly shift values.

Analyze The Above Problem Statement

1. We need to write a function which receives 3 numbers.
2. Inside the function we need to swap the values of 3 variables circularly to the right.

i.e., value of x to y, value of y to z, and value of z to a.

Related Read:
Basics of Pointers In C Programming Language
Swap 2 Numbers Using a Temporary Variable: C

Very Important Note:

Any address preceded by a * (Indirection operator) will fetch the value present at that address.

Video Tutorial: C Program To Shift Variable Values Circularly To Right


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

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


Source Code: C Program To Shift Variable Values Circularly To Right

#include<stdio.h>

void shift_right(int*, int*, int*);

int main()
{
    int x, y, z;

    printf("Enter 3 numbers\n");
    scanf("%d%d%d", &x, &y, &z);

    printf("Before shifting right: x = %d, y = %d and z = %d\n", x, y, z);

    shift_right(&x, &y, &z);

    printf("After shifting right: x = %d, y = %d and z = %d\n", x, y, z);

    return 0;
}

void shift_right(int *a, int *b, int *c)
{
    int temp;

    temp = *c;
    *c   = *b;
    *b   = *a;
    *a   = temp;
}

Output 1:
Enter 3 numbers
5
8
10
Before shifting right: x = 5, y = 8 and z = 10
After shifting right: x = 10, y = 5 and z = 8

Output 2:
Enter 3 numbers
2
3
1
Before shifting right: x = 2, y = 3 and z = 1
After shifting right: x = 1, y = 2 and z = 3

Logic To Shift Variable Values Circularly To Right

We ask the user to enter 3 numbers and store it inside the address of variables x, y and z. Now we pass these 3 address to the function shift_right();

Inside shift_right() function
Inside shift_right() function we shift the value present at *c to a temp variable. Then, we shift the value of *b to *c, then the value of *a to *b, and then we shift the value present in temp to *a. This shifts the values of variables x, y and z circularly to the right.

*a has the value present at the address &x;
*b has the value present at the address &y;
*c has the value present at the address &z;


int temp;

temp = *c;
*c = *b;
*b = *a;
*a = temp;

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 Swap Two Numbers using Function

Lets write a C program to swap 2 numbers using function/method.

When we call a function and pass the actual value it’s called as Call by Value method. If we pass the reference or the address of the variable while calling the function, then it’s called Call by Reference.

In today’s video tutorial we’ll be showing you the concept of Call By Value.

Call by Reference Example: Swapping 2 numbers using pointers

We have written the same C program using pointer and function to illustrate the concept of call by reference. To achieve call by reference we need to use pointers concept. Please watch the video tutorial present at C Program To Swap Two Numbers using Pointers to understand the concept of pointers and call by reference.

Related Read:
Swap 2 Numbers Using a Temporary Variable: C
Function / Methods In C Programming Language

Video Tutorial: C Program To Swap Two Numbers using Function


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

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


Source Code: C Program To Swap Two Numbers using Function

#include<stdio.h>

void swap(int, int);

int main()
{
    int a, b;

    printf("Enter values for a and b\n");
    scanf("%d%d", &a, &b);

    printf("\n\nBefore swapping: a = %d and b = %d\n", a, b);

    swap(a, b);

    return 0;
}

void swap(int x, int y)
{
    int temp;

    temp = x;
    x    = y;
    y    = temp;

    printf("\nAfter swapping: a = %d and b = %d\n", x, y);
}

Output 1:
Enter values for a and b
20
50

Before swapping: a = 20 and b = 50
After swapping: a = 50 and b = 20

Output 2:
Enter values for a and b
80
90

Before swapping: a = 80 and b = 90
After swapping: a = 90 and b = 80

Logic To Swap Two Numbers

We ask the user to enter values for variable a and b. We pass the user entered values to swap() function. Since we are passing the values to the function, its called Call by value method.

Values of a and b are copied into local variables of swap() that is x and y. We take a local variable temp inside swap() function. We assign the value of x to temp. Then we assign the value of y to x. And finally we assign the value of temp to y. This swaps the values of variable x and y.

Since swap() method doesn’t return anything, we print the values of x and y inside swap() method itself.


temp = x;
x = y;
y = temp;

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

Lets write a C program to swap 2 numbers using pointers and function.

When we call the function, we pass the reference or address of the variable, so this method is called “Call by Reference“.

Related Read:
Swap 2 Numbers Using a Temporary Variable: C
Function / Methods In C Programming Language
Basics of Pointers In C Programming Language

Video Tutorial: C Program To Swap Two Numbers using Pointers


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

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


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

#include<stdio.h>

void swap(int*, int*);

int main()
{
    int a, b;

    printf("Enter values for a and b\n");
    scanf("%d%d", &a, &b);

    printf("\n\nBefore swapping: a = %d and b = %d\n", a, b);

    swap(&a, &b);

    printf("\nAfter swapping: a = %d and b = %d\n", a, b);

    return 0;
}

void swap(int *x, int *y)
{
    int temp;

    temp = *x;
    *x   = *y;
    *y   = temp;
}

Output 1:
Enter values for a and b
100
200

Before swapping: a = 100 and b = 200

After swapping: a = 200 and b = 100

Output 2:
Enter values for a and b
30
20

Before swapping: a = 30 and b = 20

After swapping: a = 20 and b = 30

Logic To Swap Two Numbers using Pointers and Function

We ask the user to enter values for variable a and b. We pass the address of variable a and b to function swap().

Inside function swap() we take a local variable temp. Since address of variable a and b are passed to swap() method, we take 2 pointer variables *x and *y. Pointer variable x holds the address of a and pointer variable y holds the address of b. Using below logic we swap the values present at address a( or x ) and b( or y ).


temp = *x;
*x = *y;
*y = temp;

Since we are directly changing the value present at particular address, the value gets reflected throughout the program and not just inside particular function. That’s why we are able to print the result inside main function and the values of variable a and b are swapped, without getting any returned value from function swap().

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