Lab Manual 03
Lab Manual 03
LAB MANUAL 03
INPUT/OUTPUT
Lab Objectives:
At the end of this lab students will know about
cin is a predefined variable that reads data from the keyboard with the
extraction operator (>>).
In the following example, the user can input a number, which is stored in the
variable x. Then we print the value of x:
EXAMPLE
int x;
cout << "Type a number: "; // Type a number and press enter
cin >> x; // Get user input from the keyboard
cout << "Your number is: " << x; // Display the input value
STRING TYPES
The string type is used to store a sequence of characters (text). This is not a
built-in type, but it behaves like one in its most basic usage. String values must
be surrounded by double quotes:
EXAMPLE
To use strings, you must include an additional header file in the source code,
the <string> library:
EXAMPLE
// Include the string library
#include <string>
BOOLEAN TYPES
A boolean data type is declared with the bool keyword and can only take the
values true or false. When the value is returned, true = 1 and false = 0.
EXAMPLE
bool isCodingFun = true;
bool isFishTasty = false;
cout << isCodingFun; // Outputs 1 (true)
cout << isFishTasty; // Outputs 0 (false)
ARITHMETIC OPERATORS
Arithmetic operators are used to perform common mathematical operations.
ASSIGNMENT OPERATORS
Assignment operators are used to assign values to variables.
In the example below, we use the assignment operator (=) to assign the
value 10 to a variable called x:
EXAMPLE
int x = 10;
COMPARISON OPERATORS
Comparison operators are used to compare two values.
In the following example, we use the greater than operator (>) to find out if 5
is greater than 3:
EXAMPLE
int x = 5;
int y = 3;
cout << (x > y); // returns 1 (true) because 5 is greater than 3
== Equal to x == y
!= Not equal x != y
y += x; y = y + x;
x -= 5; x = x - 5;
x /= y; x = x / y;
and the same for all other compound assignment operators. For example :
// compound assignment operators
#include <iostream>
using namespace std;
int main ()
{
int a, b=3;
a = b;
a+=2; // equivalent to a=a+2
cout << a;
}
Tasks:
Question # 1: Write a program to take two float numbers then find
remainder of them by using type casting in integer form.
Question # 2: Write a program to take input name, address and age from
user, then display data on screen.
Question # 3: Write a program to take input a character and display its
ASCII code.
Question # 4: Take an amount from user, interest rate and number of
years from user let suppose 1000, 5% and 3. Find Interest amount for
those years. (e.g. output for above values is 150)
Question # 5: Write a program to take dividend and divisor. Then
display the quotient and remainder.(e.g. 20 3. Quotient=6,
Remainder=2)
Question # 6: Write a program to take input base and height of triangle.
Now calculate area by using formula Area=1/2 x base x height;.
Question # 7: Write a program to take temperature in Celsius and
convert it into Fahrenheit by using F=9/5 *C +32;.