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

Lets write a C program to find Least Common Multiple (LCM) of 2 integer numbers.

Related Read:
while loop in C programming
Biggest of Two Numbers Using Ternary Operator: C

Least Common Multiple(LCM): is the smallest positive integer that is perfectly divisible by the given integer values.

Logic to Find LCM of Two Numbers

We assign variable fact with a initial value of 1. While loop keeps executing until fact value is 0. We ask the user to enter 2 integer numbers. Now we find the biggest of these 2 numbers and store it inside variable lcm. We can even skip this step – in which case while loop has to execute more number of times. To optimize the code we find biggest of the 2 user input numbers and store it inside variable lcm – we do this because, we need to find a number which is perfectly divisible by both the user entered number, so obviously the number should be greater than or equal to the biggest number entered by the user.

Next, inside while loop we check if the value present in variable lcm is perfectly divisible by both num1 and num2. Once the condition is true, we display the result on to the console window and assign value 0 to variable fact. Once fact is 0, control exits while loop.

If the value of lcm is not perfectly divisible by both num1 and num2, then we increment the value of variable lcm by 1 and check for divisibility once again. We keep executing the while loop and keep incrementing the value of lcm by 1 until the value present in lcm is perfectly divisibile by value of num1 and num2.

Source Code: C Program to Find LCM of Two Numbers

 
#include < stdio.h >

int main()
{
    int num1, num2, lcm, fact =1;

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

    lcm = (num1 > num2) ? num1 : num2;

    while(fact)
    {
        if(lcm % num1 == 0 && lcm % num2 == 0)
        {
            printf("LCM of %d and %d is %d\n", num1, num2, lcm);
            fact = 0;
        }
        lcm++;
    }

    return 0;
}

Output 1:
Enter 2 numbers
24
60
LCM of 24 and 60 is 120

Output 2:
Enter 2 numbers
2
3
LCM of 2 and 3 is 6

C Program to Find LCM of Two Numbers


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

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


Explanation With Example

If 2 numbers are num1 = 2 and num2 = 3.
We find biggest of these two numbers and store it inside variable lcm.
So 3 is greater than 2, so lcm = 3.

Iteration 1
fact = 1;
lcm = 3;

lcm % num1 = 3 % 2 = 1 // not equal to 0
lcm % num2 = 3 % 3 = 0 // equal to 0

Increment value of lcm by 1. So lcm = 4.

Iteration 2
fact = 1;
lcm = 4;

lcm % num1 = 4 % 2 = 0 // equal to 0
lcm % num2 = 4 % 3 = 1 // not equal to 0

Increment value of lcm by 1. So lcm = 5.

Iteration 3
fact = 1;
lcm = 5;

lcm % num1 = 5 % 2 = 2 // not equal to 0
lcm % num2 = 5 % 3 = 1 // not equal to 0

Increment value of lcm by 1. So lcm = 6.

Iteration 3
fact = 1;
lcm = 6;

lcm % num1 = 6 % 2 = 0 // equal to 0
lcm % num2 = 6 % 3 = 0 // equal to 0

Since both the condition are true
i.e., lcm%num1 == 0 && lcm%num2 == 0

We printout the value of variable lcm i.e., 6 as the Least Common Multiple of numbers 2 and 3. Next we assign 0 to variable fact. Since fact is 0, control exits while 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 Expert0

C Program to Find GCD or HCF of Two Numbers

Lets write a C program to find Greatest Common Divisor(GCD) or the Highest Common Factor(HCF) of 2 integer numbers entered by the user.

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.

Logic to Find GCD or HCF of Two Numbers

Variable count is initialized to 1. Next, we ask the user to enter 2 integer numbers. We check the smallest of the 2 numbers and we iterate the while loop until variable count is less than or equal to this smallest number.

Inside while loop we keep incrementing the value of count by one for every iteration. We also check if both numbers entered by the user is perfectly divisible by the value present in count. If we get any such number, we store it inside the variable gcd. And after the control exits while loop we print the value present in variable gcd.

Source Code: C Program to Find GCD or HCF of Two Numbers

 
#include < stdio.h >

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

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

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

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

    return 0;
}

OR

 
#include < stdio.h >

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

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

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

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

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

    return 0;
}

Output 1:
Enter 2 positive integers
24
60
GCD or HCF of 24 and 60 is 12

Here we check the smallest number among the two numbers entered by the user. We iterate through the while loop until value of count is less than or equal to that smallest number of the 2 numbers entered by the user.
Biggest of Two Numbers Using Ternary Operator: C

C Program to Find GCD or HCF of Two Numbers


[youtube https://www.youtube.com/watch?v=4ZbDj6BrM-Q]

YouTube Link: https://www.youtube.com/watch?v=4ZbDj6BrM-Q [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

C Program To Find Factorial of a Number

Write a C program to find Factorial of a user input number, using while loop.

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

Factorial of a number is the product of all the numbers preceding it. For example, Factorial of 5 is 120 (1 x 2 x 3 x 4 x 5 = 120).

In general, n objects can be arranged in n(n – 1)(n – 2) … (3)(2)(1) ways. This product is represented by the symbol n!, which is called n factorial. By convention, 0! = 1.

Source Code: C Program To Find Factorial of a Number

 
#include<stdio.h>

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

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

    while(count <= num)
    {
        fact = fact * count; // fact *= count;
        count++;
    }

    printf("Factorial of %d is %d\n", num, fact);

    return 0;
}

Output 1:
Enter a number to find factorial
4
Factorial of 4 is 24

Output 2:
Enter a number to find factorial
5
Factorial of 5 is 120

Output 3:
Enter a number to find factorial
6
Factorial of 6 is 720

Output 4:
Enter a number to find factorial
8
Factorial of 8 is 40320

Output 5:
Enter a number to find factorial
10
Factorial of 10 is 3628800

C Program To Find Factorial of a Number using While Loop


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

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


Logic To Find Factorial of a Number

If user enters num as 4. Then here are the values of variable count and fact for each iteration of while loop.

Iteration 1
count = 1;
fact = 1;

fact = fact * count;
fact = 1 * 1;

count = 2; // value of count increments by 1 for each iteration.
fact = 1;

Iteration 2
count = 2;
fact = 1;

fact = fact * count;
fact = 1 * 2;

count = 3; // value of count increments by 1 for each iteration.
fact = 2;

Iteration 3
count = 3;
fact = 2;

fact = fact * count;
fact = 2 * 3;

count = 4; // value of count increments by 1 for each iteration.
fact = 6;

Iteration 4
count = 4;
fact = 6;

fact = fact * count;
fact = 6 * 4;

count = 5; // value of count increments by 1 for each iteration.
fact = 24;

Now the value of count is 5, which is greater than the user input number 4. So the control exits while loop. We print the value present inside variable fact as the Factorial of the number. So in this case, 24 is the Factorial of number 4.

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