Week 2 Practice Questions

Note: Try to solve these questions on your own before checking the solutions.

Filter:
Sort:

Exercise 1: Calculate Y

Write a function named GetY(), which receives the value of X and computes and returns the value of Y, as follows:

\[Y = X^2 + 3\sqrt{X} + b\]

Where b is a constant value defined in the program (let b = 7).

In main(), you need to input the value for X and pass it to the function, then receive the value and print it out to the user.

Example:

Enter value for X: 4
The value of Y is: 29.00

(Calculation: \(16 + 3(2) + 7 = 29\))

Solution
#include <stdio.h>
#include <math.h>

double GetY(double x) {
    int b = 7;
    double y = (x * x) + (3 * sqrt(x)) + b;
    return y;
}

int main() {
    double X, result;

    printf("Enter value for X: ");
    scanf("%lf", &X);

    result = GetY(X);

    printf("The value of Y is: %.2lf\n", result);

    return 0;
}

                        
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

double GetY(double x) {
    int b = 7;
    double y = (x * x) + (3 * sqrt(x)) + b;
    return y;
}

int main() {
    double X, result;

    cout << "Enter value for X: ";
    cin >> X;

    result = GetY(X);

    cout << "The value of Y is: " << fixed << setprecision(2) << result << endl;

    return 0;
}

                        

Exercise 2: Circle Properties

Write a function named CircleArea(), which receives the R (Radius) of the circle and computes both the Area and Perimeter of the Circle.

You need to display the values of Area and Perimeter within the same function.

In main(), you need to input the value for R and pass it to the function, then print out the results within the same function.

Example:

Enter the radius (R): 5
Area of the circle: 78.54
Perimeter of the circle: 31.42
Solution
#include <stdio.h>

const double PI = 3.14159;

void CircleArea(double r) {
    double area = PI * r * r;
    double perimeter = 2 * PI * r;

    printf("Area of the circle: %.2lf\n", area);
    printf("Perimeter of the circle: %.2lf\n", perimeter);
}

int main() {
    double radius;

    printf("Enter the radius (R): ");
    scanf("%lf", &radius);

    CircleArea(radius);

    return 0;
}

                        
#include <iostream>
#include <iomanip>

using namespace std;

const double PI = 3.14159;

void CircleArea(double r) {
    double area = PI * r * r;
    double perimeter = 2 * PI * r;

    cout << fixed << setprecision(2);
    cout << "Area of the circle: " << area << endl;
    cout << "Perimeter of the circle: " << perimeter << endl;
}

int main() {
    double radius;

    cout << "Enter the radius (R): ";
    cin >> radius;

    CircleArea(radius);

    return 0;
}

                        

Exercise 3: Calculator Function

Implement a function named Calc(), that receives 2 integer arguments n1, n2 and performs the basic mathematical operations (you need to display the results within the same function):

  • Addition (+)
  • Subtraction (-)
  • Multiplication (*)
  • Division (/)
  • Modulus (%)

In main(), you need to input the value for n1, n2 and pass them to the function, then display the results within the same function.

Note: The solution does not handle division by zero as conditional statements have not been covered yet.

Example:

Enter integer number 1: 10
Enter integer number 2: 5
Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2
Modulation: 0
Solution
#include <stdio.h>

void Calc(int n1, int n2) {
    printf("Addition: %d\n", n1 + n2);
    printf("Subtraction: %d\n", n1 - n2);
    printf("Multiplication: %d\n", n1 * n2);
    printf("Division: %d\n", n1 / n2);
    printf("Modulation: %d\n", n1 % n2);
}

int main() {
    int num1, num2;

    printf("Enter integer number 1: ");
    scanf("%d", &num1);

    printf("Enter integer number 2: ");
    scanf("%d", &num2);

    Calc(num1, num2);

    return 0;
}

                        
#include <iostream>

using namespace std;

void Calc(int n1, int n2) {
    cout << "Addition: " << n1 + n2 << endl;
    cout << "Subtraction: " << n1 - n2 << endl;
    cout << "Multiplication: " << n1 * n2 << endl;
    cout << "Division: " << n1 / n2 << endl;
    cout << "Modulation: " << n1 % n2 << endl;
}

int main() {
    int num1, num2;

    cout << "Enter integer number 1: ";
    cin >> num1;

    cout << "Enter integer number 2: ";
    cin >> num2;

    Calc(num1, num2);

    return 0;
}

                        

Exercise 4: Average of Digits

Implement a function named DigitsNum(), that receives a 3-digit integer argument num and returns the average of its digits.

In main(), you need to input the value for num and pass it to the function, then receive the value and print it out to the user.

Example:

Enter a 3-digit integer: 123
The average of the digits is: 2.00
(Calculation: $$ (1+2+3)/3 = 2 $$)
Solution
#include <stdio.h>

double DigitsNum(int num) {
    int d1, d2, d3;
    
    d1 = num % 10;
    d2 = (num / 10) % 10;
    d3 = num / 100;
    
    double sum = d1 + d2 + d3;
    
    return sum / 3;
}

int main() {
    int number;
    double average;

    printf("Enter a 3-digit integer: ");
    scanf("%d", &number);

    average = DigitsNum(number);

    printf("The average of the digits is: %.2lf\n", average);

    return 0;
}

                        
#include <iostream>
#include <iomanip>

using namespace std;

double DigitsNum(int num) {
    int d1, d2, d3;
    
    d1 = num % 10;
    d2 = (num / 10) % 10;
    d3 = num / 100;
    
    double sum = d1 + d2 + d3;
    
    return sum / 3;
}

int main() {
    int number;
    double average;

    cout << "Enter a 3-digit integer: ";
    cin >> number;

    average = DigitsNum(number);

    cout << "The average of the digits is: " << fixed << setprecision(2) << average << endl;

    return 0;
}

                        

Exercise 5: Currency Converter

Implement a function named CurrencyConvertor(), which receives an amount of money (double value) in JoD (Jordanian Dinar) then converts it to USD, suppose that:

\[1 \text{ JoD} = 1.4112 \text{ USD}\]

Example:

Enter amount in JOD: 100
Amount in USD: 141.1200
Solution
#include <stdio.h>

double CurrencyConvertor(double jod) {
    const double rate = 1.4112;
    return jod * rate;
}

int main() {
    double money_jod, money_usd;

    printf("Enter amount in JOD: ");
    scanf("%lf", &money_jod);

    money_usd = CurrencyConvertor(money_jod);

    printf("Amount in USD: %.4lf\n", money_usd);

    return 0;
}

                        
#include <iostream>
#include <iomanip>

using namespace std;

double CurrencyConvertor(double jod) {
    const double rate = 1.4112;
    return jod * rate;
}

int main() {
    double money_jod, money_usd;

    cout << "Enter amount in JOD: ";
    cin >> money_jod;

    money_usd = CurrencyConvertor(money_jod);

    cout << "Amount in USD: " << fixed << setprecision(4) << money_usd << endl;

    return 0;
}

                        

Exercise 6: Distance in 2D Space

Write a function named Distance2D() that receives the 2D coordinates $(x, y)$ of two points and computes the Euclidean distance between them.

In main(), you need to input the coordinates for both points, pass them to the function, and print the calculated distance to the user.

Example:

Enter coordinates for Point 1 (x1 y1): 1.5 2.0
Enter coordinates for Point 2 (x2 y2): 4.5 6.0
The distance between the points is: 5.00
Solution
#include <stdio.h>
#include <math.h>

double Distance2D(double x1, double y1, double x2, double y2);

int main() {
    double x1, y1, x2, y2;

    printf("Enter coordinates for Point 1 (x1 y1): ");
    scanf("%lf %lf", &x1, &y1);

    printf("Enter coordinates for Point 2 (x2 y2): ");
    scanf("%lf %lf", &x2, &y2);

    double dist = Distance2D(x1, y1, x2, y2);

    printf("The distance between the points is: %.2f\n", dist);

    return 0;
}

double Distance2D(double x1, double y1, double x2, double y2) {
    return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
}

                        
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

double Distance2D(double x1, double y1, double x2, double y2);

int main() {
    double x1, y1, x2, y2;

    cout << "Enter coordinates for Point 1 (x1 y1): ";
    cin >> x1 >> y1;

    cout << "Enter coordinates for Point 2 (x2 y2): ";
    cin >> x2 >> y2;

    double dist = Distance2D(x1, y1, x2, y2);

    cout << "The distance between the points is: " << fixed << setprecision(2) << dist << endl;

    return 0;
}

double Distance2D(double x1, double y1, double x2, double y2) {
    return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
}