C++ Assighnment 2
C++ Assighnment 2
Prefix
and Postfix increment operators.
Ans] arithmetic operator involves the computation of numbers. results always comes as a number.
Basic arithmetic operators are +,-,*,/ and %.
Whereas relational operator involves the comparison of two operands. Results always comes
as a Boolean value i.e. true or false. Basic relational operators are ==,!=,<,>= and >=.
Ans]
#include <iostream>
int main()
double tc,tf;
cin>>tc;
tf=(tc*1.8)+32;
return 0;
3.3 Write a program in C++ to calculate simple interest. You are given the values of principal, rate
and time.
Ans]
#include <iostream>
int main()
double p,r,t,si;
cin>>p;
cin>>t;
si= (p*r*t)/100.0;
return 0;
3.4] True/false
Ans] a. true
b. true
c. false
a. b2 -4ac
b. x = 1-x2 /2+x4/4-x6 /6
Ans] a. (b*b)-(4*a*c)
b. x = (1-(x*x))/(2+(x*x*x*x))/(4-(x*x*x*x*x*x))/6
#include <iostream>
int main()
int i=0;
i=400*400/400;
cout<<i;
return 0;
}
Ans] 400
Ans] a. integral data type store a whole number without decimal point. Range is -32768 to +32767.
Whereas floating point data type stores a numbwe which has decimal point in it. Range is 10-38 to
1038.
b. range of double data type uses 8 bytes tp store numbers and its range is -1.7 * 10-308 to 1.7 * 10308.
Whereas long double data type uses 10 bytes to store numbers and its range is -3.4 * 10-4932 to 3.4 *
104932.
c. range of int/signed int data type uses 2 bytes tp store numbers and its range is -32768 to 32767.
Whereas unsigned int data type uses 2 bytes to store numbers and its range is 0 to 65535.
d. character type data stores single alphanumeric character. Whereas string data type stores group
of characters of any length.
4.2] Is it true that an unsigned int is twice as large as the signed int? Why?
Ans] False. Both can store 65536 different values, but signed integers use half of their range
for negative numbers, whereas unsigned integers can store positive numbers that are
twice as large.
Ans] void: The type void either explicitly declares a function as returning no value or creates generic
pointers. It also indicates that a function contains an empty argument list. It was introduced in ANSI
C. void function-name (void);
void *ptr;
Here ptr has become a generic pointer. A generic pointer can be assigned a pointer value of any
basic data type, but it may not be dereferenced.
ptr=a;
But *a=*ptr;
is illegal as it would not make sense to dereference an integer pointer to a void value.
Ans] A modifier is used to alter the meaning of the base type to fit various situations more precisely.
The list of modifiers is: signed, unsigned, long and short.
#include <iostream>
int main ()
cout<<7+ 7/ 7.0;
return (0);
Ans] 8
5.1 Write a program in C++ to convert temperature form Fahrenheit to Celsius using type casting of
integer variable to float.
Ans]
#include <iostream>
int main()
float tc,tf;
cin>>tf;
tc=(float)5/9*(tf-32);
return 0;
by another integer.
increased.
Ans] a. false
b. true
c. false
#include <iostream>
int main ()
float f;
f=5/2*float(7)/(int)3.5;
cout<<f;
return (0);
Ans] 4.66667
5.4 Sometimes when an integer value is moved to a character variable and then that character value
is moved back to the integer variable, we get negative value. Why?
Ans] Character data type has range (-128,127). So if you assign any int value outside this to character
and then store it again in an int value ,negative number will be assighned.
5.5 What will be displayed when the following code is executed?
#include <iostream>
int main ()
float f1,f2;
f1=j/i;
f2=float(j)/i;
cout<<f1<<f2;
return 0;
Ans]22.5