CSC126 Tutorial 3
CSC126 Tutorial 3
Objectives:
Variable Declarations
• declaration tells the compiler what kind of data is going to be stored in the variable
• int, float, string, char
• Example:
Ø int num1;
Ø string firstName;
Assignment
• Is an order to the computer to set the value of the variable on the left hand side of the equation
to what is written on the right hand side.
• Example:
Ø int num1= 2;
Ø string firstName = “John”;
Input
• cin - is used to fill the values of variables with the input from the user of the program
• >> is called extraction operator
• Example:
cout << "Enter your first name: ";
cin >> firstName;
Task 1
#include <iostream>
using namespace std;
int main ()
{
int x=5;
int y=10;
int sum;
string bookTitle = “C++ Programming Language”;
sum = x+y;
cout << "x+y= " << sum << endl;
cout << bookTitle;
}
Task 2
Trace the output of the following statement
#include <iostream>
using namespace std;
int main ()
{
string firstName;
int age;
Task 3
Design a program that display the area of a rectangle by accepting length and width from the
user.
Task 4
Design a program to convert temperature in degrees Fahrenheit to degrees Celsius. The
equation for this conversion is:
C = (F-32) x (5/9)
Task 5
One of the methods that can be used to convert cat years to human’s years is using factor of
7. For example, a 1 year old human is equivalent to 7 years old cat. That is the main reason
why cats mature quickly that human. Write a program to convert the convert human age to
cat age.
Task 6
BMI is used to estimate your total amount of body fat. It is calculated by dividing your
weight in kilograms by your height in meters squared. Write a program to calculate the BMI
of a person.
Task 7
Write a program converts Malaysian Ringgit (RM) to US Dollar (USD) and Korean Won
(Formula 1USD = RM 4.10, 1000KRW = RM3.69)