Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Department of Computing: Lab Topic: For Loop

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

Department of Computing

CS110: Fundamentals of Computer Programming

Lab Topic: for loop

CLO1: Understand the syntax and semantics of different programming


constructs

Instructor: Mr. Jaudat Mamoon

Lab Engineer: ---

CS110: Fundamentals of Computer Programming Page 1


Lab: for loop

Introduction
The purpose of this lab is to get familiar with usage of for loop in C programming.

Objectives
The objective of this lab is to design solution using while loop.

Tools/Software Requirement
MS Visual Studio

Description
A for loop is a repetition control structure that allows you to efficiently write a loop that needs to
execute a specific number of times.

Syntax
The syntax of a for loop in C programming language is –

for ( init; condition; increment ) {


statement(s);
}

Here is the flow of control in a 'for' loop −


 The init step is executed first, and only once. This step allows you to declare and
initialize any loop control variables. You are not required to put a statement here, as
long as a semicolon appears.
 Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is
false, the body of the loop does not execute and the flow of control jumps to the next
statement just after the 'for' loop.
 After the body of the 'for' loop executes, the flow of control jumps back up to
the increment statement. This statement allows you to update any loop control
variables. This statement can be left blank, as long as a semicolon appears after the
condition.

CS110: Fundamentals of Computer Programming Page 2


 The condition is now evaluated again. If it is true, the loop executes and the process
repeats itself (body of loop, then increment step, and then again condition). After the
condition becomes false, the 'for' loop terminates.
Flow Diagram

Example

#include <stdio.h>

int main () {

int a;

/* for loop execution */


for( a = 10; a < 20; a = a + 1 ){
printf("value of a: %d\n", a);
}

CS110: Fundamentals of Computer Programming Page 3


return 0;
}

When the above code is compiled and executed, it produces the following result −
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19

Lab Tasks:
Using for loops, unless specified otherwise, perform the following tasks.

1. (Printing the Decimal Equivalent of a Binary Number) Input an integer containing only0s and 1s
(i.e., a “binary” integer) and print its decimal equivalent. [Hint: Use the remainder and division
operators to pick off the “binary” number’s digits one at a time from right to left. Just as in the
decimal number system, in which the rightmost digit has a positional value of 1, and the next
digit left has a positional value of 10, then 100, then 1000, and so on, in the binary number
system the rightmost digit has a positional value of 1, the next digit left has a positional value of
2, then 4, then 8, and so on. Thus the decimal number 234 can be described as 4 * 1 + 3 * 10 + 2
* 100.The decimal equivalent of binary 1101 is 1 * 1 + 0 * 2 + 1 * 4 + 1 * 8 or 1 + 0 + 4 + 8 or 13.]

2. (How Fast is Your Computer?) How can you determine how fast your own computer really
operates? Write a program with a for loop that counts from 1 to 300,000,000 by 1s. Every
time the count reaches a multiple of 100,000,000, print that number on the screen. Use your
watch to time how long each 100 million repetitions of the loop takes .

CS110: Fundamentals of Computer Programming Page 4


3. (Calculating the Value of π) Calculate the value of πfrom the infinite series.
π = 4 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11 +…
Print a table that shows the value of πapproximated by one term of this series, by two terms, by three terms, and
so on. How many terms of this series do you have to use before you first get 3.14?3.141? 3.1415? 3.14159?

4. Write a program that prints the following patterns separately one below the other. All asterisks
(*) should be printed by a single printf statement of the form printf( "*" ).[Hint: The
last two patterns require that each line begin with an appropriate number of blanks.]

CS110: Fundamentals of Computer Programming Page 5


Task # 1(Code):
//Hamid Muzaffar Khan
//CMS ID : 356209
//BS-CS 10-B
#include<stdio.h>
int main()
{
//This program is for any number of binary digit
int binary, base = 1, decimal = 0, digit;//binary is used to enter binary number
printf("Enter a binary number = ");
scanf_s("%d", &binary);
for (; binary >= 1;)//loop runs until binary number become zero
{
digit = binary % 10;//carry last gigit of binary
decimal = decimal + (digit * base);
binary = binary / 10;//eliminates last digit of binary
base = base * 2;//base increses by 2 in each cylcle according to number of
digit eliminmates in the next cylce
}
printf("Decimal conversion of binary is %d\n", decimal);
return 0;
}

Output:

CS110: Fundamentals of Computer Programming Page 6


Task # 2(Code):
Method 1
//Hamid Muzaffar Khan
//CMS ID : 356209
//BS-CS 10-B
#include<stdio.h>
int main()
{
int count;
for (count = 1; count <= 300000000; count++)
{
if (count == 100000000)//when count is 100000000 then it prints "Multiple
is 1"
printf("Multiple is 1\n");
else if (count == 200000000)//when count is 200000000 then it prints
"Multiple is 2"
printf("Multiple is 2\n");
else if (count == 300000000)//when count is 300000000 then it prints
"Multiple is 3"
printf("Multiple is 3\n");
}
//By calculating execution time and then divide it by three we can calculate the
time in which it counts 100000000
return 0;
}

Method 2

//Hamid Muzaffar Khan


//CMS ID : 356209

CS110: Fundamentals of Computer Programming Page 7


//BS-CS 10-B
#include<stdio.h>
int main()
{
int count;
for (count = 1; count <= 100000000; count++)//count to 100000000
{

}
printf("Multiple is 1\n");//when count is 100000000 then it prints given statement
for (; count <= 200000000; count++)//count to 200000000
{

}
printf("Multiple is 2\n");//when count is 200000000 then it prints given statement
for (count = 1; count <= 300000000; count++)//count to 300000000
{

}
printf("Multiple is 3\n");//when count is 300000000 then it prints given statement
return 0;
}

Output:

My computer counts 300000000 in 1.12sec so it counts 100000000 in 0.3733sec.

Task # 4(Code):

CS110: Fundamentals of Computer Programming Page 8


//Hamid Muzaffar Khan
//CMS ID : 356209
//BS-CS 10-B
#include<stdio.h>
int main()
{
//count_row is used for indicating row and count_column is used for column.
int count_col, count_row, gap;//gap is used for shape 3 and 4
for (count_row = 10; count_row >= 1; count_row--)//This loop is for shape 1.
{
for (count_col = 10; count_col >= count_row; count_col--)
{
printf("*");
}
printf("\n");
}
printf("\n");
for (count_row = 1; count_row <= 10; count_row++)//This loop is for shape 2
{
for (count_col = 10; count_col >= count_row; count_col--)
{
printf("*");
}
printf("\n");
}
printf("\n");
for (count_row = 1; count_row <= 10; count_row++)//This loop is for shape 3
{
for (count_col = 10; count_col >= count_row; count_col--)
{
printf("*");
}
printf("\n");
for (gap = 0; gap<count_row; gap++)
{
printf(" ");
}
}
printf("\n");
for (count_row = 10; count_row >= 1; count_row--)//This loop is for shape 4
{
for (gap = 1; gap <= count_row; gap++)
{
printf(" ");
}
for (count_col = 10; count_col >= count_row; count_col--)
{
printf("*");
}
printf("\n");
}
return 0;
}

CS110: Fundamentals of Computer Programming Page 9


Output:

Task # 3(Code):
//Hamid Muzaffar Khan
//BS-CS 10-B

CS110: Fundamentals of Computer Programming Page 10


//CMS = 356209
#include <stdio.h>
int main()
{
int count, i = 1;
double p = 0, l;
printf("Term pi\n");
for (count = 1; count <= 400000; count++)
{
l = (double)4 / i;
if (count % 2 == 0)//Count represent position number If position number is
even then it substract otherwise adds
{
p = p - l;
}
else
{
p = p + l;
}
i = i + 2;
printf("%d %f\n", count, p);
if (count == 119 || count == 1690 || count == 10736 || count ==
146063)//when count reaches any of these number then program stops and ask for some
character to continue
getchar();
}
return 0;
}

Output:

CS110: Fundamentals of Computer Programming Page 11


CS110: Fundamentals of Computer Programming Page 12
CS110: Fundamentals of Computer Programming Page 13
Result:

I got 3.14 at 119.

I got 3.141 at 1690.

I got 3.1415 at 10736.

I got 3.14159 at 146063.

CS110: Fundamentals of Computer Programming Page 14

You might also like