Fundamentals of Programming
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;
//Taking and storing Input "x" from user
printf("Enter x : ");
scanf_s("%d", &x);
//Taking and storing Input "y" from user
printf("Enter y : ");
scanf_s("%d", &y);
//Calculating sum of "x" and "y"
sum = x + y;
printf("Enter sum : %d\n", sum);
//Calculating product of "x" and "y"
product = x * y;
printf("Enter product : %d\n", product);
//Calculating difference of "x" and "y"
difference = x - y;
printf("Enter difference : %d\n", difference);
//Calculating quotient of "x" and "y"
quotient = x / y;
printf("Enter quotient : %d\n", quotient);
//Calculating remainder of "x" and "y"
remainder = x % y;
printf("Enter remainder : %d\n", 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;
//Input and storage of "x" by user
printf("Enter x: ");
scanf_s("%d", &x);
//Determining of even number and odd number
//"x" is Even if statement is true
number = x % 2;
if (number == 0)
printf("Number is even: %d\n", x % 2);
//"x" is Odd if statement is false
else
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
uses tabs to print the following table of values:
Number Square Cube
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
6 36 216
7 49 343
8 64 512
9 81 729
10 100 1000
#include <stdio.h>
int main()
{
//Declaring Variables
5
int x;
//Printing columns
printf("Number\tSquare\tCube\t\n");
printf("======================\n");
//Input and storage of x
for (x = 1; x <= 10; x++)
printf("%d\t%d\t%d\n” ,x, x*x, x*x*x);
return 0;