For Loop in C language

“A little Progress each day adds up to big results”

Let’s start our today’s topic i.e For loop in C.

Today we our going to have a look on for loop in detail with some examples.

Hope you all must be excited to learn new and interesting topic today.

Soo let’s start quickly 🙂

What is for loop??

A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.

Syntax:-
for ( initialization ; condition; increment )
{
statement(s);
}

Explanation:-
1) The initialization step is executed first, and only once. This step allows you to declare and initialize any loop control variables. You are not required to put a statement here, as long as a semicolon appears.
2) Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and the flow of control jumps to the next statement just after the ‘for’ loop.
3) After the body of the ‘for’ loop executes, the flow of control jumps back up to the increment statement. This statement allows you to update any loop control variables. This statement can be left blank, as long as a semicolon appears after the condition.
4) The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then increment step, and then again condition). After the condition becomes false, the ‘for’ loop terminates.

Flowchart:-

The flowchart below will make my concept more clear to you all 👇

This is all the explanation of for loop. I hope you all must have got an idea about how exactly the loop works..
Now will move towards some examples of for loop…… .

Q1) A program to print numbers from 1 to 10

#include<stdio.h>
main ()
{
int i;
for(i=1;i<=10;i++)
{
printf("value is: %d\n", i);
}
}

Q2) A C program to find the sum of first 10 natural numbers.

#include<stdio.h>
main ( )
{
int i, sum=0;
for(i=1;i<=10;i++)
{
sum=sum + i;
}
printf(“Sum value is: %d\n", sum);
}

Q3) Write a program to print factorial of a given Numbers.

#include<stdio.h>
main ( )
{
int i, n, fact=1;
printf(“Enter the number”);
scanf(“%d”, &n);
for(i=1; i<=n; i++)
{
fact=fact*i;
}
printf(“Factorial is %d”,fact);
}

Q4) Write a C program to display multiplication tables of n having 10 multiples.

#include<stdio.h>
main ()
{
int num,i;
printf(“enter number”);
scanf(“%d”, &num);
for(i=1;i<=10;i++)
{
ans=n*i;
printf(“%d * %d= %d”, n, i, ans);
}
}

Q5) Write a ‘C’ program to display n terms of the Fibonacci series.

#include<stdio.h>
main()
{
int n,a=0,b=1,c,i;
printf(“Enter no of terms”);
scanf(“%d”,&n);
printf(“%d %d” ,a, b);
for (i=1;i<=n-2;i++)
{
c=a + b;
a=b;
b=c;
printf (“%d”, c);
}
}

Note:-If we write printf in the loop, then printf statement will execute that many times the loops get executed. And if we write printf out of the loop, it will execute only once and it will display the final answer.

Like this by using for loop we can write many programs.

Soo that’s it for today….. If you have any queries…..feel free to ask. Will meet in next article.. Till then good bye have a wonderful day ahead 🙂

You may also like...

2 Responses

  1. Yogesh Meher says:

    Nice article, it will be great if you can share Output of the Program below it

  1. January 13, 2022

    […] last article – For Loop in C language we have seen For Loop in […]

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: