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

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

Related Read:
For Loop In C Programming Language
C Program to Print Natural Numbers from 1 to N using for loop

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


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

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


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

 
#include<stdio.h>

int main()
{
    int num, count;

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

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

    for(count = num; count >= 1; count--)
    {
        printf("%d\n", count);
    }

    return 0;
}

Output:
Enter a positive number
14

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

Natural numbers from 10 to 1 are:

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

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

This way we printout 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 for loop

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

Related Read:
For Loop In C Programming Language

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

 
#include<stdio.h>

int main()
{
    int num, count;

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

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

    for(count = 1; count <= num; count++)
    {
        printf("%d\t", count);
    }

    printf("\n");

    return 0;
}

Output:
Enter a positive number
5

Natural numbers from 1 to 5 are:
1 2 3 4 5

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


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

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


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

We start by assigning 1 to variable count. Now we ask the user to enter a positive number. Now for loop keeps executing until value of count is less than or equal to user entered value. Inside for loop we printout the value of count and then increment the value of count by one for each iteration of 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 Calculate the Sum of Natural Numbers From 1 to N

Lets write a C program to calculate sum of all natural numbers from 1 to N, using while loop.

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

Source Code: C Program to Calculate the Sum of Natural Numbers From 1 to N

 
#include < stdio.h >

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

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


    while(count <= num)
    {
        sum = sum + count;
        count++;
    }
    printf("Sum = %d\n", sum);
    return 0;
}

Output 1:
Enter a positive number
5
Sum = 15

Output 2:
Enter a positive number
10
Sum = 55

OR

 
#include < stdio.h >

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

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

    printf("Sum of natural numbers from 1 to %d is:\n", num);
    while(count <= num)
    {
        sum = sum + count;
        printf("%d  ", count);
        count++;

        if(count > num)
        {
            printf(" = %d\n", sum);
        }
        else
        {
            printf("+ ");
        }

    }
    return 0;
}

Output 1:
Enter a positive number
5
Sum of natural numbers from 1 to 5 is:
1 + 2 + 3 + 4 + 5 = 15

Output 2:
Enter a positive number
10
Sum of natural numbers from 1 to 10 is:
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55

C Program to Calculate the Sum of Natural Numbers From 1 to N


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

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


Logic To Calculate the Sum of Natural Numbers From 1 to N

We ask the user to enter a positive number and store it in variable num. We assign 1 to variable count and 0 to variable sum.

In while loop we check for the condition: count is less than or equal to num. While loop keeps executing until that condition is true. Inside while loop we increment the value of count by one and also add the value of count to the previous value of sum. We keep doing this until count is less than or equal to num. Once that condition is false, control exits the while loop and we display the value of sum as output.

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 the Sum of Natural Numbers Between Range

Lets write a C program to calculate sum of all natural numbers between the user entered range of numbers, using while loop.

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

Related Read:
while loop in C programming
C Program to Print Natural Numbers Between Two Numbers using While loop

Source Code: C Program to Calculate the Sum of Natural Numbers Between Range

 
#include < stdio.h >

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

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

    printf("\n");

    while(min <= max)
    {
        sum = sum + min;
        min++;
    }
    printf("Sum = %d\n", sum);
    return 0;
}

Output 1:
Enter 2 positive numbers
1
5
Sum = 15

Output 2:
Enter 2 positive numbers
10
15
Sum = 75

OR

 
#include < stdio.h >

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

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

    printf("\n");

    while(min <= max)
    {
        sum = sum + min;
        printf("%d ", min);
        min++;

        if(min > max)
        {
            printf(" = %d\n", sum);
        }
        else
        {
            printf("+ ");
        }
    }

    return 0;
}

Output 1:
Enter 2 positive numbers
1
5

1 + 2 + 3 + 4 + 5 = 15

Output 2:
Enter 2 positive numbers
10
15

10 + 11 + 12 + 13 + 14 + 15 = 75

Output 3:
Enter 2 positive numbers
25
30

25 + 26 + 27 + 28 + 29 + 30 = 165

C Program to Calculate the Sum of Natural Numbers Between Range


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

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


Logic To Calculate the Sum of Natural Numbers Between Range

We ask the user to enter minimum and maximum number(i.e., the range) and we store it inside variable min and max. We check if variable min is less than or equal to max. Until this condition is true, while loop keeps iterating. Inside while loop we increment the value of min by one for each iteration. We also add the value of min to the value of sum on each iteration. At the end, when min is greater than max, control exits while loop and we printout the value of sum – which has the sum of all the numbers between the range entered by the user.

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 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