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

Lecture 5 Input Output Functions

Uploaded by

ahra8462967
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)
6 views

Lecture 5 Input Output Functions

Uploaded by

ahra8462967
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/ 30

C++

Input Output
Functions/casting
Lecture 5
Taking Input in the Program

#include <iostream.h>
Output
#include <conio.h> Enter first value 4
void main(void) Enter second value 3
4+3=7
{ clrscr();
int a, b;
cout<<"Enter first value ";
cin>>a;
cout<<"Enter second value ";
cin>>b;
cout<<a<<" + "<<b<<" = "<<a+b;
getch();
}
Temperature Conversion Program

#include <conio.h>
void main(void)
{ clrscr();
int fah;
cout<<"Enter Temperature in Fahrenheit ";
cin>>fah;
int cel = (fah - 32) * 5/9;
cout<<"Equivalent temperature in Celsius is "<<cel;
getch();
}
Const Qualifier

• The qualifier const variable is used to indicate that its value will
not be changed.
• A const must be initialized with some value.
The Const Qualifier

#include <iostream.h> Output


#include <conio.h> Enter radius of circle 0.5
Area of the circle is 0.785
void main(void)
{ clrscr();
float radius;
const float PIE = 3.14;
cout<<"Enter radius of circle ";
cin>>radius;
float area = PIE * radius * radius;
cout<<"Area of the circle is "<<area;
getch();
}
TASK : what will be the output of this
code.
#include <iostream.h>
#include <conio.h>
void main(void)
{ int radius;
float PIE = 3.14;
cout<<"Enter radius of circle ";
cin>>radius;
float area = PIE * radius * radius;
cout<<"Area of the circle is "<<area;
getch();
}
Type Conversion

• If we try to perform some Arithmetic operation on different data


types, i.e. int, float and double etc then C/C++ calculates the
result of such type of Arithmetic expression without giving any
error.
Type Conversion

#include <iostream.h>
#include <conio.h>
void main(void)
{ clrscr();
int count=7;
float weight=200.5;
double totalweight = count * weight;
cout<<"Total weight calculated is "<<totalweight;
getch();
}
Output : Total weight calculated is 1403.5
Type Conversation

• A type cast is basically a conversion from one type to another.


• There are two types of type conversion:
• Implicit Type Conversion
• Explicit Type Conversion
Implicit Type Conversion
• Done by the compiler on its own, without any external trigger from the user.
using namespace std;
int main()
{
int x = 10; // integer x
char y = 'a'; // character y
// y implicitly converted to int. ASCII , value of 'a' is 97
x = x + y;
// x is implicitly converted to float
float z = x + 1.0;
cout << "x = " << x << endl
<< "y = " << y << endl
<< "z = " << z << endl;
return 0;
}
Explicit Type Conversion
This process is also called type casting and it is user-
defined.

//C++ program to demonstrate explicit type casting


#include <iostream.h>
using namespace std;
int main()
{
double x = 1.2;
// Explicit conversion from double to int
int sum = (int)x + 1;
cout << "Sum = " << sum;
return 0;
}
Casts

• Cast is a way through which we change the type of the variable


during the execution of the program for a limited time, because
variables previously defined type can not calculate the values
correctly due to its low range.
Output :
Casts Result is -1214
Result now is 25000

#include <conio.h>
void main(void)
{ clrscr();
int test=25000; //range is -32,768 to +32,767
test = (test * 10)/10;
cout<<"Result is "<<test<<endl;
test = 25000;
test = (long(test)*10)/10;
cout<<"Result now is "<<test;
getch();
}
Arithmetic Operators

• Following are the basic Arithmetic operators used in C/C++:


i) + (Addition)
ii) - (Subtraction)
iii) * (Multiplication)
iv) / (Division)
Arithmetic Operators

• Apart from the specified basic operators, there are


some other operators used in C/C++, and are

v) % (Remainder or Modulus)
vi) ++ (Increment)
vii) -- (Decrement)
viii) += (Increment Assignment)
ix) -= (Decrement Assignment)
x) *= (Multiplication Assignment)
xi) /= (Division Assignment)
xii) %= (Remainder Assignment)
Arithmetic Operators

•Increment and Decrement


operators can be used in two
ways, i.e.
• i) Prefix
• ++var, --var

• ii) Postfix
• Var++, var--
Basic operators
#include <iostream.h>
#include <conio.h>
void main(void)
{ clrscr();
int a=5,b=2;
cout<<"A = 5 and B = 2"<<endl<<endl;
cout<<a<<" + "<<b<<" = "<<a+b<<endl;
cout<<a<<" - "<<b<<" = "<<a-b<<endl;
cout<<a<<" x "<<b<<" = "<<a*b<<endl;
cout<<a<<" / "<<b<<" = "<<a/b<<endl;
cout<<a<<" % "<<b<<" = "<<a%b;
getch();
}
Other operators
#include <iostream.h>
#include <conio.h>
void main(void)
{ clrscr();
int a=5,b=2;
a+=b;
cout<<"a += b means value of a is "<<a<<endl;
a=5,b=2;
a-=b;
cout<<"a -= b means value of a is "<<a<<endl;
a=5,b=2;
a*=b;
cout<<"a *= b means value of a is "<<a<<endl;
a=5,b=2;
a/=b;
cout<<"a /= b means value of a is "<<a<<endl;
a=5,b=2;
a%=b;
cout<<"a %= b means value of a is "<<a<<endl;
getch();
}
Prefix Increment

• int x = 5;
• int y = ++x;
• // x is now equal to 6, and 6 is assigned to y
Postfix increment

• int x = 5;
• int y = x++;
• // 5 is assigned to y, and x is now equal to 6
Example

int x = 5, y = 5;
cout << x << " " << y << '\n';
// prefix
cout << ++x << " " << --y << '\n';
cout << x << " " << y << '\n';
// postfix
cout << x++ << " " << y-- << '\n';
cout << x << " " << y << '\n';
Prefix and Postfix

Suppose, a = 5 then,
++a; //a becomes 6
a++; //a becomes 7
--a; //a becomes 6
a--; //a becomes 5
Prefix - Postfix

#include <iostream.h>
#include <conio.h>
void main(void)
{ clrscr();
int a=5;
cout<<"Value of A now is "<<a<<endl<<endl;
cout<<"Prefix operator ++a gives "<<++a<<endl;
cout<<"Value of a after Prefix is "<<a<<endl<<endl;
cout<<"Postfix operator a++ gives "<<a++<<endl;
cout<<"Value of a after Postfix "<<a<<endl<<endl;
getch();
}
• #include <iostream>
• using std::cout;
• using std::endl;
• int main()
• {
• int c;
• c = 5; // assign 5 to c 13
• cout << c << endl;
• cout << c++ << endl;
• cout << c << endl;
• cout << endl;
• c = 5;
• cout << c << endl;
• cout << ++c << endl;
• cout << c << endl;
• }
QUESTION

Inspect the following code:


int x = 99;
int y = 10;
y = ++x ;
// prefix increment operator
cout<<"x: " + x + " y: " + y ;
What does this fragment write out?
Relational Operators

A relational operator compares two values.


Comparisons involved in relation operators can be
i) < Less than
ii) > Greater than
iii) == Equals to
iv) != Not equals
v) <= Less than or equals
vi) >= Greater than or equals
If result = 1,means True
If result= 0, it means False.
Relational Operators

#include <iostream.h> Output


Enter a Number 10
#include <conio.h> number < 10 = 0
void main(void) number > 10 = 0
number == 10 = 1
{ clrscr();
int number;
cout<<"Enter a Number ";
cin>>number;
cout<<"number < 10 = "<<(number<10)<<endl;
cout<<"number > 10 = "<<(number>10)<<endl;
cout<<"number == 10 = "<<(number==10)<<endl;
getch();
}
Using Library Functions

#include <iostream.h>
#include <conio.h>
#include <math.h>
void main(void)
{ clrscr();
int a;
cout<<"Enter a value ";
cin>>a;
cout<<"Square Root of "<<a<<" is "<<sqrt(a);
getch();
}
Using Library Functions

Assignment
Use at least 15 library function from more than one header files
in a program.

You might also like