Tuesday, 5 October 2010

19. Some program using for loop

Here i give some program by using for loop.
Q1. Useing for loop printf 10 star.

/* Program 1 */
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
for(i=0;i<10;i++)
printf("* ");
getch();
}

Explanation:
When the program is execute initialization scction i=0, then it execute the condition (i<10). First time when i=0 then the condition is true so it execute the printf function and pint a star. After execute the printf function it will go back in the increment section and increment the value of i, then i=1. Then again execute the condition and work again and again. When i=10 the condition is false and it can not exicute the printf function. So that the output is :

* * * * * * * * * *

/* Program 2 */

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i,n;
printf("Enter the value : ");
scanf("%d",&n);
for(i=1;i<=n;i+=4)
{
for(i=1;i<=n;i+=4)
printf(" %d",i);
}
getch();
}
if n=10
Output:
1 5 9

/* Program 3 */

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i,n;
printf("Enter the value : ");
scanf("%d",&n);
for(i=0;n>=i;n-=4)
{
printf(" %d",n);
}
getch();
}
if n=20
Output is:
20 16 12 8 4 0
Post by Arnob



18. Create Nested Loops

When the body of one loop contains another, the second is said to be nested inside the first. Any of C’s loops may be nested within any other loop. The ANSL C standard specifies that loops may be nested at least 15 levels deep. However, most compilers allow nesting to virtually any level. As a simple example of nested for, this fragment prints the numbers 1 to 10 on the screen ten times,

for (i=0;i<10;i++)
{
  for (j=1;j<11;j++)  /* nested loop */
  printf(“%d”,j);
  printf(“\n”);
}

Examples:

** This program used three for loops to print the alphabet three times, each time printing each latter twice;

#include<stdio.h>
int main(void)
{
  int I,j,k;
  for(i=0;i<3;i++)
    for(j=0;j<26;j++)
      for(k=0;k<2;k++)
      printf(“%c”,’A’+j);
return 0;
}

The statement
Printf(“%c”,’A’+j);
Works because ASCII codes for the letters of the alphabet are strictly ascending-each one is greater than the letter that precedes it.

(Taken from “Teach yourself C”)

Sunday, 3 October 2010

17. Use The For Loop

The for loop is one of C's three statements. It allows one or more statements to be repeated. The for  loop is considered by many C programmers to be its mast flexible loop. The for loop is used to repeat a statement or block of statements a specified number of tims. It's general form repeating a single statement is shown here.



for(initialization;conditionl-test;increnent or descrement)
statement;



The initialization scction is used to give an initial value to the variable that controls the loop. This varible is usually referred to as the loop-control. The initialization section is executed only before the loop be gins. Then execute the conditional-test. If the condition true then the program execute the statement. When the statement is end then the program come back in the increment statement. After increment test the condition if the condition is true then the program executed the statement. It will be continue. When the condition will be wrong the for loop is end.



Example:
As a simple first example. This program use a for loop to print the numbers 1 through 10 on the screen.

#include<stdio.h>
void main()
{
int num;
for(num=1; num<11; num=num+1)
printf("%d",num);
printf("terminating");
getch();
}

This program produces the following output:
1 2 3 4 5 6 7 8 9 10 treminating.
The program works works like this :
First, the loop control variable num is initialixed to 1. Next, the cxpession num<11 is evaluated. Since it is true, the for loop begins running, After the number is printed, num is incremented by one and the conditional test is evaluated again. This provcess continues until num equals 11. When this happens the for loop stops, and terminating is displayed, keep is mind that the initialixation portion of the for loop is only excecuted once, when the loop is first entered.
Post by Anrob.