Friday, 24 July 2015

Loops-for

for is the most popular looping instruction.The for allows us to specify three things about a loop in a single line:

1.Setting a loop counter to an initial value
2.Testing the loop counter to determine whether its value has reached the number of repetitions desired
3.Increasing the value of loop counter each time the program segment within the loop has been executed.

syntax for for- loop

for(initialization; test counter; increment counter)
{
do this;
do this;
}


//program to print N numbers

#include<stdio.h>
#include<conio.h>
int main()
{
int i,n;
printf("Till which number do you want to print");
scanf("%d",&n);
for(i=0;i<=n;i++)
{
printf("%d\n",i);
}
  getch();
  return 0;
}

output:
Till which number do you want to print 4
0
1
2
3
4


//Calculation of simple interest for 3 sets of p, n, r
























Formats:

 for(i=10;         i;                    i--)
printf("%d",i);

for(i<4;                   j=5;            j=0)
printf("%d",i);


for(i=1;              i<=10 ;             printf("%d",i++))
;


No comments:

Post a Comment