0% found this document useful (0 votes)
57 views

Lab Report 6

This program contains two parts: 1) A number guessing game that allows a user to guess a randomly generated number between 1 and 100. The program tells the user if their guess is too high, too low, or correct. 2) Four shapes are drawn using nested for loops and asterisks. The shapes demonstrate how for loops can be used to repeat actions and create simple patterns.

Uploaded by

Altaf
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

Lab Report 6

This program contains two parts: 1) A number guessing game that allows a user to guess a randomly generated number between 1 and 100. The program tells the user if their guess is too high, too low, or correct. 2) Four shapes are drawn using nested for loops and asterisks. The shapes demonstrate how for loops can be used to repeat actions and create simple patterns.

Uploaded by

Altaf
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

National University of Sciences and Technology (NUST)

SMME

LAB REPORT
LAB REPORT # 6

COURSE : Fundamentals of Programming

SUBMITTED BY : Altaf Ahmad (250282)

SUBMITTED TO: Ahmad Subhani

SECTION : ME-10 A

DATE : November 08, 2018


Program#1
Write and Execute a program to guess a number between 1 and 100:

Code:
Description:
This program sets a constant value which is to be guessed by the user. The user enters
different values and the program tells if it is greater or smaller than the value which is to be
guessed. If the user enters the correct value, it accepts and tells the user that their value is
correct. This program has a range of 1 to 100. So the user has to enter the values between 1
and 100.
Explanation:
After loading the preprocessor directives and the standard libraries, an integer variable
named input is declared. Then, the program outputs the message asking the user to enter a
value between 1 and 100. The program saves the input number in the input variable. Then a do
while loop is used. Inside the do while loop, another while loop is used which checks if the
number is within the range of 1 and 100. If not, then it prompts the user to enter a valid
number. Here the fixed number is chosen as 14. If the number is between 1 and 100, it checks if
the number is greater, less or equal to 14. If it is greater, the program tells the user that the
number is greater than the fixed number, if it is less, the program tells the user that it is less. If
the number is equal to 14, it tells the user that the number is equal to 14.
Output:

Explanation:
In the output, the program displays the message that enter a number between 1 and
100. At first the user entered 101, which is greater than 100, so it is outside the range. Then he
enters the number 0 which is less than 1, so it is also outside the range. Then the user enters 27
which is greater than 14. So, the program tells him so. Then, the user enters 13, which is less
than 14. And so, the program also tells him that it is less than required number. Finally, the user
enters 14. And the program tells him that his guess is correct.
Conclusion:
Since the program, tells the user if the digit is smaller, greater or outside the range from
the digit which is to be guessed, thus, it can be concluded that the main objective of the
program is completed. Also the importance of loops is shown in this program.
Flowchart:
Program#2
Write and Execute a program to draw 4 specific shapes using “*”:
Code:
Description:
The basic purpose of this program is to draw four specific designs using for loops by
repetition of “*”s. This program basically helps to understand how for loops and nested for
loops work.
Explanation:
After loading the preprocessor directives and the standard libraries, a for loop is created
inside which an integer y is defined with an initial value of 7. The for loop is set to run as long as
the value is greater than or equal to 0. And a decrement of 1 in y is to occur every time the loop
runs. Inside the for loop, another for loop is defined to make it a nested for loop. Inside this for
loop an integer z is defined with an initial value of 1. The loop is conditioned to run as long as
the value is less than or equal to y. And a steady increment of 1 is to occur every time the loop
runs. Inside the body of this for loop, a start is to be printed, which basically prints the shape.
Inside the first for loop, after the 2nd for loop, a line ending statement written to keep changing
the lines. Similarly, the other shapes are drawn but in some of them, the initial values of first for
loop variable is started from 1 and is increased. And another variable is also defined to use
spaces where needed. But the basic working of all the shapes is same. They are all created using
the same algorithm.
Output:
Explanation:
In the output, the required 4 shapes are drawn in sequence. For example, in the first
shape, seeing the code, we can see that at first the first for loop is executed, in which the 2nd for
loop executes and it keeps printing the stars in the first row up till the number of stars in the
first row is 7. And then line is ended. And since the value of y now becomes less, so lesser and
lesser stars will be printed in the lower lines. And in the same way the other shapes are created.
Conclusion:
All the shapes are drawn using for loops. So this program basically makes clear the
usefulness and application of for loop.

You might also like