Professional Documents
Culture Documents
Q2: Write a program to input a number (say 31) and display it in hexadecimal form (e.g.0f).
Answer:
#include <iostream.h>int main(){int x;cout << Enter an integer:" <<
endl;cin >> x;// hex keyword displays a number in hexadecimal form.cout <<
"x=" << hex << x << endl;cin.get();return 0;}
Q3: Write a C++ program two swap to integers without taking a temporary variable.
Answer:
#include <iostream.h>int main(){int a = 10;int b = 20;cout << "Value of a
(before swap): " << a << endl;cout << "Value of b (before swap): " << b <<
endl;a = a + b;b = a - b;a = a - b;cout << "Value of a (after swap): " << a
<< endl;cout << "Value of b (after swap): " << b << endl;cin.get();return
0;}
#include <conio.h>
int
main()
<< endl;
<< endl;
<< endl;
<< endl;
<< endl;
<< endl;
int
x =10;
<< endl;
float
b = -200.10;
<< endl;
getch();
return
0;
}
When run (compiled with Turbo C++ 3.0), the program will give the following output:size of char: 1
bytes.size of int: 2 bytes.size of long: 4 bytes.size of float: 4 bytes.size of short: 2 bytes.size of double:
8 bytes.size of variable x: 2 bytes.size of variable b: 4 bytes.
Q2: In Turbo C++ (TC) An unsigned integer can have maximum value of 65535. What will
happen ifyou add another number to it?
Answer : In general, we say data overflow happens here. But the behaviour is same as seen in
car odometer. When the unsigned int crosses its maximum value, it wraps around and reset to
zero.e.g. 65535 + 1 will become 0, 65535 + 2 will become 1 and so on...You may try the following
program:
#include <iostream.h>
#include <conio.h>
int
main()
int
val = 65535;
val = val + 1;
Recommended