4: Conditional Statements Question 1.a CS2311 – Yang, QingXiong

advertisement
CS2311 – Yang, QingXiong
4:
Conditional Statements
Question 1.a
Write a program so that it reads in two integer numbers a and b and a character c (which
can be ‘+’,’-’,’*’,’/’ and ‘%’ operators) and produces output as a c b. For instance, if
a=10, b=9 and c=’+’, then perform addition operation and output 19 (which is 10+9).
Question 1.b
Repeat the above question with the use of switch statement.
Question 2
Write a program that prompts the user to enter an integer and determines (a) whether it is
divisible by 5 and 7, (b) whether it is divisible by 5 or 7, (c) whether it is divisible by 5 or
7, but not both. Here is a sample runs of this programs:
Enter an integer: 10
Is 10 divisible by 5 and 7? False
Is 10 divisible by 5 or 7? True
Is 10 divisible by 5 or 7, but not both? True
Question 3: quadratic equation
Write a program to solve the quadratic equation
𝑎𝑥 2 + 𝑏𝑥 + 𝑐 = 0
by the following quadratic formula:
𝑥=
−𝑏 ± √𝑏 2 − 4𝑎𝑐
2𝑎
Please note that 1) 𝒂 cannot be equal to 0; 2) you can use sqrt function (which will
be discussed in Week 7) to compute the square root of a number (e.g., sqrt(16) gives
you 4 and sqrt(4)==2); 3) you need to understand what is a complex number:
https://en.wikipedia.org/wiki/Complex_number. If 𝑏 2 − 4𝑎𝑐 < 0,
𝑥=
−𝑏 ± √−(𝑏 2 − 4𝑎𝑐) 𝑖
2𝑎
CS2311 – Yang, QingXiong
[Sample outputs]
The underline characters are input by the user.
Sample output 1:
What is A? 1
What is B? -5
What is C? -6
The Quadratic Equation has two roots.
The roots are -1.00 and 6.00
Sample output 2:
What is A? 1
What is B? -8
What is C? 16
The Quadratic Equation has one root.
The root is 4.00
Sample output 3:
What is A? 2
What is B? 5
What is C? 8
The Quadratic Equation has no real roots.
The roots are -1.25+1.56i and -1.25-1.56i
Download