Two laboratory exercises focused on using for-loops and while-loops in programming. Exercise 1 covers printing a diamond, generating fibonacci numbers, and printing the squares of integers from 1 to 20. Exercise 2 involves rewriting code using a while-loop, creating a password-protected system, and computing the greatest common divisor (gcd) of two numbers.
Download Laboratory Exercise: For-Loop and While Loop Programming and more Lab Reports Physics in PDF only on Docsity!
LABORATORY EXERCISE NO. 8 AND 9: FOR-LOOP AND WHILE LOOP
PROGRAMMING
LABORATORY EXERCISE FORMAT
EXERCISE 1
Use for loops to print a diamond like the one below.
The Fibonacci numbers are the sequence below, where the first two numbers are 1, and each number thereafter is the sum of the two preceding numbers. Write a program that asks the user how many Fibonacci numbers to print and then prints that many.
Write a program that prints out a list of the integers from 1 to 20 and their squares. The output should look like this:
The GCD (greatest common divisor) of two numbers is the largest number that both are divisible by. Write a program that asks the user for two numbers and computes their gcd. (Hint: you can use Euclid’s Algorithm, Recursion or Loops). You can’t use math.gcd() function.