0% found this document useful (0 votes)
11 views4 pages

C++ First 5 Exercises Corrected

The document contains solutions to five C++ programming exercises. These exercises include checking for perfect numbers, formatting monetary values, calculating coin values, filling spaces with periods, and producing left-justified output. Each problem is accompanied by code snippets demonstrating the solution.

Uploaded by

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

C++ First 5 Exercises Corrected

The document contains solutions to five C++ programming exercises. These exercises include checking for perfect numbers, formatting monetary values, calculating coin values, filling spaces with periods, and producing left-justified output. Each problem is accompanied by code snippets demonstrating the solution.

Uploaded by

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

C++ Problem Solutions - First 5 Exercises

Problem 1: Perfect Number Check

#include <iostream>

using namespace std;

bool isPerfect(int num) {

if (num <= 1) return false;

int sum = 0;

for (int i = 1; i <= num / 2; i++) {

if (num % i == 0) sum += i;

return sum == num;

int main() {

int number;

cout << "Enter a number: ";

cin >> number;

if (isPerfect(number))

cout << number << " is a perfect number." << endl;

else

cout << number << " is not a perfect number." << endl;

return 0;

Problem 2: Custom Money Value Manipulator


#include <iostream>

#include <iomanip>

using namespace std;

ostream& moneyFormat(ostream &out) {

out << setw(10) << setfill('*') << showpos << fixed << setprecision(2) << '$';

return out;

int main() {

double amount = 123.4;

cout << moneyFormat << amount << endl;

return 0;

Problem 3: Coin Value Calculation

#include <iostream>

using namespace std;

int main() {

int quarters, dimes, nickels;

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

cin >> quarters;

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

cin >> dimes;

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

cin >> nickels;


double total = quarters * 0.25 + dimes * 0.10 + nickels * 0.05;

cout << "Your charge totals to $" << total << endl;

return 0;

Problem 4: Filling Spaces with Periods

#include <iostream>

#include <iomanip>

using namespace std;

int main() {

cout << left << setw(15) << setfill('.') << "Portcity" << 2425785 << endl;

return 0;

Problem 5: Left-Justified Output

#include <iostream>

#include <iomanip>

using namespace std;

int main() {

cout << left;

cout << setw(15) << "Last name" << setw(15) << "First name"

<< setw(20) << "Street address" << setw(15) << "Town"

<< "State" << endl;

cout << "-------------------------------------------------------------" << endl;


cout << setw(15) << "Jones" << setw(15) << "Bernard"

<< setw(20) << "109 Pine Lane" << setw(15) << "Little town"

<< "MI" << endl;

cout << setw(15) << "O Brian" << setw(15) << "Coleen"

<< setw(20) << "42 E. 99th Ave." << setw(15) << "Bigcity"

<< "NY" << endl;

cout << setw(15) << "Wong" << setw(15) << "Harry"

<< setw(20) << "121-A Alabama St." << setw(15) << "Lakeville"

<< "I" << endl;

return 0;

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