Sample code in C++
Task: Please try in cxx droid for u to familiarized the used of C++ building block
Problem 1. Write a C++ progtam
#include <iostream> // include the iostream library for input/output
using namespace std;
int main() {
// Declare variables
string name;
int age;
// Basic input
cout << "Enter your name: ";
cin >> name; // Read user input for name
cout << "Enter your age: ";
cin >> age; // Read user input for age
// Basic output
cout << "Hello, " << name << "!" << endl;
cout << "You are " << age << " years old." << endl;
return 0; // End of program
}
Sample Out:
Hello Armilyn!
You are 40 years old.
Problem 2: Sum
The program asks the user to input two integers.
It calculates the sum of x and y and stores it in sum.
It prints the result on the screen
#include <iostream>
using namespace std;
int main() {
// Declare integer variables
int x, y, sum;
// Ask user for input
cout << "Enter first integer (x): ";
cin >> x;
cout << "Enter second integer (y): ";
cin >> y;
// Calculate sum
sum = x + y;
// Display the result
cout << "The sum of " << x << " and " << y << " is " << sum << "." << endl;
return 0;
}
Sample output:
Enter the first integer (x )= 10
Enter the second integer (y)= 20
The sum of x and y is 30
Example #3
Machine problem: Write a C++ program that computes the net salary of an employee, where the salary
is computed as Gross Pay minus Total Deductions. Gross Pay is calculated as the number of hours
worked multiplied by the rate per hour, and Total Deductions is the sum of SSS, PhilHealth, Tax, and Pag-
IBIG
#include <iostream>
using namespace std;
int main() {
float hours, ratePerHour;
float grossPay, totalDeductions, netSalary;
float sss, philHealth, tax, pagIbig;
// Input
cout << "Enter number of hours worked: ";
cin >> hours;
cout << "Enter rate per hour: ";
cin >> ratePerHour;
cout << "Enter SSS deduction: ";
cin >> sss;
cout << "Enter PhilHealth deduction: ";
cin >> philHealth;
cout << "Enter Tax deduction: ";
cin >> tax;
cout << "Enter Pag-IBIG deduction: ";
cin >> pagIbig;
// Process
grossPay = hours * ratePerHour;
totalDeductions = sss + philHealth + tax + pagIbig;
netSalary = grossPay - totalDeductions;
// Output
cout << "\nGross Pay: " << grossPay;
cout << "\nTotal Deductions: " << totalDeductions;
cout << "\nNet Salary: " << netSalary;
return 0;
}
Sample output:
Enter number of hours worked: 40
Enter rate per hour: 150
Enter SSS deduction: 500
Enter PhilHealth deduction: 200
Enter Tax deduction: 800
Enter Pag-IBIG deduction: 100
Gross Pay: 6000
Total Deductions: 1600
Net Salary: 4400
Task to do
1. Read flowcharting symbol (Search in the internet)
2. Read the technique on how to draw a flowchart (Search in the Internet or
Refer to your note during the discussion)
3. Read the building block of C++ programming (Search in the Internet or
Refer to your note during the discussion)
Good Luck and God Bless