Assignment # 03: National University of Computer and Emerging Sciences
Assignment # 03: National University of Computer and Emerging Sciences
Assignment # 03: National University of Computer and Emerging Sciences
Assignment # 03
Programming Fundamentals
Fall 2021
Deadline: 09:30 PM
10 November 2021 (Wednesday)
Write a C++ program for the following problems. Follow efficient approach to solve each
problem and your code should be readable and/or commented (e.g. add comments for
readability). Printed/hand written assignments will get you ZERO marks.
Problem Statements
1. Write a program which prints the given triangle pattern using only single loop and
continue/break statement. Triangle size should be defined by the user. Sample Triangle of
size 6 is:
*
* *
* * *
* * * *
* * * * *
* * * * * *
2. Write a program which prints the given triangle pattern using only single loop and
continue/break statement. Triangle size should be defined by the user. Sample Triangle of
size 6 is:
* * * * * *
* * * * *
* * * *
* * *
* *
*
3. Write a program that prompts the user to input height and width and print the hollow rectangle
as given in the output below. The program should check whether the both height and width
of rectangle are equal or not. Do not use nested loops.
4. Write a program to print the given pattern. Do not use nested loops.
x
x+
x+x
x+x+
x+x+x
x+x+x+
x+x+x+x
Page 2 of 5
National University
of Computer & Emerging Sciences-Faisalabad
5. Jason typically uses the Internet to buy various items. If the total cost of the items ordered, at
one time, is $200 or more, then the shipping and handling is free; otherwise, the shipping and
handling is $10 per item. Write a program that prompts Jason to enter the number of items
ordered and the price of each item. The program then outputs the total billing amount. You
have to use a loop (repetition structure) to get the price of each item. (For simplicity, you may
assume that Jason orders no more than nine (9) items at a time.)
6. The population of town A is less than the population of town B. However, the population of
town A is growing faster than the population of town B. Write a program that prompts the
user to enter the population and growth rate of each town. The program outputs after how
many years the population of town A will be greater than or equal to the population of town
B and the populations of both the towns at that time. (A sample input is: Population of town
A = 5,000, growth rate of town A = 4%, population of town B = 8,000, and growth rate of
town B=2%.).
7. Write a program that uses while loops to perform the following steps: a. Prompt the user to
input two integers:
a. firstNum and secondNum (firstNum must be less than secondNum).
b. Output all odd numbers between firstNum and secondNum.
c. Output the sum of all even numbers between firstNum and secondNum.
d. Output the numbers and their squares between 1 and 10.
e. Output the sum of the square of the odd numbers between firstNum and secondNum.
f. Output all uppercase letters.
8. Redo Programming question 7 using for loops.
9. Redo Programming question 7 using do...while loops
10.Perfect Number: an integer is said to be a perfect number if the sum of its divisors, including
1 (but not the number itself), is equal to the number.
For example, 6 is a perfect number, because 6 = 1 + 2 + 3. Write a program that prompt the
user to enter a number and determines whether the entered number is a perfect number. Your
program should determine and print all the perfect numbers between 1 and 1000. Print the
divisors of each perfect number to confirm that the number is indeed perfect.
11.Vending Machine design was an open-ended and vague problem. At first thought, many
different images flashed my mind and the problem looked intimidating. I decided to drill
down the requirements by asking the Interviewer as many questions as I could. After all the
clarification, the expectation was to build software they could store items (track the
inventory), accept cash, & dispense items. Additionally, the interviewer wanted the design
and code to be extensible, reusable & modular. Use (Switch case) command.
HINT: you have to set some drinks and set them some valid values that should not exceed
more then 1$ (Avoid Negative values)
Requirements:
Following are the requirements for the above discussed problem:
a. Vending Machine must keep track of the inventory
b. A person should be able to insert cash into the machine & choose an item
c. The Machine should confirm the inserted cash with the price of the selected item
d. The machine must display an error in case of insufficient cash or unavailable item
e. Finally, if all the above steps succeed then the user gets the selected item
Page 3 of 5
National University
of Computer & Emerging Sciences-Faisalabad
12.Write a program that inputs a grade from the user and print an equivalent description as
follows:
Grade Description
A Excellent
B Good
C Average
D Poor
F Fail
If the user enters an invalid input (like wrong character, or an integer value etc.), display
an error and exit the program. The program must be done with all the following three
methods,
1. If-else statements
2. Switch statement
3. Ternary operator
13.Write a C++ program to find out the factorial of any given number. In mathematics, the
factorial of a positive integer n, denoted by n!, is the product of all positive integers less
than or equal to n.
Example: 5! =5*4*3*2*1=120. The value of 0! is 1, according to the convention for an
empty product. NOTE: Factorial is calculated only for non-negative integers.
14. Suppose that the first number of a sequence is x, in which x is an integer. Define a0 = x;
an+1 = an/2 if an is even; an+1 = 3 * an + 1 if an is odd. Then, there exists an integer k
such that ak = 1.
Write a program that prompts the user to input the value of x. The program output the
integer k such that ak = 1 and the numbers a0, a1, a2, . . . , ak.
(For example, if x = 75, then k = 14, and the numbers a0, a1, a2, . . ., a14, respectively,
are 75, 226, 113, 340, 170, 85, 256, 128, 64, 32, 16, 8, 4, 2, 1.)
Test your program for the following values of x: 75, 111, 678, 732, 873 2048, and 65535.
Bonus: Handle all the possible exceptions in your codes.
😉 Best of luck
Page 4 of 5
National University
of Computer & Emerging Sciences-Faisalabad
Guidelines
A single violation of guideline will lead to Zero mark in your assignment.
Make a word file and put all your codes with proper screenshots in it.
The Questions should be in order, otherwise -5.
Prepare a word document which contains: each question’s source code
(copy/paste your written code) and screen shot of its output.
Word file naming format should be ‘Rollno_Section_Assignment # 03’ and
submit this document on Slate/Google Classroom.
You can submit your assignment late, within three days after your due date
with 30% marks deduction per day.
You will have maximum marks if you have done the entire task.
Deadlines should be kept in mind no extension in assignment dates.
This is an individual assignment. PLAGARISM IS NOT ACCEPTABLE!
Follow the instructions as it is, otherwise your assignment would not
be accepted at all.
Page 5 of 5