C Program to Print Natural Numbers Between Two Numbers using While loop


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

We assume that user enters smaller number first and biggest number next.

Related Read:
while loop in C programming
Assignment Operators in C

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

 
#include < stdio.h >

int main()
{
    int min, max;

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

    printf("Natural numbers between %d and %d are:\n", min, max);
    while(min <= max)
    {
        printf("%d  ", min);
        min++;
    }

    printf("\n");

    return 0;
}

Output 1:
Enter 2 positive numbers
10
20
Natural numbers between 10 and 20 are:
10 11 12 13 14 15 16 17 18 19 20

Output 2:
Enter 2 positive numbers
25
35
Natural numbers between 25 and 35 are:
25 26 27 28 29 30 31 32 33 34 35

C Program To Find Factorial of a Number using While Loop


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

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


Logic To Print Natural Numbers Between Two Numbers using While loop

We ask the user to enter 2 numbers, and store it inside variables min and max. While loop keeps iterating till min is less than or equal to max. Inside while loop we keep incrementing the value of variable min by one for each iteration. We also display the value of min to the console window.

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

Leave a Reply

Your email address will not be published. Required fields are marked *