Fundamentals of Programming Assignment 2
Fundamentals of Programming Assignment 2
Assignment 2
Question 1:
Given the equation 𝑦 = 𝑎𝑥3 + 7, which of the following, if any, are correct C statements for this equation?
1. 𝑦 = 𝑎 ∗ 𝑥 ∗ 𝑥 ∗ 𝑥 + 7;
This statement is correct according to C operational precedence.
2. 𝑦 = 𝑎 ∗ 𝑥 ∗ 𝑥 ∗ (𝑥 + 7);
This statement is incorrect according to C operational precedence.
3. 𝑦 = (𝑎 ∗ 𝑥) ∗ 𝑥 ∗ (𝑥 + 7);
This statement is incorrect according to C operational precedence.
4. 𝑦 = (𝑎 ∗ 𝑥) ∗ 𝑥 ∗ 𝑥 + 7;
This statement is correct according to C operational precedence.
5. 𝑦 = 𝑎 ∗ (𝑥 ∗ 𝑥 ∗ 𝑥) + 7;
This statement is correct according to C operational precedence.
6. 𝑦 = 𝑎 ∗ 𝑥 ∗ (𝑥 ∗ 𝑥 + 7);
This statement is incorrect according to C operational precedence.
Question 2:
State the order of evaluation of the operators in each of the following C statements and show the value
of 𝑥 after each statement is performed.
i. 𝑥 = 7 + 3 ∗ 6 ⁄ 2 − 1;
1) 𝑥 = 7 + (3 ∗ 6) ⁄2 − 1;
2) 𝑥 = 7 + ((3 ∗ 6) ⁄2) − 1;
3) 𝑥 = (7 + ((3 ∗ 6) ⁄2)) − 1;
4) (𝑥 = (7 + ((3 ∗ 6) ⁄2))– 1);
Total = 15
ii. 𝑥 = 2 % 2 + 2 ∗ 2 – 2 ⁄ 2;
1) 𝑥 = 2 % 2 + (2 ∗ 2) – 2⁄2;
2) 𝑥 = 2 % 2 + (2 ∗ 2) – (2⁄2);
3) 𝑥 = (2 % 2) + (2 ∗ 2) – (2⁄2);
4) 𝑥 = ((2 % 2) + (2 ∗ 2)) – (2⁄2);
5) (𝑥 = ((2 % 2) + (2 ∗ 2)) – (2⁄2));
1
Total = 3
iii. 𝑥 = 3 ∗ 9 ∗ 3 + 9 ∗ 3 ⁄ 3;
1) 𝑥 = (3 ∗ 9) ∗ 3 + 9 ∗ 3 ⁄ 3;
2) 𝑥 = ((3 ∗ 9) ∗ 3) + 9 ∗ 3 ⁄ 3;
3) 𝑥 = ((3 ∗ 9) ∗ 3) + (9 ∗ 3) ⁄ 3;
4) 𝑥 = ((3 ∗ 9) ∗ 3) + ((9 ∗ 3) ⁄ 3);
5) (𝑥 = ((3 ∗ 9) ∗ 3) + ((9 ∗ 3) ⁄ 3));
Total = 90
Question 3:
Write a program that asks the user to enter two numbers, obtains them from the user and prints their
sum, product, difference, quotient, and remainder.
#include <stdio.h>
#include <math.h>
int main()
{
//Declaring Variables
int x, y, sum, product, difference, quotient, remainder;
return 0;
2
}
Question 4:
Write a program that reads an integer and determines and prints whether it is odd or even. [Hint: Use
the remainder operator. An even number is a multiple of two. Any multiple of two leaves a remainder of
zero when divided by 2.]
#include <stdio.h>
int main()
{
//Declaring Variables
int x, number;
3
{
printf("Number is odd : %d\n", x % 2);
}
{
}
return 0;
4
Question 5:
Write a program that calculates the squares and cubes of the numbers from 0 to 10 and
#include <stdio.h>
int main()
{
//Declaring Variables
5
int x;
//Printing columns
printf("Number\tSquare\tCube\t\n");
printf("======================\n");
return 0;