Master C++ Programming: Data types, operators


 4 .3. Data types : 
a) Basic Data Types: Data types that are built-in or predefined Basic Data Types, declaration of variable.
1. int : 
i) int is keyword used for integer value 
ii) Integer is number that does not include fraction. 
iii) Size of int is 2 bytes and range is -34768 to +34767
iv) Ex: int n1; //n1 is variable of integer type.

2. float : 
i) float is keyword used for real value
ii) float is number that include fraction.
iii) Size of float is 4 bytes and range is -
iv) Ex: float n1; //n1 is variable of float type

3. char : 
i) char is keyword used for non-numeric value. 
ii) char can store single char.
iii) Size of int is 1byte and range is -128 to +127
iv) Ex: char ch; // ch is variable of character type that can store one char

4. double: 
i) It is data type that includes fraction part with larger precision. 
ii) Keyword is used as double. 
iii) Size of double is 8 bytes and range is : 1.7 x 10-308 to 3.4X10308 (gives 15digits precision) 
iv) Ex: double n1; //n1 is variable of double type

5. void :
i) It is type that has no value.
ii) It cannot be used with variable declaration.
iii) It is used with function to specify that function does not return value.
iv) Ex: void sum( ); //n1 is variable of double type


b) Derived data types : Data types that are derived from primary data types ( like array, function, pointer, reference) ,

c) User derived data types : Data types that are defined by user (class, structure, union, enum, typedef) constants, declaration of variable.

Modifiers:
It is used with built-in data type that gives more precise value by increasing larger size of data types:
1. Signed (int, char, long (prefix))
2. Unsigned(int,char, short (prefix)) 
3. Long (int,double)
4. Short (int) : same as int

1. Signed (int, char, long (prefix) :
signed can be used char , int, short int, long int 

Range of modifiers:
i) signed char (-128 to 127)
ii) signed int ( same as int)
iii) signed short int ( same as int)
iv) signed long int ( 4 bytes with range -2147483648 to 2147483647)

2. Unsigned(int,char, short (prefix) : unsigned can be also used char , int, short int, long int 

i) unsigned char (1 byte 0 to 255)
ii) unsigned int ( 2 bytes 0 to 65535)
iii) unsigned short int (2 bytes 0 to 65535)
iv) signed long int ( 4 bytes with range – 0 to 4294967295)

Operator in C++:
Operator is symbol (like +,-,* etc) used to perform operationwith the help of operands. It is basically of two types 
                            1. Unary Operator 
                            2. Binary Operator

1. Unary Operator : This type of operator is used to perform operations on only one operand like ++, - - ,+,- etc. For Ex. ++a, -a etc 

2. Binary Operator : This type of operator is used to perform operations on two operands like +,-,/,* ,% etc. For Ex. a + b , a-b etc

1. Arithmetic Operator:
Arithmetic operator is used to perform arithmetic operations

i) + (Plus) : It is used to perform addition operation like a+b where a and b both are operand and + is operator.
ii) – (Minus): It is used to perform subtraction operation like a-b where a and b both are operand and - is operator.
iii) * (Multiply): It is used to perform multiplication operation like a * b where a and b both are operand and * is operator.
iv) / (Divide) : It is used to perform division operation to find quotient like 5 / 2 that gives answer(quotient) as 2 , where 5 and 2 both are operand and / is operator.
v) % (Modulus): It is basically known as division operation that is used to find remainder like 5 % 2 gives remainder as 1. 

Program :
                Ex a=7,b=5 
                result = a+b ; 12 
                            a-b; 2
                            a*b; 35
                            a/b; 1

a%b; 2 
while performing % sign of result is always the sign of first operand 
1)-16%3 = - 1 and 16% - 3 = 1


2. Assignment Operator :
1. Operator ' = ' is used for assignment, it takes the right-hand side (called R-value) and copy it into the left-hand side (called L-value)
2. Assignment operators are used to assign values to variables.
3. In the example below, we use the assignment operator (=) to assign the value 10 to a variable called x: 
         
        int x = 10;

 =                     x=5                    x=5
+=                    x+=3                  x=x+3
-=                     x-=3                   x=x-3
*=                     x*=3                  x=x*3
/=                      x/=3                   x=x/3


3. Relational Operator (<,<=,>,>=,==,!=) : To establish relations between two entities relational operator is used. 
a) A>B OR A>=B b) AB OR A=5 OR 5<=5 4. Logical Operators : (AND, OR and NOT) a) If (A >B AND A>C) (&&) OR If (A> B OR A>C) ( || )etc. A = 5 B =3 C=2 
b) NOT ( ! ) A=5 B=6 C=3 
AND : It returns True, if both the conditions are True otherwise returns False
OR : It Returns False if both the conditions are False otherwise returns True.

* AND and OR Operators are used to combine multiple if conditions in one If instead of writing separately if . 
NOT : Gives opposite answer that means if True then returns False or vice versa. 

Control Flow/Structure/Statements:
Control flow decide how the program will run/execute. So, by considering this, the logic of program can be divided into three parts. 
1. Sequence based 
2.Selection/Conditional based
3.Iteration/Repetition/Loop 

based Sequence Based Logic:
In sequence-based, logic of program’s statement is written one after another and also executes same way that too only once.



1. Write a program to enter two numbers and find the addition of two numbers. 
Program
#include<iostream.h>
#include<conio.h>

void main() 
{ 
 int n1,n2,r;
 clrscr();
 
 cout<<”Enter the two values “;
 cin>>n1>>n2; 
 
 r=n1+n2;
 cout<< “ The addition of two numbers 
 is :” << r;
                    
 getch();
}
2. Convert temperature in Fahrenheit to Celsius. ( C=(F-32)*(5.0/9.0);)
   (Practice)
Program
#include<iostream.h>
#include<conio.h>
                    
void main()
{
 float f,c;
 clrscr();
 
 cout<<”Enter the value of f : “;
 cin>>f;
 
 c=(f-32)*(5.0/9.0);
 cout<< "Thetemperature in Celsiusis 
 :” <<c;
 getch();
}
      
3. Write a program to find the average of three integers.
Program
#include<iostream.h>        
#include<conio.h>

void main()
{
 int n1,n2,n3;
 float avg;
 clrscr();
 
 cout<<”Enter the three values “;
 cin>>n1>>n2>>n3;
 
 avg=(n1+n2+n3)/3.0;
 cout<< “ The Entered numbers average 
 is :” <<avg;
 getch();
}
                

 0/p : 14 14 15 = 14.3333 (wb)
        14 14 15/3 =33 (without b)
        14 14 17/3.0 =33.6667

4.Write a Program to enter 3 digits-based number and print sum of all digits
present in that number.(sequence based). d1=n%10
Program
#include<iostream.h>
#include<conio.h>

void main()
{
 int n,d1,d2,d3,sum;
 clrscr();
 
 cout<<”Enter the three digits-based value “;       
 
 cin>>n;
 d1=n%10;
 d2=(n/10)%10;
 d3=n/100;
 sum=d1+d2+d3;
 cout<< “ The Entered number digits sum is :” <<sum ;
 
 getch();
}
      

Post a Comment

0 Comments