/* ************************************************************************** * Program name : 001_Basic_input_and_output (Version 1.00) * * Author : Duck Wong * * Language : C / C++ * * Compiler : Boodshed Dec-C++ compiler Ver 3.95 * * Computer : PII350 * * O/S : Windows 98 * ************************************************************************** * Version 1.00 : 2000/06/03 - first version * ************************************************************************** * Description : (a) basic command for sending output to screen * * (b) statement of assigning value * * (c) Ouput results * * (d) arithmetic operator & logical operator * ************************************************************************** */ #include // Note (1) #include // Note (2) int main() // C++ program runs this function "Main" before others { // begining // Section A : output to screen cout << "\n"; // Note (3) cout << "Welcome to C++!\n"; cout << "\nHave a \t"; cout << "nice day!\a\n"; cout << "\\ \" \n\n"; // Section B : statement of assigning value // declaration statements int integer1, integer2, sum; // Note (4) // executable statements cout << "Enter first integer :"; // prompt the user to input an integer cin >> integer1; // read the value of integer1 cout << "Enter second integer :"; // prompt the user to input another integer cin >> integer2; // read the value of integer2 sum = integer1+integer2; // assignment statement : assign the value // of identifier sum, use "=" not ":=" cout << "The sum is "; // output the character string "The sum is" cout << sum << ".\n" << endl; // output the value of identifier sum // endl (end line) forces any accumlated // outputs to be printed at that moment. // Section C : basic calculation such as +,-,*,/ and modulus // Note (5) cout << "Addition : " << integer1 + integer2 << "\n"; cout << "Subtraction : " << integer1 - integer2 << "\n"; cout << "Multiplication : " << integer1 * integer2 << "\n"; cout << "Division : " << integer1 / integer2 << "\n"; cout << "Modulus(Remainder) : " << integer1 % integer2 << "\n" << endl; // Section D : basic operator // Note (6) & (7) if (integer1 == integer2) cout << integer1 << " is equal to " << integer2 << endl; else cout << integer1 << " is not equal to " << integer2 << endl; cout << "\n"; if (integer1 != integer2) cout << integer1 << " is not equal to " << integer2 << endl; else cout << integer1 << " is equal to " << integer2 << endl; cout << "\n"; if (integer1 >= integer2) cout << integer1 << " is greater than or equal to " << integer2 << endl; else cout << integer1 << " is less than " << integer2 << endl; cout << "\n"; if (integer1 <= integer2) cout << integer1 << " is less than or equal to " << integer2 << endl; else cout << integer1 << " is greater than " << integer2 << endl; cout << "\n"; if (integer1%2==0) cout << integer1 << " is an even number " << endl; else cout << integer1 << " is an odd number " << endl; cout << "\n"; if (integer1%integer2==0) cout << integer2 << " is a divisor of " << integer1 << endl; else cout << integer2 << " is not a divisor of " << integer1 << endl; cout << "\n"; system("PAUSE"); // press any key to continue ... return 0; // indicate that program (function) ended successfully } // end /* Notes (1) #include means tells the preprocessor to include the contents of the input/output stream header (2) #include means standard library (3) \n\n means skip one blank line \n means newline \t means tab \a means alert (sound) \\ or \" means print specifical character \ or " \f means formfeed \b means backspace \v means vertical tab \r means return to the beginning of line \xhh means Hexadecimal byte (4) declaration can be placed anywhere and identifier name is case sensitive i.e. integer1 is not equal to Integer1 (5) Operators : a/b == a div b in pascal if a is an integer a/b == a / b in pascal if a is real (floating-point number) a%b == a mod b (in pascal) , works for integer only (6) Basic operator : == equal to != not equal to > greater than >= greater than or equal to < less than <= less than or equal to (7) Unlike pascal (a) there is a semi-colon before the keyword "ELSE" (b) No need to use the keyword "THEN" (c) the expression (condition) must be enclosed */