Tutorial 8 - Type Casting, Command Line Arguments and Defining Constants & C Operators

💡Question 2 

Write a program to evaluate the polynomial shown here:
3x3 - 5x2 + 6 for x = 2.55

  Answer

#include <stdio.h>

int main() {

    double x = 2.55;

    double result;

        result = 3 * x * x * x - 5 * x * x + 6;

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

    return 0;

}

💡Question 3 

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)

Answer

#include <stdio.h>

int main() {
    double numerator = 3.31e-8 * 2.01e-7;
    double denominator = 7.16e-6 + 2.01e-8;
    double result = numerator / denominator;

    printf("Result: %.2e\n", result);

    return 0;
}

💡Question 4

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
Write a program to find the next largest even multiple for the following values of i and j:

i                j
365          7
12258      23
996          4

Answer

#include <stdio.h>

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

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

    next_multiple = i + j - i % j;

    printf("Next largest even multiple for i = %d and j = %d is: %d\n", i, j, next_multiple);

    return 0;
}

💡Question 5

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

Answer

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

int main() {
    double radius, volume;
    const double pi = 3.14159265359;

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

    volume = (4.0 / 3.0) * pi * pow(radius, 3);

    printf("The volume of the sphere with radius %.2f is: %.2f\n", radius, volume);

    return 0;
}

💡Question 6

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.

Answer

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

#define PI 3.14159265359

float calculate_water_level_height(int num_cubes, float radius, float width, float length, float height) {
    float volume_per_cube = (4.0 / 3.0) * PI * pow(radius, 3);
    float total_volume = num_cubes * volume_per_cube;
    float base_area = width * length;
    float water_level_height = total_volume / base_area;
    return water_level_height;
}

int main() {
    int num_cubes;
    float radius, width, length, height;

    printf("Enter the number of ice cubes: ");
    scanf("%d", &num_cubes);
    printf("Enter the radius of each ice cube (in meters): ");
    scanf("%f", &radius);
    printf("Enter the width of the container (in meters): ");
    scanf("%f", &width);
    printf("Enter the length of the container (in meters): ");
    scanf("%f", &length);
    printf("Enter the height of the container (in meters): ");
    scanf("%f", &height);

    float water_level_height = calculate_water_level_height(num_cubes, radius, width, length, height);

    printf("Height of water level when ice melts: %.2f meters\n", water_level_height);

    return 0;
}

💡Question 7

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

Answer

#include <stdio.h>

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

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

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

    final_result = midterm_marks + 0.70 * final_marks;

    printf("Final result: %.2f\n", final_result);

    return 0;
}

💡Question 8

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”

Answer

#include <stdio.h>

int main(int argc, char *argv[]) {
    if (argc != 3) {
        printf("Usage: %s <source file> <destination file>\n", argv[0]);
        return 1;
    }

    char *source_file = argv[1];
    char *destination_file = argv[2];

    printf("Copy the details in %s to %s at 23.59.59\n", source_file, destination_file);

    return 0;
}

💡Question 9

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.

Answer

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

#define CASH_FLOW 100

float compute_total_commission(float total_sale, float target_rate) {
    float commission = (total_sale * 0.2 * target_rate) + CASH_FLOW;
    return commission;
}

int main(int argc, char *argv[]) {
    if (argc != 2) {
        printf("Usage: %s <target rate>\n", argv[0]);
        return 1;
    }

    float target_rate = atof(argv[1]);

    float total_sale = 1000;

    float total_commission = compute_total_commission(total_sale, target_rate);

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

    return 0;
}











Post a Comment

Previous Post Next Post