Friday, 23 February 2024

Tutorial 08

Tutorial 08

1. Write a program to evaluate the polynomial shows here.

3x 3 - 5x 2 + 6 for x = 2.55

Code
#include <stdio.h>

int main() {
    double x = 2.55;
    double result;

    // Evaluate the polynomial
    result = 3 * x * x * x - 5 * x * x + 6;

    // Print the result
    printf("The result of the polynomial for x = %.2f is: %.2f\n", x, result);

    return 0;
}

Result
The result of the polynomial for x = 2.55 is: 23.23

*In this program, we declare a variable x and initialize it with the value 2.55. Then, we use this value to evaluate the polynomial 3352+6. Finally, we print the result to the console.

2. Write a program that evaluates the following expression and displays the results
(remember to use exponential format to display the result):
(3.31 × 10-8 × 2.01 × 10-7) / (7.16 × 10-6 + 2.01 × 10-8)  

Code
#include <stdio.h>

int main() {
    double numerator, denominator, result;

    // Evaluate the numerator and denominator separately
    numerator = (3.31e-8) * (2.01e-7);
    denominator = (7.16e-6) + (2.01e-8);

    // Check if the denominator is not zero
    if (denominator != 0) {
        // Evaluate the result
        result = numerator / denominator;

        // Display the result in exponential format
        printf("Result: %.2e\n", result);
    } else {
        printf("Error: Division by zero.\n");
    }

    return 0;
}

Result
Result: 9.27e-010

*In this program, we first calculate the numerator and denominator separately using the given values in exponential notation. Then, we ensure that the denominator is not zero to avoid division by zero error. If the denominator is nonzero, we proceed to calculate the result by dividing the numerator by the denominator and display the result in exponential format with two decimal places. If the denominator is zero, we display an error message indicating division by zero.

3. To round off an integer i to the next largest even multiple of another integer j, the
following formula can be used:

Next multiple = i + j - i % j

For example, to round off 256 days to the next largest number of days evenly divisible by
a week, values of i = 256 and j = 7 can be substituted into the preceding formula as
follows:

Next multiple = 256 + 7 - 256 % 7
= 256 + 7 - 4
= 259

Write a program to find the next largest even multiple for the following values of i and j:(Use keyboard to input values for i and j)
i                    j
365              7
12258        23
996              4

Code
#include <stdio.h>

int main() {
    int i, j, next_multiple;

    // Input values of i and j from the user
    printf("Enter the value of i: ");
    scanf("%d", &i);

    printf("Enter the value of j: ");
    scanf("%d", &j);

    // Calculate the next largest even multiple using the formula
    next_multiple = i + j - i % j;

    // Print the result
    printf("Next largest even multiple: %d\n", next_multiple);

    return 0;
}

Result
Enter the value of i: 256
Enter the value of j: 7
Next largest even multiple: 259

*In this program, the user is prompted to input the values of
and . Then, the program calculates the next largest even multiple using the provided formula _=+%. Finally, the program displays the result to the user.

4. Write a program to input the radius of a sphere and to calculate the volume of the sphere.
Volume = 4/3*pi*radius3

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

#define PI 3.14

int main() {
    double radius, volume;

    // Input the radius of the sphere
    printf("Enter the radius of the sphere: ");
    scanf("%lf", &radius);

    // Calculate the volume of the sphere
    volume = (4.0/3.0) * PI * pow(radius, 3);

    // Display the volume of the sphere
    printf("Volume of the sphere: %.2f\n", volume);

    return 0;
}

Result
Enter the radius of the sphere: 20
Volume of the sphere: 33493.33

  • We use standard input/output functions for user interaction (stdio.h) and mathematical functions like pow() for calculating the cube of the radius (math.h).

  • We define the value of pi as 3.14

  • The user inputs the radius of the sphere.

  • The program calculates the volume of the sphere using the formula for its volume.

  • Finally, it displays the calculated volume.

  • For a radius of 20, the program computes the volume of the sphere and displays the result.
5. 100 spherical ice (cubes) of a given radius are placed in a box of a given width, length and height. Calculate the height of the water level when the ice melts. Neglect the change in volume when ice converts to water.

Code
#include <stdio.h>

int main() {
    // Declare variables
    double radius, width, length, height, ice_volume, box_volume, water_height;
    int num_ice_cubes;

    // Input radius of the ice cubes
    printf("Enter the radius of the ice cubes: ");
    scanf("%lf", &radius);

    // Input dimensions of the box
    printf("Enter the width, length, and height of the box: ");
    scanf("%lf %lf %lf", &width, &length, &height);

    // Input number of ice cubes
    printf("Enter the number of ice cubes: ");
    scanf("%d", &num_ice_cubes);

    // Calculate the volume of one ice cube
    ice_volume = (4.0/3.0) * 3.14 * radius * radius * radius;

    // Calculate the total volume of all ice cubes
    ice_volume *= num_ice_cubes;

    // Calculate the volume of the box
    box_volume = width * length * height;

    // Calculate the volume occupied by water when ice melts
    water_height = (box_volume - ice_volume) / (width * length);

    // Display the height of the water level
    printf("Height of the water level when ice melts: %.2f\n", water_height);

    return 0;
}

Result
Enter the radius of the ice cubes: 30
Enter the width, length, and height of the box: 15,20,30
Enter the number of ice cubes: Height of the water level when ice melts: -1.#J

*This program takes input for the radius of the ice cubes, dimensions of the box (width, length, and height), and the number of ice cubes. Then, it calculates the height of the water level when the ice cubes melt, neglecting the change in volume when ice converts to water.

6. Write a program to input your mid term marks of a subject marked out of 30 and the final exam marks out of 100. Calculate and print the final results.
Final Result = Mid Term + 70% of Final Mark  

Code
#include <stdio.h>

int main() {
    int midterm_marks;
    float final_marks, final_result;

    // Input midterm marks
    printf("Enter your midterm marks (out of 30): ");
    scanf("%d", &midterm_marks);

    // Input final exam marks
    printf("Enter your final exam marks (out of 100): ");
    scanf("%f", &final_marks);

    // Calculate final result
    final_result = midterm_marks + (0.70 * final_marks);

    // Print final result
    printf("Your final result is: %.2f\n", final_result);

    return 0;
}

Result
Enter your midterm marks (out of 30): 28
Enter your final exam marks (out of 100): 76
Your final result is: 81.20

  • *In this program:
We use scanf to input the midterm marks and the final exam marks.
We calculate the final result by adding the midterm marks to 70% of the final exam marks.
Finally, we print the final result.
  • 7. Input the source file and destination file name as command line arguments and print the following message to standard output:
    “Copy the details in <file name 1> to <file name 2> at 23.59.59”
Code
#include <stdio.h> int main(int argc, char *argv[]) { // Check if the number of command-line arguments is correct if (argc != 3) { printf("Usage: %s <source_file> <destination_file>\n", argv[0]); return 1; } // Extract the file names from command-line arguments char *source_file = argv[1]; char *destination_file = argv[2]; // Print the message to standard output printf("Copy the details in %s to %s at 23.59.59\n", source_file, destination_file); return 0; }

Result
Usage: C:\Users\MY PC\Desktop\Text1.exe <source_file> <destination_file>

*This code checks if there are exactly three command-line arguments (including the program name itself). If the number of arguments is not correct, it prints a usage message. If the number of arguments is correct, it extracts the source and destination file names from the command-line arguments and prints the specified message to standard output.

8. Input the target rate as command line argument and compute the total commission as follows:
Total commission = ( total sale × 0.2 × target rate ) + cash flow.
Cash flow is a constant.

Code
#include <stdio.h>
#include <stdlib.h>

#define CASH_FLOW 1000 // Define the constant cash flow

int main(int argc, char *argv[]) {
    // Check if the correct number of command-line arguments is provided
    if (argc != 2) {
        printf("Usage: %s <target_rate>\n", argv[0]);
        return 1;
    }

    // Convert the command-line argument (target_rate) to a float
    float target_rate = atof(argv[1]);

    // Check if the target rate is a positive number
    if (target_rate <= 0) {
        printf("Error: Target rate must be a positive number.\n");
        return 1;
    }

    // Example value for total sale (you can change it as needed)
    float total_sale = 5000;

    // Calculate the total commission
    float total_commission = (total_sale * 0.2 * target_rate) + CASH_FLOW;

    // Print the total commission
    printf("Total commission: %.2f\n", total_commission);

    return 0;
}

*This code checks if the correct number of command-line arguments is provided. It converts the target rate provided as a command-line argument to a float and checks if it's a positive number. Then it calculates the total commission using the given formula and prints the result.

No comments:

Post a Comment

Tutorial 12

Functions  Question 01 Display all prime numbers between two Intervals using a function.   C Code                #include <stdio.h>   ...