for loop


A for loop is a control statement using which the programmer can give instructions to the computer to execute a set of statement repeatedly as long as specified condition is satisfied. It is required to specify how many times a set of statements have to be executed it is also called counter controlled loop.
Syntax:-
  for(exp 1; exp 2; exp 3;)
 {
  statement....
 }
Here:-
for
is a keyword followed by three expression enclosed within parentheses.

Flowchart:-

for-loop-shineskill

When for loop is used:-

The for loop is always used when we know initial value final value and updating value,the updating value can be constantly incremented or decremented.

program of for loop shineskill

Output

1
2
3
4
5

W.A.P to print only even no b/w 1 to n.

 #include<stdio.h>
 #include<conio.h>
  void main()
  {
   int i, number;
   printf("\n enter the number :\n ");
   scanf("%d", &number);
   printf("\n even Numbers between 1 and %d are : \n", number);
   for(i = 1; i <= number; i++)
  {
   if ( i % 2 == 0 )
  {
   printf(" %d\t", i);
  }
  }
   getch();
  }

Output

enter the number:
10
even numbers between 1 and 10 are :
2 4 6 8 10

W.A.P to print sum of 1 to n.

 #include<stdio.h>
 #include<conio.h>
   int main()
  {
   int n, count, sum = 0;
   printf("enter the value of n(positive integer):\n ");
   scanf("%d",&n);
   for(count=1; count <= n; count++)
  {
   sum = sum + count;
  }
   printf("Sum of first %d natural numbers is: %d",n, sum);
   return 0;
   getch();
  }

Output

enter the value of n:
5
sum of first natural numbers is:15

W.A.P to print sum of only even no b/w 1 to n.

 #include<stdio.h>
 #include<conio.h.
  int main()
  {
   int i, n, sum=0;
   printf("enter the number: \n");
   scanf("%d", &n);
   for(i=2; i<=n; i+=2)
  {
   sum += i;
  }
   printf("Sum of all even number between 1 to %d = %d", n, sum);
   return 0;
   getch();
  }

Output

enter the number:
10
sum of all even number between 1 to 10=30

W.A.P to print table of any number.

 #include<stdio.h>
 #include<conio.h>
   int main()
  {
   int num, i, tab;
   printf("Enter the number:\n ");
   scanf("%d", &num);
   printf("\nTable of %d is:\n", num);
   for(i=1; i<=10; i++)
  {
   tab = num*i;
   printf("%d * %2d = %2d\n", num, i, tab);
  }
   getch();
   return 0;
  }

Output

Enter the number:
2 4 6 8 10 12 14 16 18 20

W.A.P to print factorial of a number.

 #include<stdio.h>
 #include<conio.h>
   int main()
  {
   int c, n, f = 1;
   printf("Enter a number to calculate its factorial\n");
   scanf("%d", &n);
   for (c = 1; c <= n; c++)
   f = f * c;
   printf("Factorial of %d = %d\n", n, f);
   return 0;
  }

Output

Enter a number to calculate its factorial
6
Factorial of 6 = 720

W.A.P to print numbers from 10 to 1.

 #include<stdio.h>
 #include<conio.h>
  void main()
  {
   int n;
   clrscr();
   printf("\n");
   for(n=10;n>=1;n--)
  {
   printf(" %d",n);
  }
   getch();
  }

Output

10 9 8 7 6 5 4 3 2 1

W.A.P to print cube upto given number

 #include<stdio.h>
 #include<conio.h>
  void main()
  {
   int i,ctr;
   printf("Input number of terms :\n ");
   scanf("%d", &ctr);
   for(i=1;i<=ctr;i++)
  {
   printf("Number is : %d and cube of the %d is :%d \n",i,i, (i*i*i));
  }
   getch();
  }

Output

Input number of terms:
5
Number is : 1 and cube of the 1 is :1
Number is : 2 and cube of the 2 is :8
Number is : 3 and cube of the 3 is :27
Number is : 4 and cube of the 4 is :64
Number is : 5 and cube of the 5 is :125

W.A.P to read 10 numbers from keyboard and print their sum and average

 #include<stdio.h>
 #include<conio.h>
  void main()
  {
   int i,n,sum=0;
   float avg;
   printf("Input the 10 numbers : \n");
   for (i=1;i<=10;i++)
  {
   printf("Number-%d :",i);
   scanf("%d",&n);
   sum +=n;
  }
   avg=sum/10.0;
   printf("The sum of 10 no is : %d\nThe average is : %f\n",sum,avg);
   getch();
  }

Output

Input the 10 numbers :
Number-1 : 1
Number-2 : 2
Number-3 : 3
Number-4 : 4
Number-5 : 5
Number-6 : 6
Number-7 : 7
Number-8 : 8
Number-9 : 9
Number-10 :10
The sum of 10 no is : 55
The average is : 5.500000

W.A.P to display the n terms of harmonic series and their sum.

 #include<stdio.h>
 #include<conio.h>
  void main()
  {
   int i,n;
   float s=0.0;
   printf("Input the number of terms : ");
   scanf("%d",&n);
   printf("\n\n");
   for(i=1;i<=n;i++)
  {
   if(i {
   printf("1/%d + ",i);
   s+=1/(float)i;
  }
   if(i==n)
  {
   printf("1/%d ",i);
   s+=1/(float)i;
  }
  }
   printf("\nSum of Series upto %d terms : %f \n",n,s);
   getch();
  }

Output

Input the number of terms: 5
1/1 + 1/2 + 1/3 + 1/4 + 1/5
sum of series upto 5 terms : 2.283334