0% found this document useful (0 votes)
38 views

Programming Fundamentals Labs Spring 2024

Programming Concepts With C++.PDF

Uploaded by

mariammosayed5
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Programming Fundamentals Labs Spring 2024

Programming Concepts With C++.PDF

Uploaded by

mariammosayed5
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Programming Fundamentals

LABS

2024
For each problem below, write a C/C++ program for solving it.

Arithmetic operations

1. The local supermarket needs a program that can compute the value of a
bag of coins. The bag has pennies, nickels, dimes, and quarters in the
bag; the program produces the amount of money in the bag.
2. Calculating the volume of material in a pipe. Use the following equation:
V =( π r 2out −π r 2¿ ) ×l

3. An old-style movie theatre has a simple profit function. Each customer


pays 5 pounds per ticket. Every performance cost the theatre 20 pounds,
plus 0.50 pound per attendee. You have to calculate how much income
the attendees produce.
4. Print the output of the following programs, assume any missing input
data:
A)

#include <iostream>

int main() {
int num;
cout << "Enter a number: ";
cin >> num;
cout << "You entered: " << num << endl;
return 0;
}

B)

#include <iostream>

int main() {
int num;
std::cout << "Enter a number: ";
std::cin >> num

Page 1 of 20
std::cout << "You entered: " << num << std::endl;
return 0;
}

C)

int main() {
int num;
std::cout << "Enter a number: ";
std::cin >> num;
std::cout << "You entered: " << num << std::endl;
return 0;
}

D)

#include <iostream>

int main() {
int num;
std::cout << "Enter a number: ";
std::cin >> num;
std::cout << "You entered: " << num << std::endl;
return 0;
}

E)

#include <iostream>

int main() {
int num1, num2, sum;
std::cout < "Enter two numbers: ";
std::cin > num1 > num2;
sum = num1 + num2;
std::cout < "The sum is: " < sum < std::endl;
return 0;
}

F)

#include <iostream>

Page 2 of 20
int main() {
int num1, num2, sum;
std::cout << "Enter two numbers: ";
std::cin >> num1 >> num2;
sum = num1 + num2;
std::cout << "The sum is: " << sum << std::endl;
return 0;
}

G)

#include <iostream.h>
using namespace std;
main()
{
int n = 4, k = 2;
cout << ++n << endl;
cout << n << endl;
cout << n++ << endl;
cout << n << endl;
cout << -n << endl;
cout << n << endl;
cout << --n << endl;
cout << n << endl;
cout << n-- << endl;
cout << n << endl;
cout << n + k << endl;
cout << n << endl;
cout << k << endl;
cout << n << k << endl;
cout << n << endl;
cout << " " << n << endl;
cout << " n" << endl;
cout << "\n" << endl;
cout << " n * n = "; //CAREFUL!
cout << n * n << endl;
cout << 'n' << endl;

int found = 0, count = 5;


if (!found || --count == 0)
cout << "d" << endl;
cout << "count = " << count << endl;
return 0;
}

H)

Page 3 of 20
#include <iostream.h>
using namespace std;

main()
{
int n;
cout << (n = 4) << endl;
cout << (n == 4) << endl;
cout << (n > 3) << endl;
cout << (n < 4) << endl;
cout << (n = 0) << endl;
cout << (n == 0) << endl;
cout << (n > 0) << endl;
cout << (n && 4) << endl;
cout << (n || 4) << endl;
cout << (!n) << endl;

int i = 5, j = 6, k = 7, nn = 3;
cout << i + j * k - k % nn << endl;
cout << i / nn << endl;
return 0;
}

Conditional operations

5. Given a street number of a one-way street, decide the direction of the


street, either eastbound or westbound. Even numbered streets are
Eastbound, odd numbered streets are Westbound.
6. Solving quadratic equation:
a x +bx +c=0 .
2

7. Finding the largest of three numbers.


8. Putting three numbers in increasing order.
9. A weather forecasting program writes the text shown in the rows and
columns of the following table, depending on values of temperature and
pressure supplied by the user. Define high temperatures to be above
20.0°C and high pressures to be above 1000 millibar.

Page 4 of 20
High pressure Low pressure

High Clear skies and hot Warm with rain


temperature

Low temperature Clear skies and cold Snow

10.Reading a student’s grade and printing an equivalent letter according to


the following table:

85 – 100 A

75 –84 B

65 – 74 C

55 – 64 D

0 – 54 F

11.Reading two numbers and an operation: +, -, *, or / and printing the


result.
12.Write a program that prompts the user to input a number. The program
should then output the number and a message saying whether the
number is positive, negative, or zero.
13.In a right triangle, the square of the length of one side is equal to the
sum of the squares of the lengths of the other two sides. Write a
program that prompts the user to enter the lengths of three sides of a
triangle and then outputs a message indicating whether the triangle is a
right triangle.

Page 5 of 20
14.A box of cookies can hold 24 cookies, and a container can hold 75 boxes
of cookies. Write a program that prompts the user to enter the total
number of cookies, the number of cookies in a box, and the number of
cookie boxes in a container. The program then outputs the number of
boxes and the number of containers to ship the cookies. Note that each
box must contain the specified number of cookies, and each container
must contain the specified number of boxes. If the last box of cookies
contains less than the number of specified cookies, you can discard it
and output the number of leftover cookies. Similarly, if the last container
contains less than the number of specified boxes, you can discard it and
output the number of leftover boxes.
15.Print the output of the following programs, assume any missing input
data:
A)

#include <iostream>

int main() {
int num;
std::cout << "Enter a number: ";
std::cin >> num;
if (num % 2 = 0) {
std::cout << num << " is even." << std::endl;
} else {
std::cout << num << " is odd." << std::endl;
}
return 0;
}

B)

#include <iostream>

int main() {
int num;
std::cout << "Enter a number: ";
std::cin >> num;
if num % 2 == 0 {

Page 6 of 20
std::cout << num << " is even." << std::endl;
} else {
std::cout << num << " is odd." << std::endl;
}
return 0;
}

C)

#include <iostream>

int main() {
int num;
std::cout << "Enter a number: ";
std::cin >> num;
if (num % 2 == 0) {
std::cout << num << " is even." << std::endl;
} else {
std::cout << num << " is odd." << std::endl;
}

int n, k = 5;
n = (100 % k ? k + 1 : k - 1);
std::cout << "n = " << n << " k = " << k <<std::endl;
return 0;
}

Looping

16.Repeat the previous problem but make the program ask the user if
he/she wants to make another calculation, if he/she enters ‘y’ the
program read another two numbers and an operation, else the program
ends.
17.Consuming a natural number n and a symbol and producing a list with n
occurrences of the symbol.
18.Computing the sum of all integers from 1 to any given integer.
19.Reading two integers and printing all odd integers between them.

Page 7 of 20
20.Reading two integers x and y, then computing the sum of squares of
integers from x to y.
21.Finding the maximum/minimum of a set of numbers.
22.Computing x y for any given two integers x and y.
23.Computing the factorial of any given integer.
24.Printing the Fahrenheit-Celsius table for F = 0, 20, 30... 300. Use the
following equation:

℃=(5 /9)∗(℉ −32)

25.Calculating the average of students’ grades for a class.


26.Printing a square of stars (asterisks), given the size of its side.
27.Printing the following patterns:

* * *

** *** **

*** ***** ***

**** ******* **

***** ********* *

28.Printing the binary numbers from (0000)2 to (1111)2


29.Printing the Hex. numbers from (0000)h to (FFFF)h
30.Converting Hex. numbers to binary numbers.
31.Calculating:

2 3 4
x x x x
e =1+ x + + + …
2! 3 ! 4 !

32.Calculating:

Page 8 of 20
3 5 7
x x x
sin ( x )=x− + − …
3! 5! 7!

33.Computing the sum of the digits of any given integer.


34.Converting decimal numbers to binary numbers.
35.Converting binary numbers to decimal numbers.
36.Write a program that prompts the user to input an integer and then
outputs both the individual digits of the number and the sum of the
digits. For example, it should output the individual digits of 3456 as 3 4 5
6, output the individual digits of 8030 as 8 0 3 0, output the individual
digits of 2345526 as 2 3 4 5 5 2 6, output the individual digits of 4000 as
4 0 0 0, and output the individual digits of -2345 as 2 3 4 5.
37.To make telephone numbers easier to remember, some companies use
letters to show their telephone number. For example, using letters, the
telephone number 438-5626 can be shown as GET LOAN. In some cases,
to make a telephone number meaningful, companies might use more
than seven letters. For example, 225-5466 can be displayed as CALL
HOME, which uses eight letters. Write a program that prompts the user
to enter a telephone number expressed in letters and outputs the
corresponding telephone number in digits. If the user enters more than
seven letters, then process only the first seven letters. Also output the –
(hyphen) after the third digit. Allow the user to use both uppercase and
lowercase letters as well as spaces between words. Moreover, your
program should process as many telephone numbers as the user wants.
38.Rewrite the following code fragment so that it uses a "do...while..." loop
to accomplish the same task.

int n;
cout << "Enter a non-negative integer: ";
cin >> n;

Page 9 of 20
while (n < 0)
{
cout << "The integer you entered is negative." << endl;
cout << "Enter a non-negative integer: ";
cin >> n;
}

39.Print the output of the following programs, assume any missing input
data:
A)

#include <iostream>

int main() {
for (int i = 1, i <= 5, ++i) {
std::cout << "Number: " << i << std::endl;
}
return 0;
}

B)

#include <iostream>

int main() {
for (i = 1; i <= 5; ++i) {
std::cout << "Number: " << i << std::endl;
}
return 0;
}

C)

#include <iostream>

int main() {
for (int i = 1; i <= 5; ++i) {
std::cout << "Number: " << i << std::endl;
}
return 0;
}

D)

Page 10 of 20
#include <iostream.h>
using namespace std;
main()
{
int n = 3;
while (n >= 0);
{
cout << n * n << endl;
--n;
}
cout << n << endl;
while (n < 4)
cout << ++n << endl;
cout << n << endl;
while (n >= 0)
cout << (n /= 2) << endl;
return 0;
}

E)

#include <iostream.h>
using namespace std;
main()
{
int n = 3;
while (n >= 0)
{
cout << n * n << endl;
--n;
}
cout << n << endl;
while (n < 4)
cout << ++n << endl;
cout << n << endl;
while (n >= 0)
cout << (n /= 2) << endl;
return 0;
}

Functions:

40.Write a value-returning function, isVowel, that returns the value true if a


given character is a vowel and otherwise returns false.

Page 11 of 20
41.Write a program that prompts the user to input a sequence of
characters and outputs the number of vowels. (Use the function
isVowel)
42.Write a function, reverseDigit, that takes an integer as a parameter and
returns the number with its digits reversed. For example, the value of
reverseDigit(12345) is 54321; the value of reverseDigit(5600) is 65; the
value of reverseDigit(7008) is 8007; and the value of reverseDigit(-532) is
-235.
43.Write a program that prints the day number of the year, given the date
in the form month-day-year. For example, if the input is 1-1-2006, the
day number is 1; if the input is 12-25-2006, the day number is 359. The
program should check for a leap year. A year is a leap year if it is divisible
by 4, but not divisible by 100. For example, 1992 and 2008 are divisible
by 4, but not by 100. A year that is divisible by 100 is a leap year if it is
also divisible by 400. For example, 1600 and 2000 are divisible by 400.
However, 1800 is not a leap year because 1800 is not divisible by 400.
44.Write a program to convert the time from 24-hour notation to 12-hour
notation and vice versa. Your program must be menu driven, giving the
user the choice of converting the time between the two notations.
Furthermore, your program must contain at least the following function:
a function to convert the time from 24-hour notation to 12-hour
notation, a function to convert the time from 12-hour notation to 24-
hour notation, a function to display the choices, function(s) to get the
input, and function(s) to display the results. (For 12-hour time notation,
your program must display AM or PM.)
45.Write a function named "digit_name" that takes an integer argument in
the range from 1 to 9, inclusive, and prints the English name for that

Page 12 of 20
integer on the computer screen. No newline character should be sent to
the screen following the digit name. The function should not return a
value. The cursor should remain on the same line as the name that has
been printed. If the argument is not in the required range, then the
function should print "digit error" without the quotation marks but
followed by the newline character. Thus, for example,
the statement digit_name(7); should print seven on the screen;
the statement digit_name(0); should print digit error on the screen and
place the cursor at the beginning of the next line.

One dimension array:

46.Write a program that reads ten numbers, computes their average, and
finds out how many numbers are above the average.
47.write a program that let the user to enter 10 float number, then take a
number x and print "found” if this number was in those 10 float number,
else print "not found"

48.Write a program that reads in ten numbers and displays distinct


numbers (i.e., if a number appears multiple times, it is displayed only
once). Hint: Read a number and store it to an array if it is new. If the
number is already in the array, discard it. After the input, the array
contains the distinct numbers.

49.Write a program that generates one hundred random integers between


0 and 9 and displays the count for each number.

Page 13 of 20
50.suppose that after filling our 4-element array with values, we need to
copy that array to another array of 4 short's?

51.Write a program that reads ten integers and displays them in the reverse
of the order in which they were read.

52.Write a program to test if a string entered is a palindrome. **string**

53.Write a program that read ten integers, print them in increasing order.
"sorting"

Two-dimension array:

54.Write a program that sums all the integers in a matrix (3,3) of integers.
55.Write a program that sums all the integers in the major diagonal in a
matrix (5,5) of integers.
56.Write a program to add two matrices. To be added, the two matrices
must have the same dimensions and the same or compatible types of
elements. As shown below, two matrices are added by adding the two
elements of the arrays with the same index:

Page 14 of 20
57.Assume there are 7 students in the class study 6 subjects "A,B,C,D,E,F".
read their grades and print them in this way:

General

58.Implement a basic calculator:


 Take user input for two numbers and an operator (+, -, *, /).
 Perform the appropriate calculation based on the chosen operator.
 Handle potential errors like division by zero and invalid operators.
 Extend the functionality to include exponentiation, modulus, and basic
trigonometric functions.

59.Write a program to generate the Fibonacci sequence:


 Start with two initial values (e.g., 0 and 1).
 Generate subsequent terms by adding the previous two terms.

Page 15 of 20
 Print the first N terms of the sequence, where N is user input.

60.Calculate the square root of a number using Newton-Raphson method:


 Implement the iterative Newton-Raphson method to approximate the
square root of a given number.
 Define an initial guess and refine it in each iteration based on the
formula.
 Set a tolerance level for convergence and stop when the difference
between consecutive approximations is within the tolerance.

61.Write a program that prompts the user to input a string. The program
then uses the function substr to remove all the vowels from the string.
For example, if str = "There", then after removing all the vowels, str =
"Thr". After removing all the vowels, output the string. Your program
must contain a function to remove all the vowels and a function to
determine whether a character is a vowel.
62.Convert between different number bases (e.g., binary, decimal,
hexadecimal).
63.Solve a system of linear equations using Gaussian elimination.
64.Implementing a simple linear search algorithm.
65.Write a program that reads in a line consisting of a student’s name,
Social Security number, user ID, and password. The program outputs the
string in which all the digits of the Social Security number, and all the
characters in the password are replaced by x. (The Social Security
number is in the form 000-00-0000, and the user ID and the password do
not contain any spaces.) Your program should not use the operator [] to
access a string element.

Page 16 of 20
66.You are given a file consisting of students’ names in the following form:
lastName, firstName middleName. (Note that a student may not have a
middle name.) Write a program that converts each name to the
following form: firstName middleName lastName. Your program must
read each student’s entire name in a variable and must consist of a
function that takes as input a string, consists of a student’s name, and
returns the string consisting of the altered name. Use the string function
find to find the index of; the function length to find the length of the
string; and the function substr to extract the firstName, middleName,
and lastName.
67.Write a C/C++ program that reads a string and reprint it without any
spaces.

Sample run:

Enter a string: I am a first-year student.

Iamafirst-yearstudent.

68.Write a C/C++ program that prints the following pattern on the console,
the program should input the number of lines:

Sample run:

Enter number of lines: 5

***

*****

*******

Page 17 of 20
*********

69.Write a C/C++ program that computes the following formula:

2 3
f =1−x + x −x +…

Sample run:

Enter the value of x: 3

Enter number of terms: 5

F = 61

70.Write a C/C++ program that prints the whole range of binary numbers
for a specified number of digits.

Sample run:

Enter number of digits: 2

00

01

10

11

71.Write a C/C++ program that lets the user input 5 integers, then takes an
integer x, and decides whether it is found in those 5 integers or not.

Page 18 of 20
Sample run:

Enter 5 integers:

Enter some integer: 5

5 is found.

Page 19 of 20

You might also like