0% found this document useful (0 votes)
9 views7 pages

PF Week 3 EX

The document contains a series of programming exercises focused on decision control structures in C++. It includes solutions for checking if a number is positive, negative, or zero; finding the largest of three numbers; calculating grades; performing basic arithmetic operations; and determining leap years, among others. Each exercise provides a question and a corresponding C++ code solution.

Uploaded by

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

PF Week 3 EX

The document contains a series of programming exercises focused on decision control structures in C++. It includes solutions for checking if a number is positive, negative, or zero; finding the largest of three numbers; calculating grades; performing basic arithmetic operations; and determining leap years, among others. Each exercise provides a question and a corresponding C++ code solution.

Uploaded by

adnanqaisar46
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

WEEK 3 EXERCISE

Exercise 1: Check if a Number is Positive, Negative, or Zero

Question: Write a program that takes a number as input and checks if it’s positive, negative, or zero.
Display a message for each case.

Solution:

#include <iostream>

using namespace std;

int main() {

int number;

cout << "Enter a number: ";

cin >> number;

if (number > 0) {

cout << "The number is positive." << endl;

} else if (number < 0) {

cout << "The number is negative." << endl;

} else {

cout << "The number is zero." << endl;

return 0;

Exercise 2: Find the Largest of Three Numbers

Question: Write a program that takes three numbers as input and finds the largest of the three using if-
else statements.

Solution:
#include <iostream>

using namespace std;

int main() {

int a, b, c;

cout << "Enter three numbers: ";

cin >> a >> b >> c;

if (a >= b && a >= c) {

cout << "The largest number is: " << a << endl;

} else if (b >= a && b >= c) {

cout << "The largest number is: " << b << endl;

} else {

cout << "The largest number is: " << c << endl;

return 0;

Exercise 3: Grade Calculator

Question: Write a program that asks for a student's score and assigns a grade based on the following
criteria:

 90 and above: Grade A

 80 to 89: Grade B

 70 to 79: Grade C

 60 to 69: Grade D

 Below 60: Grade F

Solution:

#include <iostream>

using namespace std;

int main() {

int score;
cout << "Enter your score: ";

cin >> score;

if (score >= 90) {

cout << "Grade: A" << endl;

} else if (score >= 80) {

cout << "Grade: B" << endl;

} else if (score >= 70) {

cout << "Grade: C" << endl;

} else if (score >= 60) {

cout << "Grade: D" << endl;

} else {

cout << "Grade: F" << endl;

return 0;

Exercise 4: Menu-based Calculator

Question: Write a program that takes two numbers and performs addition, subtraction, multiplication,
or division based on user choice using a switch statement.

Solution:

#include <iostream>

using namespace std;

int main() {

int choice;

float num1, num2;

cout << "Enter two numbers: ";

cin >> num1 >> num2;

cout << "Choose an operation:\n";

cout << "1. Addition\n";


cout << "2. Subtraction\n";

cout << "3. Multiplication\n";

cout << "4. Division\n";

cin >> choice;

switch (choice) {

case 1:

cout << "Result: " << num1 + num2 << endl;

break;

case 2:

cout << "Result: " << num1 - num2 << endl;

break;

case 3:

cout << "Result: " << num1 * num2 << endl;

break;

case 4:

if (num2 != 0) {

cout << "Result: " << num1 / num2 << endl;

} else {

cout << "Error: Division by zero is not allowed." << endl;

break;

default:

cout << "Invalid choice!" << endl;

return 0;

Exercise 5: Check if a Year is a Leap Year


Question: Write a program that takes a year as input and checks if it is a leap year. A year is a leap year
if it is divisible by 4, but not divisible by 100, unless it’s also divisible by 400.

Solution:

#include <iostream>

using namespace std;

int main() {

int year;

cout << "Enter a year: ";

cin >> year;

if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {

cout << year << " is a leap year." << endl;

} else {

cout << year << " is not a leap year." << endl;

return 0;

}
programs questions for Week 3 concepts, focusing on decision control structures:

1. Positive or Negative: Write a program that checks if a given number is positive, negative, or zero.

2. Even or Odd: Create a program that takes a number as input and determines if it is even or odd.

3. Largest of Three Numbers: Write a program that accepts three numbers and displays the largest
among them.

4. Grade Calculator: Create a program that assigns a grade (A, B, C, D, or F) based on a student's score.

5. Leap Year Checker: Write a program that checks if a given year is a leap year.

6. Voter Eligibility: Develop a program that checks if a person is eligible to vote based on their age (must
be 18 or older).

7. Simple Calculator: Write a program that performs addition, subtraction, multiplication, or division
based on the user's choice.

8. Day of the Week: Create a program that takes a number (1-7) and displays the corresponding day of
the week.

9. Character Checker: Write a program that checks if a character is a vowel or a consonant.

10. Number Comparison: Develop a program that accepts two numbers and displays whether they are
equal, or which one is greater.

11. Discount Calculator: Write a program that calculates a discount based on the amount spent. Apply a
10% discount if the amount is over 1000.
12. Pass or Fail: Create a program that checks if a student has passed or failed based on their score
(passing score is 50).

13. Temperature Converter: Write a program that converts a temperature from Celsius to Fahrenheit or
Fahrenheit to Celsius based on the user's choice.

14. Electricity Bill Calculator: Develop a program that calculates the total bill based on units consumed.
Charge different rates per unit based on consumption.

15. Grade Point Average (GPA): Create a program that assigns a GPA based on a student’s marks using
an `if-else if` ladder.

16. Traffic Light Simulator: Write a program that displays a message (Stop, Ready, Go) based on a traffic
light color (Red, Yellow, Green).

17. Triangle Type Checker: Develop a program that accepts the lengths of three sides and determines if
it forms an equilateral, isosceles, or scalene triangle.

18. Currency Converter: Write a program that converts a given amount from one currency to another
based on user input.

19. Number Range Checker: Create a program that checks if a number is within a specified range (for
example, between 10 and 50).

20. Password Validator: Develop a program that checks if a user-entered password matches a
predefined password and displays an appropriate message.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy