C Program To Find Even Numbers Between Range using For Loop

Lets write a C program to find all the even numbers between 2 integer values input by the user, using for loop.

Related Read:
Even or Odd Number: C Program
Modulus or Modulo Division In C Programming Language
C Program to Generate Even Numbers Between Two Integers

Note 1: An even number is an integer that is exactly divisible by 2. That is reminder of division should be zero.

Note 2: Even numbers are of the form 2 * number;

Note 3: Modular division( % ) returns remainder of division. For example, 10 / 2 = 5. But 10 % 2 = 0.

Video Tutorial: C Program to Find Even Numbers Between Range using For Loop


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

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


In above c program, we ask the user to input 2 integer value and store it in variables start and end. For loop counter is initialized to start and for loop executes until loop counter is less than or equal to value of end. For each iteration of the for loop, loop counter value increments by 1.

Inside for loop, for every value of count, we check if its perfectly divisible by 2. If true, it’s a Even number and we output that number to the console window.

Source Code: C Program to Find Even Numbers Between Range using For Loop

 
#include<stdio.h>

int main()
{
    int start, end, count, temp;

    printf("Enter start value and end value to generate Even no's\n");
    scanf("%d%d", &start, &end);

    if(start > end)
    {
        temp  = start;
        start = end;
        end   = temp;
    }

    printf("Even numbers between %d and %d are:\n", start, end);

    for(count = start; count <= end; count++)
    {
        if(count % 2 == 0)
        {
            printf("%d\n", count);
        }
    }

    return 0;
}

Output 1
Enter start value and end value to generate Even no’s
10
20
Even numbers between 10 and 20 are:
10
12
14
16
18
20

Output 2
Enter start value and end value to generate Even no’s
50
40
Even numbers between 40 and 50 are:
40
42
44
46
48
50

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 Print Natural Numbers Between Two Numbers using for loop

Lets write a C program to print natural numbers between two user entered numbers, using for loop.

We check if the user has entered smaller number first and then the larger number. If not, we swap the numbers present in the variables min and max.

Related Read:
For Loop In C Programming Language
Assignment Operators in C
Swap 2 Numbers Using a Temporary Variable: C

C Program To Find Factorial of a Number using for Loop


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

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


Source Code: C Program to Print Natural Numbers Between Two Numbers using for loop

 
#include<stdio.h>

int main()
{
    int min, max, temp, count;

    printf("Enter 2 positive numbers\n");
    scanf("%d%d", &min, &max);

    if(min > max)
    {
        temp = min;
        min  = max;
        max  = temp;
    }

    printf("Natural numbers from %d to %d are:\n", min, max);

    for(count = min; count <= max; count++)
    {
        printf("%d\n", count);
    }

    return 0;
}

Output 1:
Enter 2 positive numbers
10
15
Natural numbers from 10 to 15 are:
10
11
12
13
14
15

Output 2:
Enter 2 positive numbers
15
10
Natural numbers from 10 to 15 are:
10
11
12
13
14
15

Logic To Print Natural Numbers Between Two Numbers using for loop

We ask the user to enter 2 numbers, and store it inside variables min and max. For loop keeps iterating till min is less than or equal to max. We assign the value of min to count and keep incrementing the value of count by 1 for each iteration of the for loop. Once value of count is greater than value of max, control exits for loop.

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 Without Using a Temporary Variable: C

Today lets learn how to swap 2 integer numbers without using a temporary variable in C. We’re using arithmetic operation in our logic. We’re using addition and subtraction in this c program to swap the 2 numbers.

 
#include < stdio.h >

int main()
{
    int x = 10, y = 5;

    printf("Before swapping x = %d and y = %d\n", x, y);

    x = x + y - ( y = x );

    printf("After swapping x = %d and y = %d\n", x, y);

    return 0;
}


Output:
Before swapping x = 10 and y = 5
After swapping x = 5 and y = 10

Swap 2 Numbers Without Using a Temporary Variable: C


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

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


Swapping 2 Numbers In C using addition, subtraction and assignment operators: Logic

Here we take 2 variables, x and y; We store 10 in x and 5 in y.

We add the values of x(10) and y(5) and then subtract the value returned when we assign x value to y.

Note: When we assign a value to a variable, the same value is returned.
Ex: temp = (a = 10);
In this statement, when we assign 10 to a, the whole expression returns 10 and we can store it in variable temp, for our reference.

Swap 2 Numbers Using a Temporary Variable: C

Today lets learn how to swap 2 integer numbers using a temporary variable in C.

 
#include < stdio.h >

int main()
{
    int x = 10, y = 20, temp;

    printf("X = %d, Y = %d\n", x, y);
    
    temp = x;
    x    = y;
    y    = temp;

    printf("After swapping X = %d, Y = %d\n", x, y);

    return 0;
}


Output:
X = 10, Y = 20
After swapping X = 20, Y = 10

Swap 2 Numbers Using a Temporary Variable: C


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

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


Swapping 2 Numbers In C: Logic

Here we take 3 variables, x, y and temp; We store 10 in x and 20 in y.
1. First we copy the value present in x to temp. So now, both x and temp variables have value 10.
2. Next we copy value of y to x. So now, both x and y have value 20.
3. Next, copy the value of temp to y. So now both y and temp have value 10.

Finally after executing above 3 step logic, x has value 20 and y has a value of 10 in it.

Find First and Second Biggest In An Array, Without Sorting It: C

We assume that a[0] has first biggest and a[1] has the second biggest value.

Now check if this assumption is correct, if not, swap the values.

 
 if( sbig > fbig )
 {
    temp = sbig;
    sbig = fbig;
    fbig = temp;
 }

Now since we have already checked with a[0] and a[1], we start the comparison from a[2], till N.

for(i=2; i < n ; i++)
 if(a[i] > fbig)
 {
sbig = fbig;
fbig = a[i];
 }
 else if(a[i] > sbig)
sbig = a[i];

Now, if a[i] contains a number which is bigger than fbig, we transfer the value of a[i] to fbig and the value present in fbig to sbig;
If the value of a[i] is not bigger than fbig, then we check it with sbig. If a[i] is bigger than sbig, then we assign the value of a[i] to sbig.

This way, at the end of the loop fbig will have the first biggest and sbig will have the second biggest element/number in the array.

Video Tutorial: Find First and Second Biggest In An Array, Without Sorting It: C


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

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



Full source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include < stdio.h >
#include < conio.h >
 
void main()
{
int a[20], N, i, fbig, sbig, temp;
clrscr();
 
printf("Enter array limit\n");
scanf("%d", &N);
 
printf("Enter %d array elements\n", N);
for(i=0; i < n ; i++)
 scanf("%d", &a[i]);
 
fbig = a[0];
sbig = a[1];
 
if( sbig > fbig )
{
   temp = sbig;
   sbig = fbig;
   fbig = temp;
}
 
for(i=2; i < n ; i++)
 if(a[i] > fbig)
 {
sbig = fbig;
fbig = a[i];
 }
 else if(a[i] > sbig)
sbig = a[i];
 
 printf("First Big is %d and Second big is %d", fbig, sbig);
 
 getch();
 
}

Output:
Enter array limit
6
Enter 6 array elements
99
108
777
723
786
999
First Big is 999 and Second big is 786