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

Programming Worksheet

The document contains 5 code examples that demonstrate basic C++ programs. The first example calculates the sum and product of two integers input by the user. The second prints a 'C++ is cool' message in a banner-like format. The third prints a character input by the user in a pyramid pattern. The fourth calculates the total monetary value of coins input by the user. The fifth calculates the distance an object would fall due to gravity given the time in seconds.

Uploaded by

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

Programming Worksheet

The document contains 5 code examples that demonstrate basic C++ programs. The first example calculates the sum and product of two integers input by the user. The second prints a 'C++ is cool' message in a banner-like format. The third prints a character input by the user in a pyramid pattern. The fourth calculates the total monetary value of coins input by the user. The fifth calculates the distance an object would fall due to gravity given the time in seconds.

Uploaded by

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

1.

2. #include <iostream>
3. using namespace std;
4.
5. int main()
6. {
7. int firstNumber, secondNumber;
8. int sum, product;
9.
10. // Ask the user to enter two integers
11. cout << "Enter the first integer: ";
12. cin >> firstNumber;
13.
14. cout << "Enter the second integer: ";
15. cin >> secondNumber;
16.
17. // Calculate the sum and product of the two numbers
18. sum = firstNumber + secondNumber;
19. product = firstNumber * secondNumber;
20.
21. // Output the sum and product
22. cout << "The sum of " << firstNumber << " and " << secondNumber << " is: " << sum << endl;
23. cout << "The product of " << firstNumber << " and " << secondNumber << " is: " << product << endl;
24.
25. return 0;
26. }

2.
#include <iostream>
using namespace std;

int main() {
cout << "*********************************" << endl;
cout << "* *" << endl;
cout << "* C C C S S S S !! *" << endl;
cout << "* C C S S !! *" << endl;
cout << "* C S !! *" << endl;
cout << "* C S !! *" << endl;
cout << "* C S S S !! *" << endl;
cout << "* C S !! *" << endl;
cout << "* C C S !! *" << endl;
cout << "* C C C S S S !! *" << endl;
cout << "* *" << endl;
cout << "*********************************" << endl;
cout << "\n\nComputer Science is Cool Stuff!!!" << endl;
return 0;
}
3.
#include <iostream>

using namespace std;

int main() {
char inputChar;
cout << "Enter a character: ";
cin >> inputChar;

cout << " " << inputChar << inputChar << inputChar << endl;
cout << inputChar << " " << inputChar << endl;
cout << inputChar << endl;
cout << inputChar << endl;
cout << inputChar << endl;
cout << inputChar << " " << inputChar << endl;
cout << " " << inputChar << inputChar << inputChar << endl;

return 0;
}
4.
#include <iostream>
using namespace std;

int main()
{
int numQuarters, numDimes, numNickels;
int totalCents;

// Ask the user to enter the number of quarters, dimes, and nickels
cout << "Enter the number of quarters: ";
cin >> numQuarters;

cout << "Enter the number of dimes: ";


cin >> numDimes;

cout << "Enter the number of nickels: ";


cin >> numNickels;
// Calculate the total monetary value in cents
totalCents = numQuarters * 25 + numDimes * 10 + numNickels * 5;

// Output the total monetary value in cents


cout << "The coins are worth " << totalCents << " cents." << endl;

return 0;
}
5.
#include <iostream>
using namespace std;

int main()
{
double timeInSeconds, distance;

// Ask the user to enter the time in seconds


cout << "Enter the time in seconds: ";
cin >> timeInSeconds;

// Calculate the distance the object would drop in freefall


double acceleration = 32.0; // acceleration due to gravity in feet per second
distance = 0.5 * acceleration * timeInSeconds * timeInSeconds;

// Output the distance the object would drop


cout << "The object would drop " << distance << " feet in freefall for " <<
timeInSeconds << " seconds." << endl;

return 0;
}

You might also like