Tutorial 8 - 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
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
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
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
0 Comments