0% found this document useful (0 votes)
6 views4 pages

Loops Problem Set: Programming Exercises

The document is a problem set focused on loops in programming, providing various tasks such as printing even numbers, checking for prime numbers, calculating factorials, and creating a simple chatbot. It includes specific instructions for using while and nested loops, as well as data structures to store and manipulate data. The problems are designed to enhance understanding of loops and data handling in programming.

Uploaded by

faiza adil
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

Loops Problem Set: Programming Exercises

The document is a problem set focused on loops in programming, providing various tasks such as printing even numbers, checking for prime numbers, calculating factorials, and creating a simple chatbot. It includes specific instructions for using while and nested loops, as well as data structures to store and manipulate data. The problems are designed to enhance understanding of loops and data handling in programming.

Uploaded by

faiza adil
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Loops – Problem Set

2025-08-02

• Write a program to print even numbers from 1 to a number taken as input. The endpoint (number
given as input) should be included in output if even. Which loop is the best choice to use?

• Use a while loop to solve this problem.


Write a program to check if a number is prime. A number is prime if it is not divisible by any
other number other than 1 and itself.
HINT: % operator is used to check the remainder of division. If a number is perfectly divisible by
another number, then the remainder is 0.
Start by assuming that the number is prime then check start checking if it has any factors
(numbers that are divisible by target number).

• Use a while loop to print the factorial of a given number.


Factorial of a number is simply that number multiplied by all the numbers below it. For example,
factorial of 6 (denoted as 6!) is:
6! = 6 × 5 × 4 × 3 × 2 × 1
HINT: Note that multiplication is commutative so we could also write:
6! = 1 × 2 × 3 × 4 × 5 × 6
Test your program with 5, 4, and 3 giving results 120, 24, and 6 respectively.

1
Loops – Problem Set

• Write a program that keeps taking input for names until user enters stop. The names should be
stored in an appropriate data structure.
Once all names have been input, output the total names that were input and print the last three
names (if names < 3, this means print all names).
Example:

1 Enter name (give 'stop' to stop): Izhar


2 Enter name (give 'stop' to stop): Ali
3 Enter name (give 'stop' to stop): Ayesha
4 Enter name (give 'stop' to stop): Fareed
5 Enter name (give 'stop' to stop): stop
6
7 You entered 4 names.
8
9 Last 3 names that you entered:
10 Ali, Ayesha, Fareed

• Consider the following record of four students:

ID Name Year Major


100 Izhar 1 Computer Science
101 Ali 3 Chemistry
102 Ruhan 2 Sociology
103 Ayesha 4 Anthropology

Use one or more appropriate data structures to store the details of these four students.
Use an appropriate loop to iterate through the data structure and print the details for each student
in a nice format.
In addition to given detail, also include in the output if the student is eligible to contest in school’s
president elections. Only 3rd or higher year students are eligible to contest in president elections.

2
Loops – Problem Set

• Common AI models like ChatGPT, Gemini, etc. are able to learn as you talk to them. They
consistently update their knowledge as you interact with them.
Although the system facilitating learning in these models is mathematically complex, lets
implement a dummy chatbot that is able to learn as it talks.
Use an appropriate data structure to store questions and their respective responses. You can start
with an empty data structure. This will be your ‘knowledge base.’
The program should take user input for chatbot’s prompt. If the prompt is available in knowledge
base, print the respective response. If not, ask the user that how should the bot respond. Update
the knowledge base with user given response.
Here’s an illustration of this:

1 👋 Hello, I'm Atom. How are you?


2
3 🧑 Human: Who created Python?
4 🤖 Bot: Oh no, I don't know how to respond to that, please help me learn.
5 ❓ ===> Guido van Rossum
6
7 🤖 Bot: Got it, I will remember this from now.
8 🧑 Human: who created PYTHON?
9 🤖 Bot: Guido van Rossum

Make sure to ensure that prompts are dealt as case insensitive e.g. “How are you?” and “HOW
ARE YOU?” both should be treated the same way.

3
Loops – Problem Set

• Use a nested while loop to solve this problem.


Write a program to print number triangle as shown in the following example.

1 Enter the height of triangle (number of rows): 4


2 1
3 1 2
4 1 2 3
5 1 2 3 4

• Write a program to print a rectangle of given width and height using nested while loop.

1 Enter width of rectangle: 5


2 Enter height of rectangle: 3
3 * * * * *
4 * * * * *
5 * * * * *

• Use a nested for loop to solve this problem.


Write a program to print the patch work of stars as shown.

1 Enter rows: 5
2 * * * *
3 * * *
4 * * * *
5 * * *
6 * * * *

Note that the number of columns are seven in this case that is, there are seven characters in each
row (including spaces)
HINT: you can use a boolean variable to alternate between a star and a blank space.

You might also like