Laboratory Exercise: For-Loop and While Loop Programming, Lab Reports of Physics

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.

Typology: Lab Reports

2020/2021

Uploaded on 11/01/2021

Tiel02
Tiel02 🇵🇭

5 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
LABORATORY EXERCISE NO. 8 AND 9: FOR-LOOP AND WHILE LOOP
PROGRAMMING
LABORATORY EXERCISE FORMAT
EXERCISE 1
1. Use for loops to print a diamond like the one below.
pf3
pf4

Partial preview of the text

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

  1. Use for loops to print a diamond like the one below.
  1. 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.
  2. Write a program that prints out a list of the integers from 1 to 20 and their squares. The output should look like this:
  1. 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.