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: do…while Loop


CLO1: Understand the syntax and semantics of different programming
constructs

Instructor: Mr. Jaudat Mamoon

Lab Engineer: ---

CS110: Fundamentals of Computer Programming Page 1


do…while Loop

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

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

Tools/Software Requirement
MS Visual Studio

Description
the do...while loop in C programming checks its condition at the bottom of the loop.
A do...while loop is similar to a while loop, except the fact that it is guaranteed to execute at
least one time.
Syntax
The syntax of a do...while loop in C programming language is −
do {
statement(s);
} while( condition );
Notice that the conditional expression appears at the end of the loop, so the statement(s) in the
loop executes once before the condition is tested.
If the condition is true, the flow of control jumps back up to do, and the statement(s) in the loop
executes again. This process repeats until the given condition becomes false.

CS110: Fundamentals of Computer Programming Page 2


Flow Diagram

Example

#include <stdio.h>

int main () {

/* local variable definition */


int a = 10;

/* do loop execution */
do {
printf("value of a: %d\n", a);
a = a + 1;
}while( a < 20 );

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

CS110: Fundamentals of Computer Programming Page 3


value of a: 16
value of a: 17
value of a: 18
value of a: 19

Lab Tasks:
Using do…while loop only and the programming techniques that you have learned so far,
perform the following tasks:

1. Write a program that calculates and prints the average of several integers. Assume the last value
read with scanf() is the sentinel 9999. A typical input sequence might be

10 8 11 7 9 9999

indicating that the average of all the values preceding 9999 is to be calculated..

The screen dialogue should appear as follows:

2. Write a program that asks the user about the number of values he/she wants to enter.
Than enter the values as per the required number, calculate its sum and identify the
smallest value among them. The sample output is as follow:

Enter the number of values to be input: 5


Enter the number: 20
Enter the number: 10
Enter the number: 50
Enter the number: 4
Enter the number: 65

CS110: Fundamentals of Computer Programming Page 4


The sum is :149
The smallest value of entered numbers is 4

3. Write a program that enters numbers till the user wants. At the end, it should display the
count of positive, negative, zeros, and total numbers entered.

4. Write a program that should take pin code from user if the pin code matches it should
display the welcome message if not it should keep asking for pin after 4 tries it should
lock the account and displays the message please try again!!.

5. Working from left-to-right if no digit is exceeded by the digit to its left it is called an
increasing number; for example, 134468.

Similarly if no digit is exceeded by the digit to its right it is called a decreasing number;
for example, 66420.

We shall call a positive integer that is neither increasing nor decreasing a "bouncy"
number; for example, 155349.

Clearly there cannot be any bouncy numbers below one-hundred, but just over half of the
numbers below one-thousand (525) are bouncy. In fact, the least number for which the
proportion of bouncy numbers first reaches 50% is 538.

Write a program in C that prints on screen all the 4-digit bouncy numbers!

6. Write a program that plays an incredibly stupid number-guessing game. The user will try
to guess the secret number until they get it right. That means it will keep looping as long
as the guess is different from the secret number. You must store the secret number in a
variable, and use that variable throughout. The secret number itself must not appear in the
program at all, except in the one line where you store it into a variable. Sample output is
as following:

I have chosen a number between 1 and10. Try to guess it.


Your guess: 5

CS110: Fundamentals of Computer Programming Page 5


That is incorrect. Guess again.
Your guess: 4
That is incorrect. Guess again.
Your guess: 8
That is incorrect. Guess again.
Your guess: 6
That's right! You guessed it.

Task # 1(code):
//Hamid Muzaffar Khan
//BS-CS 10-B
//CMS ID = 356209
#include<stdio.h>
int main()
{
int num, count = 0, sum = 0;
float average;//Averagae can be in flaoting points
do{
printf("Enter Number(9999 to end) : ");
scanf("%d", &num);
if (num != 9999)//In order not to sum 9999
{
sum = sum + num; //To sum all numbers
count += 1;
}
} while (num != 9999);
if (count != 0)//If count==0 then error occur because sum cannot be divided by 0
{
average = (float)sum / count;
printf("Your average is = %f", average);
}

return 0;
}

Output: (On the next page)

CS110: Fundamentals of Computer Programming Page 6


Task # 2(code):
//Hamid Muzaffar Khan
//BS-CS 10-B
//CMS ID = 356209
#include<stdio.h>
int main()
{
int count = 2, range;
float num, sum = 0, small;//Number can be in decimal points
printf("Enter the number of values to be input : ");
scanf_s("%d", &range);
if (range != 0)//If range is 0 then no need for any number
{
printf("Enter Number 1: ");
scanf_s("%f", &num);
sum = sum + num;
small = num;//To standarize the numbers
}
do{
if (range != 0)//If range is 0 then no need for any number
{
printf("Enter Number %d: ", count);
scanf_s("%f", &num);
sum = sum + num;
if (small>num)

CS110: Fundamentals of Computer Programming Page 7


small = num;//If number is smaller than the previous one then store
it in small
}
count += 1;
} while (count <= range && range != 0);
if (range != 0)//If range is 0 then no need for printing.
{
printf("Sum = %f\n", sum);
printf("Smallest Number is %f\n", small);
}
return 0;
}

Output:

CS110: Fundamentals of Computer Programming Page 8


Task # 3(code):
//Hamid Muzaffar Khan
//BS-CS 10-B
//CMS ID = 356209
#include<stdio.h>
int main()
{
int count_p = 0, count_n = 0, count_o = 0;
float num;
char exit;
do{
printf("Enter Number : ");
scanf_s("%f", &num);//Number can be in decimal points

if (num>0)
{
count_p += 1;//count +ve-number
}
else if (num == 0)
{
count_o += 1;//count zeroes
}
else
{
count_n += 1;//count -ve-number
}
printf("Enter q to exit if you have done or y(small) to continue : ");
scanf_s(" %c", &exit);
} while (exit == 'y');//y means continue the loop. q and any other character will
end the loop
printf("Positive number = %d\nNegative number = %d\nzero = %d\n", count_p,
count_n, count_o);
return 0;
}

CS110: Fundamentals of Computer Programming Page 9


Output:

Task # 4(code):
//Hamid Muzaffar Khan
//BS-CS 10-B
//CMS ID = 356209
#include <stdio.h>
int main()
{
int count = 1, pin = 9876, pin_enter, flag;//flag is used for sentinal control and
pin is 9876
do{
printf("\nEnter Pin : ");
scanf_s("%d", &pin_enter);
if (pin_enter == pin)
{
printf("\n\"Welcome to your account\"\n");//if pin is correct
flag = 0;
}

CS110: Fundamentals of Computer Programming Page 10


else if (count == 4 && pin_enter != pin)
{
printf("\n\"Your Account is blocked.Restart program.\"\n");//if pin
is incorrect at 4th time
}
else
{
printf("\n\"Try Again\"\n");//if pin is correct 1st,2nd or 3rd time
}
count += 1;
} while (count <= 4 && flag != 0);
return 0;
}

Output:

CS110: Fundamentals of Computer Programming Page 11


Task # 6(code):
//Hamid Muzaffar Khan
//BS-CS 10-B
//CMS ID = 356209
#include <stdio.h>
int main()
{
int count = 1, num = 6, num_enter, flag;//flag is for sentinal control
printf("I have chosen a number from 1 to 10. Try to guess it.\n");
do{
printf("Your Guess = ");
scanf_s("%d", &num_enter);
if (num_enter == num)
{
printf("That's right! You guessed it.\n");//if numbers matches
flag = 0;
}
else
{
printf("That is incorrect. Guess again.\n");//if number doesn't
match
}
count += 1;
} while (count >= 1 && flag != 0);
return 0;

CS110: Fundamentals of Computer Programming Page 12


}

Output:

Task # 5(code):
//Hamid Muzaffar Khan
//BS-CS 10-B
//CMS ID = 356209
#include<stdio.h>
int main()
{
int count = 1, num = 1000, d1, d2, d3, d4;
do{
d1 = num / 1000;
d2 = num % 1000 / 100;//Seperate number's digit
d3 = num % 1000 % 100 / 10;
d4 = num % 1000 % 100 % 10;
// apply conditions in a way that if number is increasing or decreasing
// then it make the condition false and don't execute if block otherwise it
do
if (!((d1 >= d2 && d2 >= d3 && d3 >= d4) && (d1 != d2 || d2 != d3 || d3 !=
d4)) && (!((d1 >= d2 && d2 >= d3 && d3 >= d4) && (d1 != d2 || d2 != d3 || d3 != d4))) &&
(!(d1 == d2 && d2 == d3 && d3 == d4)))
printf("%d\n", num);
num += 1;

CS110: Fundamentals of Computer Programming Page 13


} while (num <= 9999);
return 0;
}

Output:
Note : This is the snap of only one part of console there are many numbers which can be seen by
executing the above code.

CS110: Fundamentals of Computer Programming Page 14

You might also like