C Program To Calculate the Sum of Natural Numbers From 1 to N using For Loop

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

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

Video Tutorial: C Program to Calculate the Sum of Natural Numbers From 1 to N using For Loop


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

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


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

 
#include<stdio.h>

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

    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\n", count);
        sum   = sum + count;
    }

    printf("\nSum of natural numbers from 1 to %d is: %d\n", num, sum);

    return 0;
}

Output 1:
Enter a positive number
5

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

Sum of natural numbers from 1 to 5 is: 15

Output 2:
Enter a positive number
10

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

Sum of natural numbers from 1 to 10 is: 55

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.

For Loop
Variable count is assigned value 1. For loop executes until count value is less than or equal to the user entered number. For each iteration of for loop, count value increments by 1.

Inside for loop, we display the value of count(which is natural number between 1 and user entered number). We also keep adding the value of count to the previous value of variable sum. Once the control exits for loop, sum will have the sum of all the natural numbers between 1 to user entered number.

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

For Loop In C Programming Language

Today lets learn about another loop control statement i.e., for loop. For loop is used to repeatedly execute certain set of instructions.

For loop allows us to specify 3 things in a single line:
1. Loop count initialization.
2. Condition Checking.
3. Modification Statement(increment/decrement statements).

Related Read
while loop in C programming
Relational Operators In C
Logical Operators In C
Video Tutorial: For Loop In C Programming Language


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

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

Source Code: For Loop In C Programming Language

#include<stdio.h>
int main()
{
    int i;

    for(i = 0; i < 10; i++)
    {
        printf("%d IBM\n", i );
    }

    return 0;
}

Output:
0 IBM
1 IBM
2 IBM
3 IBM
4 IBM
5 IBM
6 IBM
7 IBM
8 IBM
9 IBM

#include<stdio.h>
int main()
{
    int i;

    for(i = 0; i < 10; i++)
    {
        printf("%d IBM\n", i + 1);
    }

    return 0;
}

Output:
1 IBM
2 IBM
3 IBM
4 IBM
5 IBM
6 IBM
7 IBM
8 IBM
9 IBM
10 IBM

i = 1 and i<= 10

#include<stdio.h>

int main()
{
    int i;

    for(i = 1; i <= 10; i++)
    {
        printf("%d IBM\n", i);
    }

    return 0;
}

Output:
1 IBM
2 IBM
3 IBM
4 IBM
5 IBM
6 IBM
7 IBM
8 IBM
9 IBM
10 IBM

No initialization statement and No Modification Statement

#include<stdio.h>
int main()
{
    int i = 0;

    for(; i < 10;)
    {
        printf("%d IBM\n", i + 1);
        i = i + 1;
    }

    return 0;
}

Output:
1 IBM
2 IBM
3 IBM
4 IBM
5 IBM
6 IBM
7 IBM
8 IBM
9 IBM
10 IBM

No Curly braces and No initialization statement

#include<stdio.h>

int main()
{
    int i = 0;

    for(; (i < 10); i = i + 1)
        printf("%d IBM\n", i + 1);

    return 0;
}

Output:
1 IBM
2 IBM
3 IBM
4 IBM
5 IBM
6 IBM
7 IBM
8 IBM
9 IBM
10 IBM

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