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

Assignment #01 (3) 215151115151

This document outlines Assignment #01 for Computer Programming-II, focusing on control structures in C++. It consists of three tasks: factorization and divisor analysis, ATM cash denomination problem, and mixed fraction and time conversions, each worth 10 marks. Students are required to write C++ programs that utilize loops and conditional logic while adhering to specific guidelines and constraints.

Uploaded by

xavier16dude
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)
17 views4 pages

Assignment #01 (3) 215151115151

This document outlines Assignment #01 for Computer Programming-II, focusing on control structures in C++. It consists of three tasks: factorization and divisor analysis, ATM cash denomination problem, and mixed fraction and time conversions, each worth 10 marks. Students are required to write C++ programs that utilize loops and conditional logic while adhering to specific guidelines and constraints.

Uploaded by

xavier16dude
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
You are on page 1/ 4

Comp-1431 Assignment # 01 Computer Programming-II

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.

Instructions: Please see the attached assignment submission template.

Tasks

Question 1: Factorization & Divisor Analysis [10 Marks]

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."

-----------------------------

Question 2: Atm Cash Denomination Problem [10 Marks]


Write a C++ program simulating an ATM that dispenses cash in the least number of bills possible.
The program will:

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

● 35 / 20 = 1 → one $20, remaining 15


● 15 / 10 = 1 → one $10, remaining 5
● 5 / 5 = 1 → one $5, remaining 0
● Output: 2 x $100, 1 x $20, 1 x $10, 1 x $5

-----------------------------

Question 3: Mixed Fraction And Time Conversions [10 Marks]


Write a C++ program that provides two different functionalities, selected by a menu. Use loops
and conditionals as needed. No arrays.

A. Mixed Fraction Conversion


a. Prompt the user for two integers: numerator (N) and denominator (D).
b. Check that D != 0. If D = 0, prompt again or show an error.
c. Convert the fraction N/D to mixed fraction form:
d. Whole part = N / D
e. Remainder part = N % D
f. Display the result in the format: "N/D = WholePart Remainder/Denominator" For
example, if N=14 and D=4, output: "14/4 = 3 2/4".

B. Time Difference Calculator (using hours and minutes in 24-hour format)


a. Prompt the user two times (each given in hours and minutes). For instance, "Start:
9 45" and "End: 13 10".
b. Convert each time into total minutes from midnight:
c. total_minutes_start = start_hour * 60 + start_minute
d. total_minutes_end = end_hour * 60 + end_minute
e. Calculate the difference in minutes. If the second time is earlier than the first, handle
it appropriately (either assume the next day or show an error – your choice).
f. Convert that difference back to hours and minutes:
g. hours_diff = difference_in_minutes / 60
h. minutes_diff = difference_in_minutes % 60
i. Display the time difference in the format "HH hours and MM minutes".
Key Points:
● Use integer division / and modulus % in multiple places.
● Incorporate loops or repeated prompts if inputs are invalid.
● No arrays allowed.
● Use if-else for menu selection (e.g., "Press 1 for Mixed Fraction Conversion, 2 for Time
Difference Calculator, -1 to Exit").

---------------------------------------------
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.).

You might also like