Assignment #01 (3) 215151115151
Assignment #01 (3) 215151115151
Assignment #01
Using Control Structures
(Selection and Repetition)
Total Task: 03
Marks: 10 x 3 = 30 marks
Objective:
The goal of this assignment is to give you an opportunity to practice code writing and making logic
using Control Structures i.e., Selection (if-else versions, switch) and Repetition (loops) concepts
to solve real-world problems.
Tasks
Write a C++ program that takes an integer n (greater than 1) from the user and performs the
following:
1. Check if n is prime or composite.
a. You must use the % operator in a loop to test divisibility.
2. If n is composite, find and display all of its factors (excluding 1 and itself). Then also
display:
a. The sum of these factors.
b. A conclusion on whether the number is "perfect" (sum of proper divisors equals
n) or "deficient"/"abundant" (if the sum is less than or greater than n, respectively).
3. If n is prime, simply state that it has no proper divisors beyond 1 and itself.
Key Points:
● Use at least one loop structure (for or while) to iterate through possible divisors.
● Incorporate conditional logic (if-else) to distinguish prime vs. composite.
● Extensively use the % operator to check divisibility.
● No arrays are allowed; you can only use basic variables to keep track of sums or counts.
Example:
● Input: 6
○ Factors (excluding 1 and 6) are 2 and 3.
Comp-1431 Assignment # 01 Computer Programming-II
○ Sum of factors = 5.
○ Since 5 < 6, output "6 is a deficient number."
● Input: 28
○ Factors (excluding 1 and 28) are 2, 4, 7, 14.
○ Sum = 27 + 1 (if you included 1 as well it would be a perfect number, but for this
assignment, check the exact instructions you provide). Adjust the logic as you see
fit.
● Input: 13
○ It's prime. Output "13 is prime, no proper divisors."
-----------------------------
1. Prompt the user to enter an amount of money to withdraw (in integer dollars).
2. Validate that the amount is a multiple of 5 (e.g., 5, 10, 15, …). If not, keep prompting until
a valid amount is entered.
3. Dispense the amount using the following denominations (example set; you may
customize):
○ $100
○ $50
○ $20
○ $10
○ $5
4. Display how many of each bill is dispensed.
5. Optionally, prompt the user if they want to perform another transaction.
Key Points:
● Use integer division (/) and modulus (%) extensively. For example:
○ Count how many $100 bills: count_100 = amount / 100; amount = amount % 100; ○
Then proceed with $50, $20, $10, $5 similarly.
● No arrays allowed. Maintain counts using simple int variables.
● Loop until the user decides to exit or enter a sentinel value.
Example:
Input: 235
● 235 / 100 = 2 → two $100 bills, remaining 35
● 35 / 50 = 0 → skip $50
Comp-1431 Assignment # 01 Computer Programming-II
-----------------------------
---------------------------------------------
Comp-1431 Assignment # 01 Computer Programming-II
ADDITIONAL REQUIREMENTS:
1. Use meaningful variable names.
2. Provide proper user prompts and error messages if the input is invalid.
3. Include comments in your code to explain your logic.
4. All solutions must avoid using arrays; and rely on basic variables.
5. Use at least one loop structure (for, while, do-while) in each question and incorporate
conditionals (if-else, switch if desired).
6. Thoroughly test your code with various inputs to ensure correctness, especially focusing
on edge cases (e.g., prime vs. composite edge cases, zero or negative amounts in ATM,
fraction denominator = 0, etc.).