Technology Advances
Basic I/O, Variables and Constants
1. Hello World:
Code Listing:
(Note: lines started with // are comments, which will be ignored by the compiler)
// Purpose: A simple C++ program which prints "Hello World!" on the screen
#include // need this header file to support the C++ I/O system
using namespace std; // telling the compiler to use namespace "std",
// where the entire C++ library is declared.
int main()
{
// Print out a sentence on the screen.
// "<<" causes the expression on its right to
// be directed to the device on its left.
// "cout" is the standard output device -- the screen.
cout << "Hello World!" << endl;
return 0; // returns 0,which indicate the successful
// termination of the "main" function
}
using namespace std; // telling the compiler to use namespace "std",
// where the entire C++ library is declared.
int main()
{
// Print out a sentence on the screen.
// "<<" causes the expression on its right to
// be directed to the device on its left.
// "cout" is the standard output device -- the screen.
cout << "Hello World!" << endl;
return 0; // returns 0,which indicate the successful
// termination of the "main" function
}
Running session:
Hello World!
2. Variables and Constants
Code listing:
// Purpose: Demonstrate the use of variables and constants
//
#include
using namespace std;
// declaring a constant. It's value cannot be changed.
const int CONST_VAL = 5;
int main()
{
int iValue; // An integer variable
float fValue; // A floating point variable
char cValue; // A character variable
iValue = 1234; // Assigns 1234 to iValue
fValue = 1234.56; // Assigns 1234.56 to fValue
cValue = 'A'; // Assigns A to cValue
// Now print them out on the screen:
cout << "Integer value is: " << iValue << endl;
cout << "Float value is: " << fValue << endl;
cout << "Character value is: " << cValue << endl;
cout << "The constant is: " << CONST_VAL << endl;
return 0;
}
using namespace std;
// declaring a constant. It's value cannot be changed.
const int CONST_VAL = 5;
int main()
{
int iValue; // An integer variable
float fValue; // A floating point variable
char cValue; // A character variable
iValue = 1234; // Assigns 1234 to iValue
fValue = 1234.56; // Assigns 1234.56 to fValue
cValue = 'A'; // Assigns A to cValue
// Now print them out on the screen:
cout << "Integer value is: " << iValue << endl;
cout << "Float value is: " << fValue << endl;
cout << "Character value is: " << cValue << endl;
cout << "The constant is: " << CONST_VAL << endl;
return 0;
}
Running session:
Integer value is: 1234
Float value is: 1234.56
Character value is: A
The constant is: 5
3. Basic IO
Code listing:
// Purpose: Converts gallons to liters
#include
using namespace std;
int main()
{
float gallons, liters;
cout << "Enter number of gallons: ";
cin >> gallons; // Read the inputs from the user
liters = gallons * 3.7854; // convert to liters
cout << "Liters: " << liters << endl;
return 0;
}
using namespace std;
int main()
{
float gallons, liters;
cout << "Enter number of gallons: ";
cin >> gallons; // Read the inputs from the user
liters = gallons * 3.7854; // convert to liters
cout << "Liters: " << liters << endl;
return 0;
}
Running session:
Enter number of gallons: 14
Liters: 52.9956
Contact
Microlaunch
Rayat Institute of Engineering and Information Technology, Railmajra, Near Ropar (Chandigarh)
+91-9463349819
microlaunch@rediffmail.com
Rayat Institute of Engineering and Information Technology, Railmajra, Near Ropar (Chandigarh)
+91-9463349819
microlaunch@rediffmail.com