Professional Documents
Culture Documents
Department of Computing: CLO1: Understand The Syntax and Semantics of Different Programming Constructs
Department of Computing: CLO1: Understand The Syntax and Semantics of Different Programming Constructs
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.
Example
#include <stdio.h>
int main () {
/* 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
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..
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:
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:
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:
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;
}
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;
}
Output:
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;
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.