0% found this document useful (0 votes)
36 views

Lab Session 3

The document discusses programming fundamentals related to arithmetic operators and expressions in C++. It covers various types of operators like arithmetic, assignment, relational, and logical operators. It also discusses built-in data types in C++ like integer, float, char, etc. and how to declare and initialize variables. The document provides examples of writing expressions in C++ and getting user input using the cin operator. It lists some in-lab and post-lab tasks related to arithmetic operations, data type conversions, and problem-solving using C++ programming.

Uploaded by

Muhammad Faisal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Lab Session 3

The document discusses programming fundamentals related to arithmetic operators and expressions in C++. It covers various types of operators like arithmetic, assignment, relational, and logical operators. It also discusses built-in data types in C++ like integer, float, char, etc. and how to declare and initialize variables. The document provides examples of writing expressions in C++ and getting user input using the cin operator. It lists some in-lab and post-lab tasks related to arithmetic operations, data type conversions, and problem-solving using C++ programming.

Uploaded by

Muhammad Faisal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Programming Fundamentals (CS-116) LAB # 03

LAB #3

Working with C++ Arithmetic Operators & Expressions

Building Blocks of Programming Language


In any language, there are certain building blocks:
  Operators
 Constants
  Variables
 Methods to get input from user

Operators

There are various types of operators that may be placed in these categories:

Basic: +,-,*,/,%

Power: ^

Assignment: =, +=, -=, /=, *=, %=


(++, -- may also be considered as assignment operators)

Relational: <,>,<=,>=,==,!=

Logical: &&,||,!

Variables and Constants

If the value of an item can be changed in the program then it is a variable. If it will not
Change then that item is a constant. The various variable types (also called data type)
in C+ are: int, float, char etc.

Variable declaration:

Variable are generally declared as:

Type var-name;
For example:-
int a;
int a,b; int a,b,c;
‘int’ is the data type and ‘a’ is a variable name , you can declare more than one variable
at a time by using one same data type.

Sir Syed University of Engineering and Technology 1|Page


(Department of Computer Science)
Programming Fundamentals (CS-116) LAB # 03

Primitive Built-in Types

C++ offers the programmer a rich assortment of built-in as well as user defined data
types. Following table lists down seven basic C++ data types –

Type Keyword
Boolean bool
Character char
Integer int
Floating point float
Double floating point double
Valueless void
Wide character wchar_t

Several of the basic types can be modified using one or more of these type modifiers –

  signed
 unsigned
  short
 long

The following table shows the variable type, how much memory it takes to store the value
in memory, and what is maximum and minimum value which can be stored in such type
of variables.

Type Typical Bit Width Typical Range


char 1byte -127 to 127 or 0 to 255
unsigned char 1byte 0 to 255
signed char 1byte -127 to 127
int 4bytes -2147483648 to 2147483647
unsigned int 4bytes 0 to 4294967295
signed int 4bytes -2147483648 to 2147483647
short int 2bytes -32768 to 32767
unsigned short int 2bytes 0 to 65,535
signed short int 2bytes -32768 to 32767
long int 4bytes -2,147,483,648 to 2,147,483,647
signed long int 8bytes same as long int
unsigned long int 4bytes 0 to 4,294,967,295
long long int 8bytes -(2^63) to (2^63)-1

Sir Syed University of Engineering and Technology 2|Page


(Department of Computer Science)
Programming Fundamentals (CS-116) LAB # 03

Type Typical Bit Width Typical Range


unsigned long long int 8bytes 0 to 18,446,744,073,709,551,615
float 4bytes
double 8bytes
long double 12bytes
wchar_t 2 or 4 bytes 1 wide character

Operator Precedence:

Writing Algebraic Expressions in C++

Sir Syed University of Engineering and Technology 3|Page


(Department of Computer Science)
Programming Fundamentals (CS-116) LAB # 03

C++ User Input


You have already learned that cout is used to output (print) values. Now we will use cin
to get user input. 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

In this example, the user needs to input two numbers, and then we print the sum:

Example

int x, y;
int sum;
cout << "Type a number: ";
cin >> x;
cout << "Type another number: ";
cin >> y;
sum = x + y;
cout << "Sum is: " << sum;

Sir Syed University of Engineering and Technology 4|Page


(Department of Computer Science)
Programming Fundamentals (CS-116) LAB # 03

Sir Syed University of Engineering and Technology 5|Page


(Department of Computer Science)
Programming Fundamentals (CS-116) LAB # 03

In-Lab Tasks

Task#1: Write a program that take four floating numbers from keyboard and prints their
sum, product and average.

Task#2: Write a program to calculate the area of the circle, taking the value of the radius
from the user.

Task#3: Write a program that prints to calculate the age in days by using the formula:
days = years * 365.

Task#4: Write a C++ program to convert temperature from degree Celsius to Fahrenheit.

/* celsius to fahrenheit conversion formula */

fahrenheit = (celsius * 9 / 5) + 32;

Post-Lab Tasks

Task 1:
Write a C++ program that takes person’s weight in KG, and convert it into pounds.
Your program should output both weight. (Note that 1 KG 2.2 pounds.)
Expected output:
Example 1:
Enter weight in KG: 69
Value in KG is : 69
Value in pounds is : 151.8

Example 2:
Enter weight in KG: 189
Value in KG is : 189
Value in pounds is : 415.8

Task 2:
A cashier has currency notes of denominations 10, 50 and 100. If the amount to be withdrawn is input through the
keyboard in hundreds, find the total number of currency notes of each denomination the cashier will have to give
to the withdrawer. Write a C++ program to implement the above scenario.
Expected output:
Example 1:
Enter amount in hundreds: 700
Currency note in denominations 10 are: 70
Currency note in denominations 50 are: 14
Currency note in denominations 100 are: 7
Example 2:
Enter amount in hundreds: 1400
Currency note in denominations 10 are: 140
Currency note in denominations 50 are: 28

Sir Syed University of Engineering and Technology 6|Page


(Department of Computer Science)
Programming Fundamentals (CS-116) LAB # 03

Currency note in denominations 100 are: 14

Task 3:
Write a C++ program that calculates monthly salary of an employee. The salary is calculated after deduction of
following taxes.
Income Tax: 10%
Global Life Insurance: 2.75%
Pension Plan: 2.3%
Health Insurance: 200.0 rupees
Your program should prompt the user to input the gross amount and your program should show each tax and net
salary (after tax deduction). Your output should be in same format (not same values) as shown in example below.

Expected output:
Example 1:
Enter Gross amount of employee(RS. ): 100000
Income Tax (RS.): 10000
Global Life Insurance(RS.): 2750
Pension Plan (RS.):2300
Health Insurance: 200.0 rupees
Net Salary (after tax deduction) (Rs. ): 84750

Task 4:
Write a C++ program that takes a 5-digit number in variable X. Your program should store the reverse of entered
number into variable Y. You program should also print it on screen.
Note: you are not supposed to take 5 separate numbers, but you have to take one number of 5 digits in a single
variable. Also store its reverse in a single variable.
Hint: Use % and / operator
Expected output:
Example 1:
Enter 5 Digit number in Variable X: 57391
5 Digit number in reverse order in Variable Y is:19375

Sir Syed University of Engineering and Technology 7|Page


(Department of Computer Science)
Programming Fundamentals (CS-116) LAB # 03

Sir Syed University of Engineering and Technology 8|Page


(Department of Computer Science)

You might also like