Programming Fundamentals Labs Spring 2024
Programming Fundamentals Labs Spring 2024
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
#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;
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
Page 4 of 20
High pressure Low pressure
85 – 100 A
75 –84 B
65 – 74 C
55 – 64 D
0 – 54 F
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:
* * *
** *** **
**** ******* **
***** ********* *
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!
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:
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.
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"
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.
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
Page 15 of 20
Print the first N terms of the sequence, where N is user input.
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:
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:
***
*****
*******
Page 17 of 20
*********
2 3
f =1−x + x −x +…
Sample run:
F = 61
70.Write a C/C++ program that prints the whole range of binary numbers
for a specified number of digits.
Sample run:
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:
5 is found.
Page 19 of 20