Lecture 5 Input Output Functions
Lecture 5 Input Output Functions
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>
#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
#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
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
• 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
#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.