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

Source Codes Examples

Uploaded by

tmtommy655
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)
3 views4 pages

Source Codes Examples

Uploaded by

tmtommy655
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

Example 1: Simple Addition Calculator

#include <iostream> // Library for input and output

using namespace std; // Allows use of standard commands without


std::

int main() // Main function starts here


{
int num1; // Variable to store first number
int num2; // Variable to store second number
int sum; // Variable to store addition result

cout << "Enter first number: "; // Ask user to enter first
number
cin >> num1; // Receive first number from keyboard

cout << "Enter second number: "; // Ask user to enter second
number
cin >> num2; // Receive second number from keyboard

sum = num1 + num2; // Add the two numbers

cout << "Sum = " << sum << endl; // Display the result

return 0; // End program successfully


}

Example 2: Subtraction Calculator

#include <iostream> // Input and output library

using namespace std; // Standard namespace

int main() // Main program function


{
float number1; // First number variable
float number2; // Second number variable
float answer; // Result variable

cout << "Enter first number: "; // Ask for first number
cin >> number1; // Read first number

cout << "Enter second number: "; // Ask for second number
cin >> number2; // Read second number
answer = number1 - number2; // Perform subtraction

cout << "Subtraction Result = " << answer << endl; // Show
result

return 0; // Program ends


}

Example 3: Multiplication Calculator

#include <iostream> // Includes input/output functions

using namespace std; // Uses standard namespace

int main() // Starting point of program


{
double a; // First decimal number
double b; // Second decimal number
double product; // Stores multiplication result

cout << "Enter first value: "; // Prompt user


cin >> a; // Input first value

cout << "Enter second value: "; // Prompt user


cin >> b; // Input second value

product = a * b; // Multiply numbers

cout << "Multiplication Result = " << product << endl; //


Display answer

return 0; // Exit program


}

Example 4: Division Calculator

#include <iostream> // Input and output library

using namespace std; // Standard namespace

int main() // Main function


{
float x; // Numerator
float y; // Denominator
float result; // Division answer
cout << "Enter numerator: "; // Ask user for numerator
cin >> x; // Input numerator

cout << "Enter denominator: "; // Ask user for denominator


cin >> y; // Input denominator

result = x / y; // Divide numbers

cout << "Division Result = " << result << endl; // Show
division result

return 0; // End of program


}

Example 5: Simple Full Calculator

#include <iostream> // Library for input and output

using namespace std; // Standard namespace

int main() // Program execution starts here


{
float num1; // First number
float num2; // Second number
float result; // Stores result
char operation; // Stores mathematical operator

cout << "Enter first number: "; // Ask for first number
cin >> num1; // Read first number

cout << "Enter operator (+, -, *, /): "; // Ask for operator
cin >> operation; // Read operator

cout << "Enter second number: "; // Ask for second number
cin >> num2; // Read second number

if(operation == '+') // Check if operator is addition


{
result = num1 + num2; // Perform addition
}
else if(operation == '-') // Check subtraction
{
result = num1 - num2; // Perform subtraction
}
else if(operation == '*') // Check multiplication
{
result = num1 * num2; // Perform multiplication
}
else if(operation == '/') // Check division
{
result = num1 / num2; // Perform division
}
else // If invalid operator entered
{
cout << "Invalid operator entered" << endl; // Error
message
return 0; // Stop program
}

cout << "Result = " << result << endl; // Display final
result

return 0; // Program ends successfully


}

You might also like