Syntax : for(initalization;termination condition; increament)
FOR loop statement is defined by 3 parameters
1) Initialization of variable, which says that from which value loop will start
2) Termination condition, at what point loop should stop
3) Increament of variable,
for(i=2;i<=5;i++)
{
//Some code ....
}
above loop will run four times
loop start at i=2 and every time it increase value of "i" by 1
when "i" values becomes 6, lop will stop.
//Write a program to print 1 to N number, where 0<N<=100
#include<conio.h>
#include<stdio.h>
void main()
{
int n,i;
clrscr();
printf("Enter Value for N(0 - 100):");
scanf("%d",&n);
if(n>0 && n<=100)
{
for(i=1;i<=n;i++)
{
printf("%d ",i);
}
}
else
{
printf("Invalid input.\n Enter Value from 1 to 100\n");
}
getch();
}
O/P:
Enter Value for N(0 - 100):105
Invalid input.\n Enter Value from 1 to 100
Enter Value for N(0 - 100):15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
-----------------------------------------------------------------
//Find odd/even number from 0 - 100 and count frequency for the same
#include<conio.h>
#include<stdio.h>
void main()
{
int n=100,i,Total_Even=0,Total_Odd=0;
clrscr();
//Find Even Numbers
printf("Even Numbers:");
for(i=0;i<=100;i++)
{
if(i%2==0)
{
printf("%d ",i);
Total_Even+=1;
}
}
//Find Odd Numbers
printf("\n\nOdd Numbers:");
for(i=0;i<=100;i++)
{
if(i%2!=0)
{
printf("%d ",i);
Total_Odd+=1;
}
}
prinft("\n\nTotal Even No:%d\n Total Odd No:%d",Total_Even,Total_Odd);
getch();
}
FOR loop statement is defined by 3 parameters
1) Initialization of variable, which says that from which value loop will start
2) Termination condition, at what point loop should stop
3) Increament of variable,
for(i=2;i<=5;i++)
{
//Some code ....
}
above loop will run four times
loop start at i=2 and every time it increase value of "i" by 1
when "i" values becomes 6, lop will stop.
//Write a program to print 1 to N number, where 0<N<=100
#include<conio.h>
#include<stdio.h>
void main()
{
int n,i;
clrscr();
printf("Enter Value for N(0 - 100):");
scanf("%d",&n);
if(n>0 && n<=100)
{
for(i=1;i<=n;i++)
{
printf("%d ",i);
}
}
else
{
printf("Invalid input.\n Enter Value from 1 to 100\n");
}
getch();
}
O/P:
Enter Value for N(0 - 100):105
Invalid input.\n Enter Value from 1 to 100
Enter Value for N(0 - 100):15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
-----------------------------------------------------------------
//Find odd/even number from 0 - 100 and count frequency for the same
#include<conio.h>
#include<stdio.h>
void main()
{
int n=100,i,Total_Even=0,Total_Odd=0;
clrscr();
//Find Even Numbers
printf("Even Numbers:");
for(i=0;i<=100;i++)
{
if(i%2==0)
{
printf("%d ",i);
Total_Even+=1;
}
}
//Find Odd Numbers
printf("\n\nOdd Numbers:");
for(i=0;i<=100;i++)
{
if(i%2!=0)
{
printf("%d ",i);
Total_Odd+=1;
}
}
prinft("\n\nTotal Even No:%d\n Total Odd No:%d",Total_Even,Total_Odd);
getch();
}
No comments:
Post a Comment