Experiment No.6: 1.0 TITLE
Experiment No.6: 1.0 TITLE
6 Programming In ‘C’
EXPERIMENT NO.6
1.0 TITLE :
To understand Loop control statements in ‘C’.
Concept Structure:
The initialization statement is executed only once prior to the statements within the for loop. The
counter condition is executed once before execution of the statements within the loop. If this condition
is true, the statements within the loop are executed. If it is false, the loop terminates and the control
of execution is then transferred to the statement following the for loop. The update counter statement
is executed once after every execution of the statements within the loop.
Proposition 3: while statement:
This loop executes the condition before executing any of the statements in its body. This loop is
used when the number of times the loop is to be executed is not known in advance
Syntax:
The general syntax for while loop is
Initialize loop counter
while ( test counter)
{
Statement 1;
Statement 2;
:
:
Statement n;
Increment loop counter;
}
Concept Structure:
The loop is repeated as long as the test expression is true and when it becomes false the while loop
is terminated and program execution proceeds with the next statement immediately after the while
loop.
Proposition 4: do…. while statement:
This loop executes the condition after execution of the statements in its body. This loop is used
when at least once the statements has to be execute even if the test condition of do … while
statement false first time itself.
Syntax:
The general syntax for do… while loop is
do
{
Statement 1;
Statement 2;
:
:
Statement n;
} while ( test expression);
Concept Structure:
The loop is repeated as long as the test expression is true and when it becomes false the do…
while loop is terminated and program execution proceeds with the next statement immediately after
the do.. while loop.
Program code:
# include < stdio.h >
# include < conio.h >
main ( )
{
int i;
clrscr ( );
i=1;
while ( i<10)
{
printf (“ %4d”, i);
i++;
}
}
Output:
1 2 3 4 5 6 7 8 9
Program code:
# include < stdio.h >
# include < conio.h >
main ( )
{
int i;
clrscr ( );
i=1;
do {
printf (“ %4d”, i);
i++;
} while ( i<10);
}
Output:
1 2 3 4 5 6 7 8 9
6.0 EXERCISES:
A. Attempt Q… , Q… from the following. (Teacher shall allot the questions)
1. Write a program to calculate the sum of N element.
2. Write a program to print sum of the digits in the given number.
3. Write a program to calculate factorial of a given number.
4. Write a program to find sum of given series using while statement 1+3+5+7+…….
5. Write a program to find sum of given series using do while loop.
0+ 1 +3 +6 +10 +15 +22.
B. Attempt all questions and write the output of program in the space provided below.
1. # include <stdio.h>
main( )
{
int i=0;
for ( ; i ; )
printf( “ Hello………………..Welcome” );
}
2. # include<stdio.h>
main ( )
{
int j,j;
for ( j=0;j<4;j++)
{
for ( i=1;i<=4;i++)
{
printf( “ %4d”,i );
}
}
}
number2= number1+number2;
printf(“ \n%d ”, number2);
number1=fib;
}
getch( );
}
C. Attempt all questions, Debug following programs and write only errors if any in the
space provided.
1. # include<stdio.h>
main( )
{
int i=1,j=1;
for ( ; ; )
{
if (i>5)
break;
else
j+=i;
printf( “\n%d”, j);
i+= j;
}
}
2. # include<stdio.h>
main ( )
{
char x;
while(x=0;x<=255;x++)
printf(“ \n The character is %c”, x);
}
Signature of Teacher