CoE 113 Programming and Problem Solving Exercises PDF

Document Details

LucidCarbon2914

Uploaded by LucidCarbon2914

College of Engineering - Computer

2024

Dr.Dhayaa Khudher

Tags

C++ programming programming exercises problem solving computer science

Summary

This document contains a set of exercises for a C++ programming course, likely for a first-semester undergraduate level. It includes various programming tasks from simple calculations to more complex tasks such as creating a calculator program and determining whether numbers are even, odd, factors of each other and calculations.

Full Transcript

12/15/2024 CoE 113 Programming and Problem Solving [Exercises] [First semester] Dr.Dhayaa Khudher COLLEGE OF ENGINEERING - COMPUTER 1 CoE 113 Programming and Problem Solving [Exercises] Q1: Write a C++ code to...

12/15/2024 CoE 113 Programming and Problem Solving [Exercises] [First semester] Dr.Dhayaa Khudher COLLEGE OF ENGINEERING - COMPUTER 1 CoE 113 Programming and Problem Solving [Exercises] Q1: Write a C++ code to swap two integer numbers and find the sum of them (use temp variable). Q2: Write a C++ code to swap two integer numbers (use Bitwise exclusive OR operator). Q3: Write a C++ program to read five numbers, find their sum, and print the numbers in reverse order. Q4: Write a C++ program to read six number, find the sum and average of these numbers. Q5: Write a C++ program to print the sequence of numbers (10, 11, 12 … 30). Use while loop statement. Q6: Write a C++ program to print the sequence of odd numbers from (11 - 29). Use while loop statement. Q7: Write a C++ program to print the sequence of even numbers from (10 - 30). Use while loop statement. Q8: Write a C++ program to print the sequence of numbers (15, 16, 17 … 30). Use do while loop statement. Q9: Write a C++ program to print the sequence of odd numbers from (11 - 29). Use do while loop statement. Q10: Write a C++ program to print the sequence of even numbers from (10 - 30). Use do while loop statement. Q11: Write a C++ program that takes two integers as input and checks if they are equal, greater than, or less than each other. Q12: Create a C++ program that determines whether a given year is a leap year or not. Provide appropriate messages based on the result. Q13: Implement a C++ program that takes a student's percentage as input and assigns a grade according to the following criteria: 90% and above: A 80-89%: B 70-79%: C Below 70%: F Q14: Write a C++ program to determine if a given number is positive, negative, or zero. Use appropriate conditional statements. 2 CoE 113 Programming and Problem Solving [Exercises] Q15: Develop a simple C++ calculator program with a menu that performs addition, subtraction, multiplication, and division based on the user's choice. Q16: Create a C++ program that takes a character as input and determines whether it is a vowel, consonant. Q17: Write a C++ program that classifies a person into different age groups: child (0-12), teenager (13-19), adult (20-59), or senior (60 and above). Q18: Implement a C++ program that converts temperatures between Celsius and Fahrenheit. Ask the user for the temperature and the unit, then perform the conversion based on the input. Hint: (Temperature in degrees Fahrenheit (°F) = (Temperature in degrees Celsius (°C) * 9/5) + 32). Q19: Create a C++ program that takes two integers as input and checks whether the first number is divisible by the second one. Provide appropriate messages based on the result. Q20: Write a C++ program that takes three numbers as input and determines the largest among them using conditional statements. Q21: Create a C++ program that takes three angles as input and classifies the triangle based on the angle values (acute, obtuse, or right-angled). Q22: Write a C++ program that calculates the total cost after applying a discount based on the purchase amount. For example, provide a 10% discount if the purchase amount is above $100. Q23: C++ code to calculate the factorial of any entered number. Q24: C++ code to calculate the power of any entered number (the base and exponent should be entered by user). Q25: Write a program that calculates and prints the sum of the even integers from 2 to user defined limit. Use a do-while loop that asks the user whether the program should be terminated or not. Q26: Write a C++ program to print the sequence of numbers (101, 111, 112 … 130), except number 107 and 125. 27: Write a program that calculates and prints the product of odd integers from 1 to user defined limit. Use a do-while loop that asks the user whether the program should be terminated or not. Q28: Write a program which takes PIN number of the user as input and then verifies his pin. If pin is verified the program shall display “pin verified” and “Welcome”; otherwise, the program shall 3 CoE 113 Programming and Problem Solving [Exercises] give user another chance. After 4 wrong attempts, the program shall display “Limit expired” and then exit. Use for loops to implement the logic. Note: 5 random PIN numbers can be assumed and fed into the program with which input pin is matched. Q29: Write a C++ program to calculate the summation of the following sequence for nth terms. 2 3 𝑆𝑢𝑚 = 1 + + +............ 4 9 Q30: C++ code to find the summation of the following series. Sum = 𝑎 + 𝑎 + 𝑎 + ⋯ + 𝑎 Q31: C++ code to find the summation of the following series for nth terms. 1 2 3 𝑆𝑢𝑚 = + + +............ 𝑎 𝑎 𝑎 Q32: C++ code to find the summation of the following series. Sum = 1 + 2! + 3! + ⋯ + n! Q33: Grades in the programming lab are classified according to letters A case of Excellent, B case of Well done, C case of done, D case of Pass and F case of Try again. Write C++ code to implement this logic using Switch statement. Q34: Create a C++ program that takes five integers as input and determines whether their product is positive or negative. Q35: Write a C++ program that takes a day number (1-7) as input and prints the corresponding day of the week. Q36: Write a C++ program that determines if a person is eligible to vote based on their age. Consider the legal voting age as 18. Q37: Write C++ program takes an input of a person first name and uses a switch statement to output the corresponding person's age (consider three persons). Q38: Implement a C++ program that converts an amount in one currency to another. Ask the user for the input currency, amount, and desired currency. Q39: Write C++ Program which computes area of circle. The program takes radius of the circle as an input, calculates the area of the circle and outputs it on the screen. (Area =𝜋𝑟 ). Q40: Write C++ code to computes area of a shape using switch case. The program takes shape as an input and identifies the shape using a switch case. The statements then take required values as input, computes the area and prints it. (assume two shapes only, square and rectangle) 4 CoE 113 Programming and Problem Solving [Exercises] Q41: Implement a C++ code to decide the car type depending on the number of seats. Print an appropriate massage to indicate a car type. (Saloon has four seats, SUV has seven seats and Bus has more than seven seats). Q42: Consider two integer numbers A=7 and B=14. Write C++ program to implement the following assignment operator. 1- Left shift operator. (shift by 2). 2- Modulus operator. Q43: Consider two integer numbers C=8 and D=4. Write C++ program to implement the following assignment operator. 1- Right shift operator. (shift by 1) 2- Bitwise AND assignment operator. Q44: Consider two integer numbers N=11 and M=9. Write C++ program to implement the following assignment operator. 1- Divide AND assignment operator. 2- Bitwise inclusive OR and assignment operator. Q45: Consider two integer numbers X=1 and Y=0. C++ program to implement the following logical operator. 1- Logical AND operator. 2- Logical NOT Operator. Q46: Write C++ code to determine the size of various data type on a computer. (use character and Boolean only). Q47: Write C++ code to determine the size of various data type on a computer. (use Floating point and integer only). Q48: Write C++ code to determine the size of various data type on a computer. (use character and double only). Q49: Consider two integer numbers U=15 and V=8. C++ program to implement the following operator description. 1- Checks if the values of two operands are equal or not. 2- Checks if the value of left operand is greater than or equal to the value of right operand. Q50: Write a C++ program to print the sequence of numbers (95-105), except number 101. The code should has continue statement. Exe51: Write a function which, given an array of integers, returns the integer that appears most frequently in the array. E.g. for the array [ 1 2 3 2 3 4 2 5 ] your function should return 2. 5 CoE 113 Programming and Problem Solving [Exercises] Exe52: Write the function sum() with four parameters that calculates the arguments provided and returns their sum. Parameters: Four variables of type long. Returns: The sum of type long. Use the default argument 0 to declare the last two parameter of the function sum().Test the function sum() by calling it by all three possible methods. Use random integers as arguments. Exe53: The factorial n! of a positive integer n is defined as n! = 1*2*3... * (n-1) * n Where 0! = 1 Write a function to calculate the factorial of a number. Argument: A number n of type unsigned int. Returns: The factorial n! of type long double. Formulate two versions of the function, where the factorial is a. calculated using a loop b. calculated recursively Test both functions by outputting the factorials of the numbers 0 to 20 as shown opposite on screen. Exe54: Write a function pow(double base, int exp) to calculate integral powers of floating-point numbers. Arguments: The base of type double and the exponent of type int. Returns: The power baseexp of type double. For example, calling pow(2.5, 3) returns the value 2.53 = 2.5 * 2.5 * 2.5 = 15.625 This definition of the function pow()means overloading the standard function pow(), which is called with two double values. Test your function by reading one value each for the base and the exponent from the keyboard. Compare the result of your function with the result of the standard function. Exe55: Write a program to create function with Return type and Argument. Exe56: Write C++ program to implement a recursive function. 6 CoE 113 Programming and Problem Solving [Exercises] Exe57: Write a program to create function with Return type and no Argument. Exe58: Write a program to create function with no Return type and no Argument. Exe59: Write C++ program to find the transpose of (n x m) array. Note: The transpose of a matrix is found by interchanging its rows into columns or columns into rows. Exe60: Write a C++ program to find the transpose of the following matrix. 3 4 6 1 𝑚𝑎𝑡𝑟𝑖𝑥 = 4 8 0 9 5 7 1 2 Exe61: Write a C++ program to find the greatest number in a given 4x4 two-dimensional matrix. Your program should include the declaration and initialization of the matrix, and then it should find and display the greatest number within the matrix. Exe62: You are given two 3x3 matrices, matrixA and matrixB, both initialized with random integer values. Write a C++ program to swap the contents of these two matrices. Your program should include the declaration and initialization of the matrices, and then perform the swapping operation. Exe63: List the types of user-defined functions in C++, and support your answer with an appropriate example. Exe64: An array (one-dimension) has 16 elements, write C++ program to fine the sum of all its elements. The program should include the part of declaration and initialization of the array. Exe65: Create an (m x n) array using C++ language that contains odd numbers only. The code should contain a sentence to ask the user to enter the value of m and n. Exe66: Write C++ code to find the sum of the elements in the Lower triangular of a (n x n) matrix. Exe67: Write C++ code to find the sum of the elements in the Upper triangular of a (n x n) matrix. Exe68: Write C++ code to find the sum of the elements in the diagonal of a (n x n) matrix. Exe69: Write C++ code to find the summation of the elements in the second column of (4x4) matrix. Exe70: Write a function that displays a message indicating the number of times it gets called: For instance, “The function has been called three times”. Write a program that calls this 7 CoE 113 Programming and Problem Solving [Exercises] function 10 times from main (). You will need to use a global variable to store the count. A global variable is declared outside the main () function. Note: All variables declared inside a function are local variables. Their value is lost once the function ends. Exe71: Write C++ code to find the summation of the elements in the second and fourth column of (4x4) matrix. Exe72: Write C++ code to check whether a matrix is sparse or not. Define a variable to count the zero and the non-zero values. Definition of sparse Matrix: Sparse matrices are those matrices that have the majority of their elements equal to zero. Exe73: Write a C++ program to calculate an estimation of Sin(x) using Taylor series approximation according the following formula: (−1) sin(𝑥) = 𝑥 (2𝑖 + 1)! Exe74: Write C++ code to create (3x4) array. The elements of the array are the factorial of integer numbers starting from 4. Exe75: Write C++ code to create (2x3) array. The elements of the array are the power of a number the exponent of the first number should start from 2 and increased by one for the next numbers. 8 CoE 113 Programming and Problem Solving [Exercises] Typical solution to some questions: Exe60: Write a C++ program to find the transpose of the following matrix. 3 4 6 1 𝑚𝑎𝑡𝑟𝑖𝑥 = 4 8 0 9 5 7 1 2 Solution: #include using namespace std; int main() { int A={{3,4,6,1},{4,8,0,9},{5,7,1,2}}; int B; for (int i=0; i

Use Quizgecko on...
Browser
Browser