In: Computer Science
1. Write the C++ code for a program that calculates how many days are left until Halloween, when given as an input how many weeks are left until Halloween. Use variables named weeks and days.
------------
2. What header file must be included
- To perform mathematical functions like sqrt?
- To use cin and cout?
- To use stream manipulators like setprecision?
--------------------
3. What value will be stored in the variable t after each of the following statements executes?
1. t = (12 > 1);
2. t = (2 < 0);
3. t = (5 == (3 * 2));
4. t = ( 5 == 5);
----------------
4. Convert the following conditional expression into an if/else
statement:
q = (x < y) ? (a+b) : (x * 2);
------------------
5. Write a function named getNumber which uses a reference parameter to accept an integer argument. The function should prompt the user to enter a number in the range of 1 through 100. The input should be validated and stored in the parameter value.
------------------------
6. Write a C++ statement that prints the message “The number is valid.” If the variable temperature is within the range -50 through 150
Code:
#include<iostream>
using namespace std;
int main(){
// declare variables
int weeks,days;
// take input from user number of weeks
cout<<"Enter number of Weeks left until
Halloween : ";
cin>>weeks;
// calculate number of days
days = 7*weeks;
// print the days
cout<<endl<<days<<" days are left
until Halloween";
return 0;
}
Code::
#include<iostream>
using namespace std;
//functin that reads the data from user
void getNumber(int *n){
int x;
cout<<"Enter a number in the range of 1 through
100. : ";
cin>>x;
if(x>=1 || x<=100){
*n = x;
}
}
int main(){
int n;
// call the function getNumber pass the reference of
n
getNumber(&n);
return 0;
}
Code:
#include<iostream>
using namespace std;
int main(){
int temp;
// prompt the user for number
cin>>temp;
// check if the number is in range of -50 to 150
if(temp>=-50 && temp<=150){
cout<<"The number is
valid.";
}
return 0;
}