Tutorial 7

👉

1.Write a program that subtracts the value 15 from 87 and displays the result, together with an appropriate message, at the terminal.

#include <stdio.h>
int main(void)
{
int sum;

/* COMPUTE RESULT */
sum = 87 - 15;

/* DISPLAY RESULTS */
printf ("The answer is %i \n", sum);

return 0;
}


This C program calculates the result of subtracting 15 from 87 and then displays the result as "The answer is 72" using the `printf` function.

Output:
The answer is 72

👉
2.Identify the syntactic errors in the following program. Then type in and run the corrected program to ensure you have correctly identified all the mistakes.

#include <stdio.h>

int main (Void)

(

INT sum;

/* COMPUTE RESULT

sum = 25 + 37 – 19

/* DISPLAY RESULTS //

printf ("The answer is %i\n" sum);

return 0;

}

  1. Void should be lowercase void.
  2. Parentheses should be used instead of curly braces for the function body.
  3. INT should be lowercase int.
  4. The calculation statement is missing a semicolon at the end.
  5. The comment delimiter // is incorrect; it should be /* and */.
  6. There is a missing comma , between the string and variable in the printf statement.
Corrected code

#include <stdio.h>
int main(void)
{
    int sum;
/* COMPUTE RESULT */
    sum = 25 + 37 - 19;
/* DISPLAY RESULTS */
    printf("The answer is %i\n", sum);
return 0;
}

👉
4.What output would you expect from the following program?

#include <stdio.h>
int main (void)
{
    printf ("Testing...");
    printf ("....1");
    printf ("...2");
    printf ("..3");
    printf ("\n");

return 0;
}


OUTPUT =>

Testing.......1...2..3


This C program defines a main() function that sequentially prints out the strings "Testing...", "....1", "...2", and "..3" each on a separate line using the printf() function. It concludes with a newline character. The program then returns 0, indicating successful execution to the operating system.


👉
5.Which of the following are invalid constants? Why?

1. `123.456`: Valid decimal floating-point number.
2. `0x10.5`: Invalid due to the '.' in a hexadecimal literal.
3. `0X0G1`: Invalid due to 'G' in a hexadecimal literal.
4. `0001`: Valid octal integer.
5. `0xFFFF`: Valid hexadecimal integer.
6. `123L`: Valid decimal long integer.
7. `0Xab05`: Valid hexadecimal integer.
8. `0L`: Valid decimal long integer.
9. `-597.25`: Valid decimal floating-point number.
10. `123.5e2`: Valid decimal floating-point number in scientific notation.
11. `.0001`: Valid decimal floating-point number.
12. `+12`: Valid decimal integer.
13. `98.6F`: Invalid suffix 'F' for a numeric literal.
14. `98.7U`: Invalid suffix 'U' for a numeric literal.
15. `17777s`: Invalid suffix 's' for a numeric literal.
16. `0996`: Valid octal integer but may be interpreted differently due to the leading '0'.
17. `-12E-12`: Valid decimal floating-point number in scientific notation.
18. `07777`: Valid octal integer.
19. `1234uL`: Invalid due to improper combination of 'u' and 'L'.
20. `1.2Fe-7`: Invalid due to unrecognized suffix 'Fe'.
21. `15,000`: Invalid due to the comma within the number.
22. `1.234L`: Valid decimal long floating-point number.
23. `197u`: Invalid due to incomplete syntax.
24. `100U`: Invalid due to incomplete syntax.
25. `0XABCDEFL`: Valid hexadecimal long integer.
26. `0xabcu`: Invalid due to incomplete syntax.
27. `+123`: Valid decimal integer.


👉
6.What output would you expect from the following program?

#include <stdio.h>
int main (void)
{
char c, d;
c = 'd';
d = c;
printf ("d = %c\n", d);
return 0;
}

Output:

d = d

Explanation:
- The character variable `c` is assigned the character `'d'`.
- Then, the value of `c` is assigned to the character variable `d`.
- Finally, the program prints the value of `d`, which is `'d'`.

👉
7.Convert given value in Meter to centimeter.

#include <stdio.h>
int main()
{
    double meters, centimeters;
    /* Input length in meters*/
    printf("Enter length in meters: ");
    scanf("%lf", &meters);
    /* Convert meters to centimeters*/
    centimeters = meters * 100;
    /* Display the result*/
    printf("%.2f meters is equal to %.2f centimeters.\n", meters, centimeters);
return 0;
}
This C program prompts the user to input a length in meters, reads the input, then converts the length to centimeters by multiplying it by 100 (since 1 meter equals 100 centimeters). Finally, it displays the converted length in centimeters with two decimal places.

👉
8.Calculate the volume of a cylinder. PI * r2 h

#include <stdio.h>
#define PI 3.14159
int main() 
{
float radius, height, volume;

/* Input radius and height from the user*/
    printf("Enter the radius of the cylinder: ");
    scanf("%f", &radius);
    printf("Enter the height of the cylinder: ");
    scanf("%f", &height);

/* Calculate volume*/
    volume = PI * radius * radius * height;

/* Display the volume*/
    printf("The volume of the cylinder is: %.2f cubic units\n", volume);
return 0;
}


This program prompts the user to enter the radius and height of the cylinder, then calculates and displays the volume. Make sure to save the file with a .c extension (e.g., cylinder_volume.c) before compiling and running it.


👉
9.Calculate average marks of 4 subjects which, entered separately.


#include <stdio.h>

int main() {
float subject1, subject2, subject3, subject4, average;

    /* Input marks of each subject from the user*/
    printf("Enter marks of subject 1: ");
    scanf("%f", &subject1);
    printf("Enter marks of subject 2: ");
    scanf("%f", &subject2);
    printf("Enter marks of subject 3: ");
    scanf("%f", &subject3);
    printf("Enter marks of subject 4: ");
    scanf("%f", &subject4);

/* Calculate average marks*/
    average = (subject1 + subject2 + subject3 + subject4) / 4;

/* Display the average marks*/
    printf("The average marks of the four subjects is: %.2f\n", average);

return 0;
}


This program prompts the user to enter the marks of each of the four subjects separately, calculates the average, and then displays the result. Compile and run this program in a C compiler like Quincy or any other compiler of your choice.


👉
10.Convert the given temperature in Celsius to Fahrenheit. T(°F) = T(°C) × 1.8 + 32

#include <stdio.h>

int main() {
float celsius, fahrenheit;

/* Input temperature in Celsius from the user*/
    printf("Enter temperature in Celsius: ");
    scanf("%f", &celsius);

/* Convert Celsius to Fahrenheit*/
    fahrenheit = celsius * 1.8 + 32;

/* Display the temperature in Fahrenheit*/
    printf("%.2f Celsius is equal to %.2f Fahrenheit\n", celsius, fahrenheit);

return 0;
}



This program prompts the user to enter a temperature in Celsius, then converts it to Fahrenheit using the formula T(°F) = T(°C) × 1.8 + 32, and finally displays the result. Compile and run this program in a C compiler.


👉
11.Find the value of y using y = 3.5x+5 at x = 5.23.

#include <stdio.h>


int main() {
float x, y;

/* Given value of x*/
    x = 5.23;

/*Calculate y using the equation y = 3.5x + 5*/
    y = 3.5 * x + 5;

/*Display the result*/
    printf("When x = %.2f, the value of y is %.2f\n", x, y);

return 0;
}

This C program computes the value of ( y ) using the equation ( y = 3.5x + 5 ), with ( x ) assigned the value of 5.23. It then utilizes this value of ( x ) to calculate ( y ) and outputs the result. This simple yet effective code demonstrates the straightforward implementation of mathematical expressions in C, enabling users to swiftly determine ( y ) given ( x ) in the specified equation.

👉
12.Find the cost of 5 items if the unit price is 10.50 Rupees.

#include <stdio.h>
int main() 
{
    float unit_price = 10.50;
    int num_items = 5;
    float cost;

/* Calculate the cost*/
    cost = unit_price * num_items;

/* Display the result*/
    printf("The cost of %d items is %.2f Rupees\n", num_items, cost);

return 0;
}


This C program computes the cost of 5 items when each item's unit price is 10.50 Rupees. It multiplies the unit price by the number of items to calculate the total cost and then displays the result. By providing a simple and concise implementation, this program demonstrates the straightforward computation of total costs based on unit prices and quantities, exemplifying fundamental arithmetic operations in C programming.

👉
13.Enter the name, height, weight and gender of a person and calculate his/her BMI in Kg. BMI = weight/ height2

#include <stdio.h>
int main()
{
char name[50], gender;
float height, weight, bmi;

/* Input data from user */
    printf("Enter name: ");
    scanf("%s", name);
    printf("Enter height in meters: ");
    scanf("%f", &height);
    printf("Enter weight in kilograms: ");
    scanf("%f", &weight);
    printf("Enter gender (M/F): ");
    scanf(" %c", &gender); 

/* Calculate BMI*/
    bmi = weight / (height * height);

/* Display the result*/
    printf("\n%s's BMI: %.2f\n", name, bmi);

return 0;
}

This C program prompts the user to input a person's name, height (in meters), weight (in kilograms), and gender (M/F). It then calculates the Body Mass Index (BMI) using the formula BMI = weight / (height * height) and displays the result. By incorporating user input and basic arithmetic operations, the program swiftly computes the BMI, offering a practical tool for assessing an individual's health status based on their weight and height.


👉
14.Write a program that converts inches to centimeters. For example, if the user enters 16.9 for a Length in inches, the output would be 42.926cm. (Hint: 1 inch = 2.54 centimeters.)

#include <stdio.h>
int main()
{
float inches, centimeters;

/*Input length in inches from the user*/
    printf("Enter length in inches: ");
    scanf("%f", &inches);

/*Convert inches to centimeters*/
    centimeters = inches * 2.54;

/* Display the result */
    printf("%.2f inches is equal to %.3f centimeters\n", inches, centimeters);

return 0;
}

This C program efficiently converts a length in inches inputted by the user into centimeters using the conversion factor of 1 inch equal to 2.54 centimeters. By taking user input, performing a simple multiplication based on the conversion factor, and then displaying the converted length in centimeters, the program offers a straightforward and practical tool for length conversion, facilitating quick and accurate calculations in a single execution.


👉
15.The figure gives a rough sketch of a running track. It includes a rectangular shape and two semi-circles. The length of the rectangular part is 67m and breadth is 21m.Calculate the distance of the running track.

 



#include <stdio.h>
#define PI 3.142
int main() 
{
float distance_rectangular, distance_semi_circles, total_distance;
float length_rectangular = 67.0; 
float breadth_rectangular = 21.0; 

distance_rectangular = 2 * length_rectangular ;
distance_semi_circles = 2 * PI * (breadth_rectangular/2);

total_distance = distance_rectangular +distance_semi_circles;

/* Display the total distance*/
printf("The distance of the running track is %.2f meters\n", total_distance);

return 0;
}


The provided code calculates the total distance of a running track composed of a rectangular part and two semicircular ends. It first initializes variables for the length and breadth of the rectangular part. Then, it computes the distances of the rectangular part and the semicircular ends separately. The distance of the rectangular part is calculated by doubling the length, representing the perimeter of the rectangle. The distance of the semicircular ends is calculated using the formula for the circumference of a circle, where the radius is half the breadth of the track. Finally, the total distance is obtained by summing the distances of the rectangular part and the semicircular ends. The result is printed out with two decimal places, indicating the total distance of the running track in meters.

Comments

Popular posts from this blog

Introduction To C

First C Program

Compilation process in c