for loop

for loop: The for loop is also entry-controlled loop.

Syntax:

for(initialization; condition; increment/decrement)

{

Body of loop

}

Initialization: in this part of the loop the variable is initialized such as i=0; and this is the control variable for the loop, which tells from where the loop is starting.

Condition: The condition is a relational expression and the value of the variable is evaluated using some condition. E.g: i<=5;

Increment/decrement: After the execution of the statements which are in the body of the loop the control will again transfer to the loop and then the value of the variable is incremented. This value can be incremented by one or more according to our need.

if we want to increment variable with one then we can use i++;

e.g:     

for(a=1;a<5;i=i+1)

{

printf(“c programming”);

}

It will print ‘c programming’ four times.

Programs:

 PROGRAM TO CHECK WHETHER A GIVEN NUMBER IS PRIME OR NOT

#include<stdio.h>

#include<conio.h>

void main()

{

int i,n,b=0;

clrscr();

printf(“Enter any number: “);

scanf(“%d”,&n);

if(n<=1)

printf(“Given number is not a prime number”);

else

{

for(i=2;i<n;i++)

{

if(n%i==0)

b=1;

}

if(b==0)

printf(“Given number is a prime number”);

else

printf(“Given number is not a prime number”);

}

getch();

}

Posted in C

Leave a Reply

Your email address will not be published. Required fields are marked *