0% found this document useful (0 votes)
16 views6 pages

C Lab Exercises 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views6 pages

C Lab Exercises 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

PROGRAM 1.

#include <stdio.h>

int main() {

char name[100]; // Declaring a character array to hold the name

printf("Please enter your name: ");

scanf("%99s", name); // Using %99s to avoid buffer overflow

printf("Hello, %s!\n", name);

return 0;

OUTPUT:

Please enter your name: John

Hello, John!

PROGRAM 1.B

#include <stdio.h>

float fahrenheitToCelsius(float fahrenheit)

return (fahrenheit - 32) * 5.0 / 9.0;

float celsiusToFahrenheit(float celsius)

return (celsius * 9.0 / 5.0) + 32;

int main()

int choice;

float temperature, convertedTemperature;

printf("Temperature Conversion Menu:\n");


printf("1. Convert Fahrenheit to Celsius\n");

printf("2. Convert Celsius to Fahrenheit\n");

printf("Enter your choice (1 or 2): ");

scanf("%d", &choice);

switch(choice)

case 1:

printf("Enter temperature in Fahrenheit: ");

scanf("%f", &temperature);

convertedTemperature = fahrenheitToCelsius(temperature);

printf("%.2f Fahrenheit is %.2f Celsius\n", temperature, convertedTemperature);

break;

case 2:

printf("Enter temperature in Celsius: ");

scanf("%f", &temperature);

convertedTemperature = celsiusToFahrenheit(temperature);

printf("%.2f Celsius is %.2f Fahrenheit\n", temperature, convertedTemperature);

break;

default:

printf("Invalid choice. Please enter 1 or 2.\n");

return 0;

OUTPUT:

Temperature Conversion Menu:

1. Convert Fahrenheit to Celsius

2. Convert Celsius to Fahrenheit

Enter your choice (1 or 2): 1


Enter temperature in Fahrenheit: 98.6

98.60 Fahrenheit is 37.00 Celsius

Temperature Conversion Menu:

1. Convert Fahrenheit to Celsius

2. Convert Celsius to Fahrenheit

Enter your choice (1 or 2): 2

Enter temperature in Celsius: 37

37.00 Celsius is 98.60 Fahrenheit

PROGRAM 1.C

#include <stdio.h>

int main()

int num1, num2, num3, largest;

printf("Enter three numbers: ");

scanf("%d %d %d", &num1, &num2, &num3);

if (num1 >= num2 && num1 >= num3) {

largest = num1;

else if (num2 >= num1 && num2 >= num3)

largest = num2;

else

largest = num3;

}
printf("The largest number is: %d\n", largest);

return 0;

OUTPUT:

Enter three numbers: 10 5 7

The largest number is: 10

PROGRAM 1.D:

#include <stdio.h>

#include <math.h>

float areaRectangle(float length, float width)

return length * width;

float areaCircle(float radius)

return M_PI * radius * radius;

float areaCylinder(float radius, float height)

return 2 * M_PI * radius * (radius + height);

int main()

int choice;

float length, width, radius, height, area;

printf("Area Calculation Menu:\n");

printf("1. Calculate area of a rectangle\n");


printf("2. Calculate area of a circle\n");

printf("3. Calculate surface area of a cylinder\n");

printf("Enter your choice (1, 2, or 3): ");

scanf("%d", &choice);

switch(choice)

case 1:

printf("Enter length and width of the rectangle: ");

scanf("%f %f", &length, &width);

area = areaRectangle(length, width);

printf("The area of the rectangle is: %.2f\n", area);

break;

case 2:

printf("Enter the radius of the circle: ");

scanf("%f", &radius);

area = areaCircle(radius);

printf("The area of the circle is: %.2f\n", area);

break;

case 3:

printf("Enter the radius and height of the cylinder: ");

scanf("%f %f", &radius, &height);

area = areaCylinder(radius, height);

printf("The surface area of the cylinder is: %.2f\n", area);

break;

default:

printf("Invalid choice. Please enter 1, 2, or 3.\n");

}
return 0;

OUTPUT:

Area Calculation Menu:

1. Calculate area of a rectangle

2. Calculate area of a circle

3. Calculate surface area of a cylinder

Enter your choice (1, 2, or 3): 1

Enter length and width of the rectangle: 5 10

The area of the rectangle is: 50.00

Area Calculation Menu:

1. Calculate area of a rectangle

2. Calculate area of a circle

3. Calculate surface area of a cylinder

Enter your choice (1, 2, or 3): 2

Enter the radius of the circle: 7

The area of the circle is: 153.94

Area Calculation Menu:

1. Calculate area of a rectangle

2. Calculate area of a circle

3. Calculate surface area of a cylinder

Enter your choice (1, 2, or 3): 3

Enter the radius and height of the cylinder: 3 10

The surface area of the cylinder is: 245.04

You might also like