Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Lab 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;
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 2 – Design (Pseudocode/Flowchart)


----Try to design your own pseudocode/flowchart

Step 3 – Coding

1.
2. Calculate and display the average of 2 integer numbers (Use data type conversion).

Step 1 – Problem analysis


----Try to analyse the problem on your own

Step 2 – Design (Pseudocode/Flowchart)


----Try to design your own pseudocode/flowchart

Step 3 – Coding / implementation


3. Display the area of a rectangle by accepting length and width from the user.

Step 1 – Problem analysis


Input: length, width
Process: area= length * width
Output: area

Step 2 – Design (Pseudocode/Flowchart)


----Try to design your own pseudocode/flowchart

Step 3 – Coding / implementation


4. Calculate the volume of a cylinder.

Step 1- Problem Analysis


Input: radius, height, pi = 3.142
Process: volume = pi * (radius * radius) * height
Output: volume

Step 2 – Design (Pseudocode/Flowchart)


Start
pi = 3.142
Read radius, height
Calculate volume of cylinder = pi * (radius * radius) * height
Display volume
End

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.

Step 1- Problem Analysis


Input: weight, height
Process: bmi = weight / (height * height)
Output: bmi, overweight, underweight, ideal

Step 2 – Program Design (Pseudocode/Flowchart)


Step 3 – Coding / Implementation

You might also like