For loop:- C language Program
For loop is the iterations that is used to execute a sequence of code until the given condition returns false. The best condition to use for loop is when the number of iterations is known in advance.
Synstex of the for loop
for(initialise; condition ; increment/decrement)
{
// Statements inside the body of loop;
}
#include(stdio.h)
#include(conio.h)
Int main()
{
int i;
clrscr();
for(i= 1; i<11; i++)
{
Printf (“%d”, i);
}
getch();
}
Output:- 12345678910
Write a C-language program for Addition,Substraction,Multiplication and Division.
2- Write a c language program to calculate the sum of first n natural numbers
#include(stdio.h)
#include(conio.h)
Int main ()
{
int num, count, sum=0;
printf(“Enter a positive number”);
scanf(“%d”, &num);
for(count=1; count<=num; ++count)
{
sum +=count;
}
Printf(“sum=%d”,sum);
getch();
}
Output :- Enter a positive number: 10
Sum= 55
#include<stdio.h>
#include<conio.h>
int main ()
{
int i, f=1, num;
printf(“Enter a number:-”);
scanf(“%d”, &num);
for (i=1; i<=num; i++)
f=f*1;
printf (“Factorial of %d is: %d”, num, f);
getch ();
}
Output:-
Enter a number:- 5
Factorial of 5 is :- 120
4-Write a program in c language to print multiple of 2 from 2 to 20.
#include(stdio.h)
#include(conio.h)
int main()
{
int count;
for(count=1;count<=20; count++)
{
If(count%2==1)
{
continue;
}
printf(“%d”,count);
}
getch();
}
Output:- 2 4 6 8 10 12 14 16 18 20
#include(stdio.h)
#include(conio.h)
Int main()
{
int i, k, rows , blank;
printf(“ Enter the number of rows:”);
scanf(“%d”,&row);
blank = rows;
for(i=1; i<= rows ;i++)
{
for(k=1; k<blank; k++)
printf(“ ”);
blank- -;
for( k=1; k<= 2*i – 1 ; k ++)
printf(“ * ”);
printf(“ \n”);
}
getch();
}
Output:- Enter the number of rows : 4
*
* * *
* * * * *
For loop is the iterations that is used to execute a sequence of code until the given condition returns false. The best condition to use for loop is when the number of iterations is known in advance.
Synstex of the for loop
for(initialise; condition ; increment/decrement)
{
// Statements inside the body of loop;
}
1-Write a c-language program to print numbers from 1 to 10 using for loop.
![]() |
for loop |
#include(conio.h)
Int main()
{
int i;
clrscr();
for(i= 1; i<11; i++)
{
Printf (“%d”, i);
}
getch();
}
Output:- 12345678910
Write a C-language program for Addition,Substraction,Multiplication and Division.
2- Write a c language program to calculate the sum of first n natural numbers
#include(stdio.h)
#include(conio.h)
Int main ()
{
int num, count, sum=0;
printf(“Enter a positive number”);
scanf(“%d”, &num);
for(count=1; count<=num; ++count)
{
sum +=count;
}
Printf(“sum=%d”,sum);
getch();
}
Output :- Enter a positive number: 10
Sum= 55
3-Write a c language program of factorial program in c using for loop.
#include<stdio.h>
#include<conio.h>
int main ()
{
int i, f=1, num;
printf(“Enter a number:-”);
scanf(“%d”, &num);
for (i=1; i<=num; i++)
f=f*1;
printf (“Factorial of %d is: %d”, num, f);
getch ();
}
Output:-
Enter a number:- 5
Factorial of 5 is :- 120
4-Write a program in c language to print multiple of 2 from 2 to 20.
#include(stdio.h)
#include(conio.h)
int main()
{
int count;
for(count=1;count<=20; count++)
{
If(count%2==1)
{
continue;
}
printf(“%d”,count);
}
getch();
}
Output:- 2 4 6 8 10 12 14 16 18 20
5- Write a program in c language of a triangle pattern using for loop.
#include(stdio.h)
#include(conio.h)
Int main()
{
int i, k, rows , blank;
printf(“ Enter the number of rows:”);
scanf(“%d”,&row);
blank = rows;
for(i=1; i<= rows ;i++)
{
for(k=1; k<blank; k++)
printf(“ ”);
blank- -;
for( k=1; k<= 2*i – 1 ; k ++)
printf(“ * ”);
printf(“ \n”);
}
getch();
}
Output:- Enter the number of rows : 4
*
* * *
* * * * *
0 Comments