Вы находитесь на странице: 1из 8

Lab-04

CF-Zero Semester
Practice the following concepts in this lab
1. While Loop
2. For Loop
3. Do while loop
WHILE LOOP
A while loop statement repeatedly executes a target statement as long as a given condition is true.
Syntax

while(condition)
{
statement(s);
}
Here, statement(s) may be a single statement or a block of statements. The condition may be any
expression, and true is any non-zero value. The loop iterates while the condition is true.
When the condition becomes false, program control passes to the line immediately following the loop.

Figure-while loop flow control


Here, key point of the while loop is that the loop
will run only if the condition is true, in case the
condition is false the loop might not ever run.
When the condition is tested and the result is true
all statements inside body of the loop will be
executed and the condition is tested again. When
the condition is false the loop body will be skipped
and the first statement after the while loop will
be executed
CF-Lab prepared by NaumanShamim

Example-1

When the program is executed the


output will be as under

#include <stdio.h>

All numbers between 1 and 10 are


#include <conio.h>
void main(){
intnum=2;
printf(All numbers between 1 and 10 are \n);
while(num<10) {

2
3
4
5
6
7
8
9

printf(%d \n,num);
num++;
}
getch();
}

TASK-1
Try to use the above program and print numbers between two given numbers A and B, the value of A and B will be
acquired from the user. To make the program easier it can be supposed that A < B
TASK-2
Modify the program developed in task-1 and print the number between two given numbers A and B such that A
can be greater or smaller than B. You might require an if statement of some similar statement to get the desired
results. Try not to use two different loops.
TASK-3
Try to print even numbers between two given numbers A and B such that A can be greater or smaller than B
TASK-4
Write a program to print the following series upto N term , the value of N should be acquired from the user
1

15

31

CF-Lab prepared by NaumanShamim

63 . . .

FOR LOOP
A for loop is like while loop but it provides more control over number of times the loop should execute.
Syntax

for(init; condition; increment )


{
statement(s);
}

Figure-for loop flow control

CF-Lab prepared by NaumanShamim

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
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.

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.

Example -2

When the program is executed the output


will be as under

#include <stdio.h>

All numbers between 1 and 10 are

#include <conio.h>
void main(){
intnum;
printf(All numbers between 1 and 10 are \n);
for(num=2;num<10;num++) {
printf(%d \n,num);

2
3
4
5
6
7
8
9

}
getch();
}

Example-3
Printing even and odd series together, by using a for loop
#include <stdio.h>
#include <conio.h>
int main(){
printf(Odd

Even \n);

for(int a=1,b=2;a<=10;a+=2,b+=2)

Output of the program would be


Odd

printf(%d \t %d \n,a,b);
getch();
return 0;
}

CF-Lab prepared by NaumanShamim

Even

3
5
7
9

4
6
8
10

Example-4
A program that calculates the factorial of a given number N

#include <stdio.h>
#include <conio.h>
int main(){
int n, num=1,factorial=1;
printf(Enter value of N : )
scanf(%d,&n);
for(num=1;num<=n;num++){
factorial=factorial*num;
}
printf(The factorial of %d = %d ,n,factorial);
getch();
return 0;
}

Task-05
Try to write a program that gets a number from the user and prints whether the number is even or odd, the
program should do so 5 times, i.e. the program should get 5 numbers one at a time and print their even or odd
status. The expected output of the program should be as under
Expected Output
Enter a number : 5
Status : Odd
Enter a number :10
Status :Even
Enter a number :9
Status : Odd
Enter a number :15
Status : Odd
Enter a number :2
Status : Even

Task-06
If you are able to complete the above program , try to modify your program and count the number of even
numbers and odd numbers entered by the user. The expected output of the program should be as under.

CF-Lab prepared by NaumanShamim

Expected Output
Enter a number : 5
Status : Odd
Enter a number :10
Status :Even
Enter a number :9
Status : Odd
Enter a number :15
Status : Odd
Enter a number :2
Status : Even

Even Numbers : 2
Odd Numbers : 3

Example-5
This program gets a number from the user if the number provided is even the program will
terminate otherwise the program will get another number. The while loop here will terminate
only when an even number is provided

#include <stdio.h>
#include <conio.h>
int main()
{
int a=1;
while(a%2!=0){
printf(\n Enter Value of a = );
scanf(%d,&a);
}
printf(\nEnd of While loop);
getch();
return 0;
}

CF-Lab prepared by NaumanShamim

DO WHILE LOOP
A do-while loop is like a while loop expect

It has two parts, a do part and a while part

This is the only loop that ends with a semi colon

Body of the loop comes first so body of this loop executes at least once

do{
statement(s);
} while(condition);

Following is a do-while version of the program in example 5


Example -5 second version
#include <stdio.h>
#include <conio.h>
void main()
{
int a=1;
do{
printf(\n Enter Value of a = );
scanf(%d,&a);
} while(a%2!=0);
printf(\nEnd of do-while loop);
getch();
}
Task-07
Write a program that lets user enter an integer value between 1 and 10, the program validates the
input , if the value entered is between 1 and 10 the program prints the value entered otherwise the
program takes the input again , the program ends on legal values otherwise keep taking input.

Sample Program Output


Enter a number between 1 and 10 -->56
Number is outside range 1-10. Please re-enter
Enter a number between 1 and 10 -->6
The number is 6

CF-Lab prepared by NaumanShamim

Positive divisor of a number

B is positive divisor of a number A if and only if B divides A completely, in other words A / B has zero
remainder. In C we can check whether a number B is positive divisor of another number A by using
simple % operator. i.e.
1. A mod(2)=0 means A is an even number,
2. A mod(B)=0 means B divides A and is a positive divisor of A
3. In C A%B= A mod (B)
Example

17 % 5 = 2

(5 is not positive divisor of 17)

65 %13 = 0

(13 is positive divisor of 65 )

Task-08
By using % operator and for loop perform the following activities
a) Write a program that gets a number N from user and prints all positive divisors of N
b) Write a program that gets a number P from user, the program will check whether the number
is prime or not

Task-09
Re-write the number reverse program such that the program should allow user to enter a number
consisting of 3,4 or 5 digits. The programs should print the number in reverse. You can use any of
the loop.

Comparison of Loops
Write a program that allow user to enter two numbers A and B , the program should print all the
even numbers between these two numbers, to make the program simple it is supposed that A<B
Comparison Table (scale 1 to 10 , 1 very easy, 10 extremely difficult)

Sr.No.
1
2
3
4
5
6
7
8

Element
for loop
Number of comparison
Number of variables
Number of statement
Difficulty in developing program, (scale 1-10)
Clarity of program (scale 1 to 10)
Number of lines of program
Number of times body of loop executed
Loop control (scale 1 to 10)

CF-Lab prepared by NaumanShamim

While loop

do while loop

Вам также может понравиться