Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
40 views

CSC126 Tutorial 3

The document discusses basic elements in C++ programming including declaring variables of different data types like int and string, assigning values to variables, and using input and output streams like cin and cout to take user input and display output. It provides examples of variable declarations, assignments, and using cin and cout. It also lists 7 tasks for readers to practice writing basic C++ programs involving variables, input, output, and calculations.

Uploaded by

maya adam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

CSC126 Tutorial 3

The document discusses basic elements in C++ programming including declaring variables of different data types like int and string, assigning values to variables, and using input and output streams like cin and cout to take user input and display output. It provides examples of variable declarations, assignments, and using cin and cout. It also lists 7 tasks for readers to practice writing basic C++ programs involving variables, input, output, and calculations.

Uploaded by

maya adam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Tutorial 3: Basic Element in C++

Objectives:

1. Define the concept of variable in C++


2. Practice writing, compiling and running basic C++ programs with cin statements
3. Identify and declare variables that are required to solve a particular problem

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

Trace the output of the following statement

#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;

cout << "Enter your name \n";


cin >> firstName;
cout << "Enter your age \n";
cin >> age;
cout << "Hello " << firstName << "Your age is " << age << endl;
}

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)

You might also like