C Program To Find Odd Numbers Between Range using For Loop

Lets write a C program to generate odd numbers between 2 integer values input by the user using For loop.

Related Read:
Even or Odd Number: C Program
C Program to Generate Odd Numbers Between Two Integers

Note 1: An odd number is an integer that is not exactly divisible by 2.

Note 2: Odd numbers are of the form (2 * number + 1);

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

Video Tutorial: C Program To Find Odd Numbers Between Range using For Loop


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

YouTube Link: https://www.youtube.com/watch?v=P85sziIyIdA [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. If value of start is greater than the value of end, then we swap the values.

For loop counter is initialized to start, and for loop executes until value of count is less than or equal to end. For each iteration of the for loop, count value increments by 1.

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

Source Code: C Program To Find Odd Numbers Between Range using For Loop

 
#include<stdio.h>

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

    printf("Enter start and end value, to find odd numbers\n");
    scanf("%d%d", &start, &end);

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

    printf("Odd 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 and end value, to find odd numbers
40
60
Odd numbers between 40 and 60 are
41
43
45
47
49
51
53
55
57
59

Output 2
Enter start and end value, to find odd numbers
60
40
Odd numbers between 40 and 60 are
41
43
45
47
49
51
53
55
57
59

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 Prime Numbers Between Range, using For Loop

Lets write a C program to find and print/display all the prime numbers between 2 integer values input by the user, using nested for loop.

Prime Number: Any natural number which is greater than 1 and has only two factors i.e., 1 and the number itself is called a prime number.

Related Read:
Decision Control Instruction In C: IF
Nested For Loop In C Programming Language
break Statement In C Programming Language
C Program To Find Prime Number or Not using For Loop
C Program To Find Prime Numbers From 2 To N, using For Loop

Video Tutorial: C Program To Find Prime Numbers Between Range, using For Loop


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

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

Logic To Find Prime Number Between Range, using For Loop

We ask the user to enter start and end value. We check if the value of variable start is greater than variable end. If true, we swap the values of variable start and end.

Outer For Loop Logic

We assign value of start to num and keep iterating the for loop until num is less than or equal to value of variable end. For each iteration of outer for loop num will increment by 1, from start to end value.

Inner For loop Logic

All the numbers are perfectly divisible by number 1, so we initialize the variable count to 2, instead of 1. So our inner for loop starts checking for divisibility from number 2.

The selected number(selected by outer for loop and stored in variable num), is divided by numbers 2 to num-1 times. If num is perfectly divisible by any number between 2 to num-1, then the number is not a prime number, else its a prime number.

Source Code: C Program To Find Prime Numbers Between Range, using For Loop

#include<stdio.h>
#include<math.h>

int main()
{
    int start, end, num, count, prime, temp, inum;

    printf("Enter start and end value\n");
    scanf("%d%d", &start, &end);

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

    printf("Prime Numbers between %d and %d are\n", start, end);

    for(num = start; num <= end; num++)
    {
        prime = 1;
        inum  = sqrt(num);
        for(count = 2; count <= inum; count++)
        {
            if(num % count == 0)
            {
                prime = 0;
                break;
            }
        }

        if(prime) printf("%d,\t", num);
    }

    return 0;
}

Output 1:
Enter start and end value
10
20
Prime Numbers between 10 and 20 are
11, 13, 17, 19,

Output 2:
Enter start and end value
20
10
Prime Numbers between 10 and 20 are
11, 13, 17, 19,

Output 3:
Enter start and end value
25
60
Prime Numbers between 25 and 60 are
29, 31, 37, 41, 43, 47, 53, 59,

Output 4:
Enter start and end value
50
150
Prime Numbers between 50 and 150 are
53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149,

Output 5:
Enter start and end value
5
41
Prime Numbers between 5 and 41 are
5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41,

Logic To Find Prime Number, using For Loop

In this method, we apply square root to the selected number and store it inside variable inum. This reduces the number of iterations of inner while loop.

For example,
If num = 41;
inum = sqrt(num);
inum = sqrt(41);
inum = 6;

User entered number 41 is not perfectly divisible by any number between 2 to 6, so number 41 is a prime number.

So its enough if we iterate through the while loop sqrt(num) times to check if the selected number is divisible by any number other than 1 and itself.

Table of all prime numbers up to 1,000:

prime number or not

Note: We are not using curly braces around if statement because we only have 1 line of code after if – so curly braces are optional. If we have multiple lines of code, then we must use curly braces to wrap around the block of code.

You can also watch C Program To Find Prime Numbers Between Two Intervals, using While Loop video tutorial.

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 Calculate Sum of Natural Numbers Between Range using For Loop

Lets write a C program to calculate sum of all natural numbers between the user entered range of 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
C Program To Print Natural Numbers Between Two Numbers using for loop
Swap 2 Numbers Using a Temporary Variable: C

Video Tutorial: C Program To Calculate Sum of Natural Numbers Between Range using For Loop


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

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


Source Code: C Program To Calculate Sum of Natural Numbers Between Range using For Loop

 
#include<stdio.h>

int main()
{
    int min, max, temp, count, sum = 0;

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

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

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

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

    printf("Sum of Natural Numbers from %d to %d is %d\n", min, max, sum);

    return 0;
}

Output 1:
Enter 2 positive numbers
5
10

Natural Numbers from 5 to 10 are:
5
6
7
8
9
10
Sum of Natural Numbers from 5 to 10 is 45

Output 2:
Enter 2 positive numbers
14
10

Natural Numbers from 10 to 14 are:
10
11
12
13
14
Sum of Natural Numbers from 10 to 14 is 60

Logic To Calculate Sum of Natural Numbers Between Range using For Loop

We ask the user to enter minimum and maximum number(i.e., the range) and we store it inside variable min and max. If value of min is greater than value of max, then we swap the values of min and max.

We initialize the variable count to min and iterate through the for loop until count value is less than or equal to value of max. We keep incrementing the value of count by 1 for each iteration of for loop.

Inside for loop we add the value of count to previous value of sum and once the control exits the for loop we display the value present in variable sum.

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

C Program To Find Range of Set of Numbers

Write a C program to find the range of a set of numbers entered through the keyboard. Range is the difference between the smallest and biggest number in the list.

Example: If biggest number in the list is 5 and smallest number in the list is 1. The difference between them is the range. i.e., 5 – 1 = 4. So range = 4.

Related Read:
while loop in C programming
if else statement in C
Relational Operators In C
C Program To Find Absolute Value of a Number

Expected Output for the Input

User Input:
Enter the limit
5
Enter 5 numbers
1
2
3
4
5

Output:
Small Number = 1
Big Number = 5
Range is 4

Logic To Find Range of Set of Numbers

First we ask the user to enter the length or the size of the list, and store it inside the variable limit. If the user enters the list size as 5, then we ask the user to enter 5 numbers.

Next we ask the user to enter the first number. We assign the first number entered by the user to variables small and big. Since user already entered 1 number into the list, we decrement the value of variable limit by 1.

Next we take remaining inputs inside the while loop. For each iteration of the while loop we decrement the value of variable limit by 1, until the value of limit is 0. Once value of limit is zero, control exits while loop.

For each input(inside the while loop), we check if the value entered is bigger than the value present in variable big. If its true, we assign the bigger number to variable big. We also check if the value entered is smaller than the value present in variable small. If its true, we assign the smaller number to variable small.

Once the control exits the while loop, variable big will have the biggest number in the list and variable small will have the smallest number in the list.

Finally, we use below formula to calculate range of the list:
range = big – small;

Video Tutorial: C Program To Find Range of Set of Numbers


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

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

Source Code: C Program To Find Range of Set of Numbers

#include<stdio.h>
#include<stdlib.h>

int main()
{
    int small, big, range, num, limit;

    printf("Enter the limit\n");
    scanf("%d", &limit);

    printf("Enter %d numbers\n", limit);
    scanf("%d", &num);

    small = big = num;

    limit = limit - 1;

    while(limit)
    {
        scanf("%d", &num);

        if(num > big)
        {
            big = num;
        }

        if(num < small)
        {
            small = num;
        }

        limit--;
    }

    range = big - small;

    printf("Small Number = %d\nBig Number = %d\n", small, big);
    printf("Range is %d\n", abs(range));

    return 0;
}

Output 1:
Enter the limit
5
Enter 5 numbers
0
-1
2
3
4
Small Number = -1
Big Number = 4
Range is 5

Output 2:
Enter the limit
6
Enter 6 numbers
10
9
8
7
6
5
Small Number = 5
Big Number = 10
Range is 5

Note: abs() returns the absolute value of a number. Absolute value is always positive.

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