In: Computer Science
Please answer urgently its 3 questions
1. If a=20 and b=10, and the types of a and b are int, then what will be the result of a%b?
(b). Suppose x is of type double. Fill in the dotted blank below to print x correct to one decimal place: cout << ............................... ;
2. Fill in the two dotted blank spaces below such that the output of the code will be: 35. int a ........... 7;
a ........... 7 ? cout<<5*a : cout<<4*a;
3. Indicate if each of the following will be represented accurately by a variable of type double by saying ”yes” or ”no” for each: (i) 1.0; (ii) 1/2; (iii) 1/3; (iv) 1/10.
Ques 1)
a) a%b will print 0. '%' is the modulus which prints the remainder when a is divided by b.
When 20 is divided by 10 the remainder is zero, hence the output is 0.
b) cout<<setprecision(2)<<x;
The function 'setprecision()' is a predefined function in the 'iomanip' header file. It is used to set the precision of decimal numbers up to the required number of decimal places.
Ques 2)
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int a = 7;
a == 7? cout<<a*5 : cout<<a*4;
}
In the first blank '=' operator is used to assign a value of a to 7.
In the second blank '==' operator is used to check if the value of a is equal to 7.
'=' is used because 35 is a factor of 5 and 7, so the value of a is assigned as 7.
'==' is used because 7*5 = 35, so to make the condition true, the equal operator is used.
Ques 3)
i) No. By default in C++, if the value after decimal place is zero, then double truncates zeros, so this will be printed as '1'.
ii)No
iii)No
iv) No
The reason for the above is if we directly assign a double variable the value like 1/3 or 1/10, etc. Then it performs integer division which results as 0.
Hence, if we assign double x = 1/3, it will give output as 0.
So none of this number will be printed accurately.
Note:- Please comment down if you face any problem. :)