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

C Program to Print Natural Numbers from 1 to N In Reverse Order using While loop

Lets write a simple C program to print natural numbers from 1 to N in reverse order, using while loop.

Related Read:
while loop in C programming
C Program to Print Natural Numbers from 1 to N using While loop

Source Code: C Program to Print Natural Numbers from 1 to N In Reverse Order using While loop

 
#include < stdio.h >

int main()
{
    int num;

    printf("Enter a positive number\n");
    scanf("%d", &num);

    printf("\nNatural numbers from 1 to %d are:\n", num);

    while(num)
    {
        printf("%d  ", num);
        num--;
    }

    printf("\n");

    return 0;
}

Output 1:
Enter a positive number
10

Natural numbers from 1 to 10 are:
10 9 8 7 6 5 4 3 2 1

Output 2:
Enter a positive number
14

Natural numbers from 1 to 14 are:
14 13 12 11 10 9 8 7 6 5 4 3 2 1

C Program to Print Natural Numbers from 1 to N In Reverse Order using While loop


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

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


Logic To Print Natural Numbers from 1 to N In Reverse Order using While loop

We ask the user to enter a positive number, and store it inside a variable num. We iterate through the loop until the number is equal to zero. For example, if user enters num = 5, we iterate the loop 5 times. Inside the while loop we keep decrementing the value of variable num. Once the value of num becomes 0, we exit the loop. For each iteration we print the value of num.

This way we printout all the natural numbers from 1 to N, 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 Print Natural Numbers from 1 to N using While loop

Lets write a simple C program to print natural numbers from 1 to N, using while loop.

Related Read:
while loop in C programming

Source Code: C Program to Print Natural Numbers from 1 to N using While loop

 
#include < stdio.h >

int main()
{
    int num, count = 1;

    printf("Enter a positive number\n");
    scanf("%d", &num);

    printf("\nNatural numbers from %d to %d:\n", count, num);

    while(count <= num)
    {
        printf("%d  ", count);
        count++;
    }

    printf("\n");

    return 0;
}

Output 1:
Enter a positive number
10

Natural numbers from 1 to 10:
1 2 3 4 5 6 7 8 9 10

Output 2:
Enter a positive number
14

Natural numbers from 1 to 14:
1 2 3 4 5 6 7 8 9 10 11 12 13 14

C Program to Print Natural Numbers from 1 to N using While loop


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

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


Logic To Print Natural Numbers from 1 to N using While loop

We start by assigning 1 to variable count. Now we ask the user to enter a positive number. Now while loop keeps executing until value of count is less than or equal to user entered value. Inside while loop we printout the value of count and then increment the value of count by one for each iteration of while loop.

This way we printout all the natural numbers from 1 to N.

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 GCD and LCM of Two Numbers using Euclidean algorithm

Lets write a C program to find GCD / HCF and LCM of Two user entered Numbers using Euclidean algorithm.

Full Forms

GCD: Greatest Common Divisor.
HCF: Highest Common Factor.
LCM: Least common Multiple.

Related Read:
while loop in C programming

Formula To Calculate LCM

Once we get the GCD, we use the below formula to calculate LCM.

LCM = ( num1 * num2 ) / GCD;

Source Code: C Program To Find GCD and LCM of Two Numbers using Euclidean algorithm

 
#include < stdio.h >

int main()
{
    int num1, num2, gcd, lcm, rem, numerator, denominator;

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

    if(num1 > num2)
    {
        numerator   = num1;
        denominator = num2;
    }
    else
    {
        numerator   = num2;
        denominator = num1;
    }

    rem = numerator % denominator;

    while(rem != 0)
    {
        numerator   = denominator;
        denominator = rem;
        rem         = numerator % denominator;
    }

    gcd = denominator;
    lcm = (num1 * num2) / gcd;

    printf("GCD of %d and %d is %d\n", num1, num2, gcd);
    printf("LCM of %d and %d is %d\n", num1, num2, lcm);

    return 0;
}

Output
Enter 2 integer numbers
15
20
GCD of 15 and 20 is 5
LCM of 15 and 20 is 60

C Program To Find GCD and LCM of Two Numbers using Euclidean algorithm


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

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


Logic To Find GCD and LCM of Two Numbers using Euclidean algorithm

We find biggest of the 2 numbers entered by the user. Biggest number is assigned to variable numerator and smaller number is assigned to variable denominator. Now reminder is calculated. If the reminder is not equal to zero, then the value of variable denominator is assigned to variable numerator and the value of variable reminder is transferred to variable denominator and once again reminder is calculated – until reminder is zero. Once reminder is zero, gcd will be present in variable denominator.

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 GCD and LCM of Two Numbers

Lets write a C program to find GCD(Greatest Common Divisor) or HCF(Highest common Factor) and LCM(Least Common Multiple) of 2 user entered integer numbers.

Related Read:
while loop in C programming
Relational Operators In C
Logical Operators In C

GCD or HCF: Largest Integer that can divide both the numbers without any remainder or with 0 as remainder.
C Program to Find GCD or HCF of Two Numbers

Least Common Multiple(LCM): is the smallest positive integer that is perfectly divisible by the given integer values.
C Program to Find LCM of Two Numbers
Biggest of Two Numbers Using Ternary Operator: C

Logic To Find GCD and LCM of Two Integer Numbers.

We ask the user to enter 2 integer numbers. Next we find the smallest number among the two. Example, if num1 = 2 and num2 = 3. We can’t have any number bigger than 2, which will divide num1 and have reminder as 0.

Further explanation to find GCD or HCF is present in detail in this article: C Program to Find GCD or HCF of Two Numbers

Formula To Calculate LCM

Once we get the GCD, we use the below formula to calculate LCM.

LCM = ( num1 * num2 ) / GCD;

Source Code: C Program To Find GCD and LCM of Two Numbers

 
#include < stdio.h >

int main()
{
    int num1, num2, gcd, lcm, count = 1, small;

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

    small = (num1 < num2) ? num1 : num2;

    while(count <= small)
    {
        if(num1 % count == 0 && num2 % count == 0)
        {
            gcd = count;
        }
        count++;
    }

    lcm = ( num1 * num2 ) / gcd;

    printf("GCD = %d\nLCM = %d\n", gcd, lcm);

    return 0;
}

Output 1:
Enter 2 integer numbers
30
40
GCD = 10
LCM = 120

Output 2:
Enter 2 integer numbers
12
24
GCD = 12
LCM = 24

C Program To Find GCD and LCM of Two Numbers


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

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


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