C-For Loop In C Programming

In this tutorial you will come to know about the for loop in C - programming with the help of some examples .



The loops are used to execute a given block set of statements until a given condition becomes false .

The statements which are used to execute loops are known as looping statements .

Looping Statements :



These are also called iterative or repetitive statements. 

When we want to execute or print one statement for more than one time with a single specification then we use the concept of looping

For creating a looping statement we need components

Initialization:- from which the    variable  value     starts execution or printing.

Condition:- upto which the variable value  should goes.

 Incr/decr:- used to increase or decrease the value of the variable after each          execution.

For Loop In C Programming 


It is the simplest type of looping statement because here all the three parts of the loop written in one line , so it reduce the line of codes.



Syntax Of For Loop





for (initializationStatement; testExpression; updateStatement)
{
    // statements inside the body of loop
}




How For loop Works?



The initialized statement is executed only once .

Once the initialized statement is executed then the test expression is evaluated if it false then the loop will terminates .

Whereas if the test expression is evaluated true then the statements inside the loop is executed and body of the expression is updated .

Again the whole process will be followed .

The process of the updating the expression  continues until the condition becomes false and loops comes out.

Question-1 Write a program to print the series like 50,45,40,.... ,5 ?





#include <stdio.h>
int main()
{
int i; 
clrscr();
for(i=50;i>=5;i=i-5)
{
printf(“%d\n”,i);
}
return 0;
}







Question-2 Write a program to find hcf and lcm of two integer number ?



#include <stdio.h>
int  main()
{
int num1,num2,hcf,lcm,i;
clrscr();
printf(“enter two number”); 
scanf(“%d%d”,&num1,&num2); 
for(i=1;i<=num1,i<=num2;i++)
{
if(num1%i==0&&num2%i==0) 
hcf=i;
}
lcm=(num1*num2)/hcf;
printf(“hcf value=%d\n lcm value=%d”,hcf,lcm);
return 0;







Question-3  Write a program to display all the prime number between 1 to 100?



#include <stdio.h>
int main()
{
int i,j,c=0;
clrscr();
for(i=1;i<=100;i++)
{
for(j=1;j<=i;j++) 
if(i%j==0)
c++;
if(c==2) 
printf(“%d\n”,i);
}
return 0;
}







Question-4 Write a program to display all the even number and odd number  between 1 to 100?



#include <stdio.h>
int main()
{
int i,even=0,odd=1; 
clrscr(); 
for(i=1;i<=10;i++)
{ 
if(i%2==0)
even=even+i; 
else odd=odd*i;
}
printf(“even  sum=%d\n odd product=%d”,even,odd);
return 0;
}






Nested For Loop In C



When one or more than one for loops are nested inside another for loop then it is called nested for loop.

Nested Loop is possible in C also .

Multiple initialization  inside for loop in C

We can create multiple initialization in the single for loop of c .
This can be done as :-


for (i=1,j=1;i<10 && j<10; i++, j++)

Difference between multiple initialization for loop and simple for loop


 1. Multiple initialization takes two variables for initializing

 2. Both the variables will be separated by comma (,)

 3. It will have two test conditions which are joined together with the help of either And or OR

4. You must use logical operators to to join such conditions .

5. It will also have two parts in the incr/decr section .

6. Both incr/decr will be separated by Comma.




Let's take a look at some examples for applying above topics  :-

Question-1 Write a Program for Pyramid Series  using for loop?




#include <stdio.h>
int  main()
{
int i,j,n;
clrscr();
printf(“enter the row size\n” 
scanf(“%d”,&n); 
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++) 
printf(“%d”,j);
printf(“\n”);
}
 
return 0:
}






Output:-

1

1    2

1      2     3

1      2     3      4

1      2     3      4


Question-2  Write a program for reverse of pyramid series using for loop?




#include <stdio.h>
int main()
{
int i,j; 
clrscr();
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++) 
printf(“%d”,j);
printf(“\n”);
}
return 0;
}






Output:-

1
2
3
4
5
1
2
3
4

1
2
3


1
2



1









Question-3 Write a program for pyramid series ?




#include <stdio.h>
int main()
{
int i,j,k=1; 
clrscr();
for(i=1;i<=5;i++,k=k+2)
{
for(j=i;j<=4;j++) 
printf(“ “); 
for(j=1;j<=k;j++) 
printf(“*”);
printf(“\n”);
}
return 0;
}









Output:-

*
*      *     *
*     *      *      *     *
*      *     *      *      *      *     *
*     *      *     *      *     *      *      *     *






If you find anything inappropriate or want to give more knowledge about above topic you can comments us .





























C-For Loop In C Programming C-For Loop In C Programming Reviewed by Digital Roy on January 19, 2020 Rating: 5

No comments:

Powered by Blogger.