0% found this document useful (0 votes)
31 views4 pages

Lab 3

Uploaded by

reloca1570
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)
31 views4 pages

Lab 3

Uploaded by

reloca1570
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/ 4

Programming Fundamentals (CS-116) LAB # 3

LAB # 3

DECLARING VARIABLES AND TO TAKE INPUT FROM USER

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
(Department of Computer Science and Information Technology) Page 1
Programming Fundamentals (CS-116) LAB # 3

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


(Department of Computer Science and Information Technology) Page 2
Programming Fundamentals (CS-116) LAB # 3

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

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


(Department of Computer Science and Information Technology) Page 3
Programming Fundamentals (CS-116) LAB # 3

Lab Tasks

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

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

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

4- Write a C++ program to convert temperature from degree Celsius to Fahrenheit.

/* celsius to fahrenheit conversion formula */

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

5- Mention output for following code fragment.

cout << "Size of char : " << sizeof(char) << endl;


cout << "Size of int : " << sizeof(int) << endl;
cout << "Size of short int : " << sizeof(short int) << endl;
cout << "Size of long int : " << sizeof(long int) << endl;
cout << "Size of float : " << sizeof(float) << endl;
cout << "Size of double : " << sizeof(double) << endl;
cout << "Size of wchar_t : " << sizeof(wchar_t) << endl;

6- State the order of evaluation the operation in each of the following C++ statements,
and show the value of x after each statement is performed.

a) x = 7 + 3 * 6 / 2 - 1;
b) x = 2 % 2 + 2 * 2 - 2 / 2;

7- Write equivalent C++ statements for given mathematical expressions

a) root =

b) z =

Sir Syed University of Engineering and Technology


(Department of Computer Science and Information Technology) Page 4

You might also like