0% found this document useful (0 votes)
11 views6 pages

C++ Assighnment 2

The document explains the differences between arithmetic and relational operators, as well as prefix and postfix increment operators. It includes C++ programs for converting temperatures and calculating simple interest, along with true/false questions regarding data types and operators. Additionally, it discusses the use of void data types, modifiers on basic data types, and the implications of type casting.

Uploaded by

Sahil
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)
11 views6 pages

C++ Assighnment 2

The document explains the differences between arithmetic and relational operators, as well as prefix and postfix increment operators. It includes C++ programs for converting temperatures and calculating simple interest, along with true/false questions regarding data types and operators. Additionally, it discusses the use of void data types, modifiers on basic data types, and the implications of type casting.

Uploaded by

Sahil
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/ 6

3.1] Differentiate between the following: b. Arithmetic operators and Relational operators c.

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 >=.

3.2] Write a program in C++ to convert temperature form Celsius to Fahrenheit.

Ans]

#include <iostream>

using namespace std;

int main()

double tc,tf;

cout<<"enter the temperature in celsius"<<endl;

cin>>tc;

tf=(tc*1.8)+32;

cout<<"temperature after conversion is:"<<tc<<" degree fahrenite"<<endl;

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>

using namespace std;

int main()

double p,r,t,si;

cout<<"enter the principle"<<endl;

cin>>p;

cout<<"enter the rate"<<endl;


cin>>r;

cout<<"enter the time"<<endl;

cin>>t;

si= (p*r*t)/100.0;

cout << "simple interest is :" <<si<< endl;

return 0;

3.4] True/false

a. Modulus operator can be applied on floating-point numbers.

b. Relational operators are used for comparisons.

c. The arithmetical operators * and + have same precedence.

Ans] a. true

b. true

c. false

3.5 Write the C++ equivalent expression for the following:

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

3.6 What will be displayed when the following code is executed?

#include <iostream>

using namespace std;

int main()

int i=0;

i=400*400/400;

cout<<i;

return 0;
}

Ans] 400

4.1 Differentiate between the following:

a. integral type data and floating-point data

b. double and long double

c. int, signed int and unsigned int

d. character type data and string type data.

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.

4.3] Can we assign a void pointer to an int type pointer? Why?

Ans] NO. it gives compiler error.

4.4] Write a short note on void data type.

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.

For example: int *a;

ptr=a;

here an int pointer is assigned to void pointer.

But *a=*ptr;

is illegal as it would not make sense to dereference an integer pointer to a void value.

4.5] What is the use of modifiers on basic data types?

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.

4.6] What will be the output of the following program:

#include <iostream>

using namespace std;

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>

using namespace std;

int main()

float tc,tf;

cout<<"enter the temperature in fahrenite"<<endl;

cin>>tf;
tc=(float)5/9*(tf-32);

cout<<"temperature after conversion is:"<<tc<<" degree celsius"<<endl;

return 0;

5.2] State true/false

a.) In an assignment expression, the type casting of variable on the left

hand side can be done.

b.) There is never a loss of information when an integer variable is divided

by another integer.

c.) By type casting an expression, the accuracy of the result can be

increased.
Ans] a. false

b. true

c. false

5.3 What will be the output of the following program?

#include <iostream>

using namespace std;

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>

using namespace std;

int main ()

int i=2, j=5;

float f1,f2;

f1=j/i;

f2=float(j)/i;

cout<<f1<<f2;

return 0;

Ans]22.5

You might also like