Professional Documents
Culture Documents
Lab 3:basic Element in C++: Variable Declarations
Lab 3:basic Element in C++: Variable Declarations
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;
Sample Exercise and Solution
1. Display the sum, difference, product, quotient, and remainder of 2 decimal numbers
entered by user.
Step 1 – Analysis
Input: 2 decimal numbers (num1, num2)
Process: sum = num1 + num2
difference = num1 – num2;
product = num1 * num2
quotient = num1 / num2
remainder = num1 % num2
Output: sum, difference, product, quotient, remainder
Step 3 – Coding
1.
2. Calculate and display the average of 2 integer numbers (Use data type conversion).
Step 3 – Coding/Implentation
5.
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. Using your BMI, you
can determine whether you are overweight, underweight or of ideal weight. Write a
program to calculate the BMI of a person.