C Program To Generate Fibonacci Series using For Loop

Lets write a C program to generate Fibonacci Series, using for loop.

Related Read:
Fibonacci Series using While loop: C Program

First Thing First: What Is Fibonacci Series ?
Fibonacci Series is a series of numbers where the first two Fibonacci numbers are 0 and 1, and each subsequent number is the sum of the previous two. Its recurrence relation is given by Fn = Fn-1 + Fn-2.

Below are a series of Fibonacci numbers(10 numbers):
0
1
1
2
3
5
8
13
21
34

How Its Formed:
0 <– First Number (n1)
1 <– Second Number (n2)
1 <– = 1 + 0
2 <– = 1 + 1
3 <– = 2 + 1
5 <– = 3 + 2
8 <– = 5 + 3
13 <– = 8 + 5
21 <– = 13 + 8
34 <– = 21 + 13

Video Tutorial: C Program To Generate Fibonacci Series using For Loop


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

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


Source Code: C Program To Generate Fibonacci Series using For Loop

#include<stdio.h>

int main()
{
    int n1 = 0, n2 = 1, n3, count, num;

    printf("Enter the number of terms to be printed\n");
    scanf("%d", &num);

    printf("\n%d\n%d\n", n1, n2);

    for(count = 3; count <= num; count++)
    {
        n3 = n1 + n2;
        printf("%d\n", n3);

        n1 = n2;
        n2 = n3;
    }

    return 0;
}

Output:
Enter the number of terms to be printed
10

0
1
1
2
3
5
8
13
21
34

Logic To Generate Fibonacci Series using For Loop

We know that the first 2 digits in Fibonacci series are 0 and 1. So we directly initialize n1 and n2 to 0 and 1 respectively and print that out before getting into For loop logic. Since we’ve already printed two Fibonacci numbers, we assign 3 to loop control variable count and iterate through the for loop till count is less than or equal to the user entered number.

Inside For loop
As per definition of Fibonacci series: “..each subsequent number is the sum of the previous two.” So we add n1 and n2 and assign the result to n3 and display the value of n3 to the console.

Next, we step forward to get next Fibonacci number in the series, so we step forward by assigning n2 value to n1 and n3 value to n2.

For loop keeps repeating above steps until the count is less than or equal to the user entered number. If the user entered limit/count is 10, then the loop executes 8 times(as we already printed the first two numbers in the Fibonacci series, that is, 0 and 1).

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 Multiplication Table Using For Loop

Lets write a C program to print the multiplication table of the number entered by the user. The table should get displayed in the following form:

Example: Multiplication table of 29
29 x 1 = 29
29 x 2 = 58
29 x 3 = 87
29 x 4 = 116
29 x 5 = 145
29 x 6 = 174
29 x 7 = 203
29 x 8 = 232
29 x 9 = 261
29 x 10 = 290

Related Read:
Basic Arithmetic Operations In C
For Loop In C Programming Language

Simple Mathematical Trick — logical thinking

C Program To Print Multiplication Table Using For Loop


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

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


Source Code: C Program To Print Multiplication Table Using For Loop

 
#include<stdio.h>

int main()
{
    int num, count;

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

    printf("Multiplication table for %d is:\n", num);

    for(count = 1; count <= 10; count++)
    {
        printf("%d x %d = %d\n", num, count, (num*count));
    }

    return 0;
}

Output 1:
Enter a number
5
Multiplication table for 5 is:
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

Output 2:
Enter a number
10
Multiplication table for 10 is:
10 x 1 = 10
10 x 2 = 20
10 x 3 = 30
10 x 4 = 40
10 x 5 = 50
10 x 6 = 60
10 x 7 = 70
10 x 8 = 80
10 x 9 = 90
10 x 10 = 100

Output 3:
Enter a number
7
Multiplication table for 7 is:
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70

Output 4:
Enter a number
6
Multiplication table for 6 is:
6 x 1 = 6
6 x 2 = 12
6 x 3 = 18
6 x 4 = 24
6 x 5 = 30
6 x 6 = 36
6 x 7 = 42
6 x 8 = 48
6 x 9 = 54
6 x 10 = 60

Logic: Multiplication Table

We ask the user to enter a number. We initialize for loop counter to 1 and iterate through the loop until loop counter is less than or equal to 10. Inside for loop we multiply the user entered number with the loop counter variable to get the result of multiplication table.

You can also watch C Program To Print Multiplication Table Using While Loop video tutorial.

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 Generate All Combinations of 1, 2 and 3

Lets write a C program to generate all combinations of 1, 2 and 3 using for loop.

Related Read:
For Loop In C Programming Language
Nested For Loop In C Programming Language

Logic To Generate All Combinations of 1, 2 and 3

In this program we take 3 for loops. Nesting of for loops go 3 levels deep. For every iteration of outer most for loop, the inner for loop executes 3 times. Similarly, for every iteration of the inner for loop, the inner most for loop executes 3 times. At the end of 3 iteration of outer most for loop, all the combinations of numbers 1, 2 and 3 are generated.

Video Tutorial: C Program To Generate All Combinations of 1, 2 and 3


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

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


Source Code: C Program To Generate All Combinations of 1, 2 and 3, without Repetition

 #include<stdio.h>

int main()
{
    int i, j, k;

    for(i = 1; i <= 3; i++)
    {
        for(j = 1; j <= 3; j++)
        {
            for(k = 1; k <= 3; k++)
            {
                if( i != j && i != k && j != k)
                    printf("%d %d %d\n", i, j, k);
            }
        }
    }

    return 0;
}

Output:
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

Using a if condition inside inner most for loop, we make sure i, j and k value are not same – hence eliminating the chances of displaying repeated numbers.

Source Code: C Program To Generate All Combinations of 1, 2 and 3

#include<stdio.h>

int main()
{
    int i, j, k;

    for(i = 1; i <= 3; i++)
    {
        for(j = 1; j <= 3; j++)
        {
            for(k = 1; k <= 3; k++)
            {
                    printf("%d %d %d\n", i, j, k);
            }
        }
    }

    return 0;
}

Output:
1 1 1
1 1 2
1 1 3
1 2 1
1 2 2
1 2 3
1 3 1
1 3 2
1 3 3
2 1 1
2 1 2
2 1 3
2 2 1
2 2 2
2 2 3
2 3 1
2 3 2
2 3 3
3 1 1
3 1 2
3 1 3
3 2 1
3 2 2
3 2 3
3 3 1
3 3 2
3 3 3

Source Code: C Program To Generate All Combinations of 1, 2, 3 and 4

#include<stdio.h>

int main()
{
    int i, j, k, l;

    for(i = 1; i <= 4; i++)
    {
        for(j = 1; j <= 4; j++)
        {
            for(k = 1; k <= 4; k++)
            {
                for(l = 1; l <= 4; l++)
                {
                    printf("%d %d %d %d\n", i, j, k, l);
                }
            }
        }
    }

    return 0;
}

Output:
1 1 1 1
1 1 1 2
1 1 1 3
1 1 1 4
1 1 2 1
1 1 2 2
1 1 2 3
1 1 2 4
1 1 3 1
1 1 3 2
1 1 3 3
1 1 3 4
1 1 4 1
1 1 4 2
1 1 4 3
1 1 4 4
1 2 1 1
1 2 1 2
1 2 1 3
1 2 1 4
1 2 2 1
1 2 2 2
1 2 2 3
1 2 2 4
1 2 3 1
1 2 3 2
1 2 3 3
1 2 3 4
1 2 4 1
1 2 4 2
1 2 4 3
1 2 4 4
1 3 1 1
1 3 1 2
1 3 1 3
1 3 1 4
1 3 2 1
1 3 2 2
1 3 2 3
1 3 2 4
1 3 3 1
1 3 3 2
1 3 3 3
1 3 3 4
1 3 4 1
1 3 4 2
1 3 4 3
1 3 4 4
1 4 1 1
1 4 1 2
1 4 1 3
1 4 1 4
1 4 2 1
1 4 2 2
1 4 2 3
1 4 2 4
1 4 3 1
1 4 3 2
1 4 3 3
1 4 3 4
1 4 4 1
1 4 4 2
1 4 4 3
1 4 4 4
2 1 1 1
2 1 1 2
2 1 1 3
2 1 1 4
2 1 2 1
2 1 2 2
2 1 2 3
2 1 2 4
2 1 3 1
2 1 3 2
2 1 3 3
2 1 3 4
2 1 4 1
2 1 4 2
2 1 4 3
2 1 4 4
2 2 1 1
2 2 1 2
2 2 1 3
2 2 1 4
2 2 2 1
2 2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 2 2
4 2 2 3
4 2 2 4
4 2 3 1
4 2 3 2
4 2 3 3
4 2 3 4
4 2 4 1
4 2 4 2
4 2 4 3
4 2 4 4
4 3 1 1
4 3 1 2
4 3 1 3
4 3 1 4
4 3 2 1
4 3 2 2
4 3 2 3
4 3 2 4
4 3 3 1
4 3 3 2
4 3 3 3
4 3 3 4
4 3 4 1
4 3 4 2
4 3 4 3
4 3 4 4
4 4 1 1
4 4 1 2
4 4 1 3
4 4 1 4
4 4 2 1
4 4 2 2
4 4 2 3
4 4 2 4
4 4 3 1
4 4 3 2
4 4 3 3
4 4 3 4
4 4 4 1
4 4 4 2
4 4 4 3
4 4 4 4

We can use below condition inside inner most for loop to eliminate displaying repeated numbers. As shown in below code:

Source Code: C Program To Generate All Combinations of 1, 2, 3 and 4, without Repetition

#include<stdio.h>

int main()
{
    int i, j, k, l;

    for(i = 1; i <= 4; i++)
    {
        for(j = 1; j <= 4; j++)
        {
            for(k = 1; k <= 4; k++)
            {
                for(l = 1; l <= 4; l++)
                {
                    if(i != j && i != k && i != l && j != k && j != l && k != l)
                        printf("%d %d %d %d\n", i, j, k, l);
                }
            }
        }
    }

    return 0;
}

Output:
1 2 3 4
1 2 4 3
1 3 2 4
1 3 4 2
1 4 2 3
1 4 3 2
2 1 3 4
2 1 4 3
2 3 1 4
2 3 4 1
2 4 1 3
2 4 3 1
3 1 2 4
3 1 4 2
3 2 1 4
3 2 4 1
3 4 1 2
3 4 2 1
4 1 2 3
4 1 3 2
4 2 1 3
4 2 3 1
4 3 1 2
4 3 2 1

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 Sum of Series 1/1! + 2/2! + 3/3! + …. + n/n!

Lets write a C program to add first seven terms of the following series 1 / 1! + 2 / 2! + 3 / 3! + ….. + 7 / 7!

We’ve also written the C program to ask the user to enter the number of terms of the series that has to be added. You can find code to both of it below.

Related Read:
For Loop In C Programming Language
while loop in C programming
C Program To Find Factorial of a Number using For Loop

C Program To Find Sum of Series 1/1! + 2/2! + 3/3! + …. + n/n!


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

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


Source Code: C Program To Find Sum of Series 1/1! + 2/2! + 3/3! + …. + 7/7!

 

int main()
{
    int num = 1, count;
    float sum = 0.0, fact;

    while(num <= 7)
    {
        fact = 1;
        for(count = 1; count <= num; count++)
        {
            fact = fact * count;
        }

        sum = sum + (num / fact);

        num++;
    }

    printf("Sum of series is %f\n", sum);

    return 0;
}

Output:
Sum of series is 2.718056

In above code, while loop iterates from 1 to 7. For each iteration for loop calculates the factorial of the selected number – which is present in variable num. Outside for loop, we add the individual series elements. At the end of while loop execution, variable sum will have sum of 7 terms of the series.

Source Code: C Program To Find Sum of Series 1/1! + 2/2! + 3/3! + …. + n/n!

 

int main()
{
    int num = 1, count, limit;
    float sum = 0.0, fact;

    printf("Enter the number of terms\n");
    scanf("%d", &limit);

    while(num <= limit)
    {
        fact = 1;
        for(count = 1; count <= num; count++)
        {
            fact = fact * count;
        }

        sum = sum + (num / fact);

        num++;
    }

    printf("Sum of %d terms of series is %f\n", limit, sum);

    return 0;
}

Output 1:
Enter the number of terms
7
Sum of 7 terms of series is 2.718056

Output 2:
Enter the number of terms
8
Sum of 8 terms of series is 2.718254

In above code, we ask the user to enter the number of terms in the series that needs to be added. While loop iterates until the user entered number of times. Inside for loop we calculate the factorial of the selected number(number is selected by while loop and it’ll be present in variable num). Outside the for loop we add the individual series element. At the end of execution of while loop we’ll have sum of the series until user entered number of terms in the series.

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 using For Loop

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

Related Read:
For Loop In C Programming Language
C Program To Find Factorial of a Number using While Loop

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 using For Loop

 
#include<stdio.h>
int main()
{
    int num, count, fact = 1;

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

    for(count = 1; count <= num; count++)
    {
        fact = fact * count;
    }

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

    return 0;
}

Output 1:
Enter a number to find its Factorial
5
Factorial of 5 is 120

Output 2:
Enter a number to find its Factorial
4
Factorial of 4 is 24

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

Output 4:
Enter a number to find its Factorial
6
Factorial of 6 is 720

Output 5:
Enter a number to find its Factorial
0
Factorial of 0 is 1

C Program To Find Factorial of a Number using For Loop


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

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


Logic To Find Factorial of a Number using For Loop

If user enters num as 4. Then here are the values of variable count and fact for each iteration of for 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 for 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