You are on page 1of 6

Computer Fundamentals-I Roll No.

_________________ Exp 3: Page 1 of 6

Computer Fundamentals-I
Laboratory Session-3
For loop, while loop, do-while loop

In this Lab Session you will learn how to use different loops, specifically the for loop, while loop and do-
while loop. You will be graded on the basis of the exercise programs given at the end.

Following program computes the sum of first ten integers. It also computes the sum of squares of first ten
integers. Run the following program.

#include <stdio.h>

int main()
{
int iCounter;
int iSum=0;
int iSumSquared=0;

for( iCounter=1; iCounter<=10; iCounter++ )


{
iSum += iCounter;
iSumSquared += iCounter*iCounter;
}
printf("Sum of integers is %d\n", iSum );
printf("Sum of squares is %d\n", iSumSquared);
return 0;

Following program is from book. Type and run the program and try to understand what does it do? To end
the while-loop, you need to press Ctrl+Z key sequence which is called EOF.

#include <stdio.h>

int main()
{
int grade; /* one grade */
int aCount = 0; /* number of As */

int bCount = 0; /* number of Bs */


int cCount = 0; /* number of Cs */
int dCount = 0; /* number of Ds */
int fCount = 0; /* number of Fs */
Computer Fundamentals-I Exp 4: Page 2 of 6
printf( "Enter the letter grades.\n" );
printf( "Enter the EOF character to end input.\n" );
/* loop until user types Ctrl+Z key sequence */
while ( ( grade = getchar() ) != EOF ) {

/* determine which grade was input */


switch ( grade )
{
case 'A':
case 'a':
++aCount;
break;
case 'B':
case 'b':
++bCount;
break;
case 'C':
case 'c':
++cCount;
break;
case 'D':
case 'd':
++dCount;
break;
case 'F':
case 'f':
++fCount;
break;
case '\n':
case '\t':
case ' ':
break;
default:
printf( "Incorrect letter grade entered." );
printf( " Enter a new grade.\n" );
break;
}
}

/* output summary of results */


printf( "\nTotals for each letter grade are:\n" );
printf( "A: %d\n", aCount );
printf( "B: %d\n", bCount );
printf( "C: %d\n", cCount );
printf( "D: %d\n", dCount );

printf( "F: %d\n", fCount );

return 0;
}
Computer Fundamentals-I Exp 4: Page 3 of 6

Following program uses do-while loop to compute the average of student grades. -1 is used to end the
do-while loop.

#include <stdio.h>

int main()
{
int iCounter;
int iGrade;
int iTotal;

float fAverage;

iGrade = 0;
iTotal = 0;
iCounter = 0;

do
{
iTotal = iTotal + iGrade;
iCounter = iCounter + 1;

printf( "Enter Grade, -1 to end: " );


scanf("%d", &iGrade);
}
while(iGrade != -1);

--iCounter;

if ( iCounter != 0 )
{
fAverage = ( float ) iTotal / iCounter;
printf( "Total students are %d\n", iCounter );
printf( "Class fAverage is %.2f\n", fAverage );
}
else
{
printf( "No iGrades were entered\n" );
}

getch();

return 0;
}
Computer Fundamentals-I Exp 4: Page 4 of 6

Following program is an example of nested for-loop. Type and run the program and try to understand
what does it do?

#include <stdio.h>

int main()
{
int iCounter;

int i,j;

printf("Enter input: ");


scanf("%d", &iCounter);

for(i=0; i<iCounter; i++)


{
for(j=0; j<iCounter; j++)
{
printf("*");
}
printf("\n");
}
return 0;
}

Exercises

1) The Fibonacci numbers are integers defined as follows: The first two numbers are 1 and 1, and every
subsequent number is the sum of the preceding two numbers in the sequence.
i.e. f(n) = f(n-1)+f(n-2) f(0) = 1 and f(1) = 1

Thus the sequence starts like:


1 1 2 3 5 8 13 21 34 55...

Write a C program that outputs the first n Fibonacci numbers, where n can be specified by the user of
the program.

Fibonacci numbers are important in mathematics. Ratio of the two consecutive fibonacci numbers
converges to 1.618033988 which is called Golden Number.

2) Use nested for loop to make following pattern with “*”. Your program should input an integer input
from user which will decide the rows of the output pattern. Sample outputs are given below.
Computer Fundamentals-I Exp 4: Page 5 of 6
Enter input: 4
*
**
***
****

Enter input: 6
*
**
***
****
*****
******

3) Use nested for loop to make following pattern with “*”. Your program should input an integer input
from user which will decide the rows of the output pattern. Sample outputs are given below.

Enter input: 4
****
***
**
*

Enter input: 6
******
*****
****
***
**
*

4) Use nested for loop to make following pattern with “*”. Your program should input an integer input
from user which will decide the rows of the output pattern. Sample outputs are given below.
Enter input: 4
*
***
*****
*******
*****
***
*

Enter input: 6
*
***
*****
*******
*********
***********
*********
Computer Fundamentals-I Exp 4: Page 6 of 6
*******
*****
***
*

You might also like