0% found this document useful (0 votes)
10 views5 pages

C Programming Loboratory Exp 1 7

The document outlines a series of laboratory exercises for learning C programming, covering topics such as basic syntax, data types, input/output operations, and mathematical calculations. It includes specific tasks like printing variable sizes, using printf and scanf functions, calculating mechanical energy, and converting units. Each task comes with expected outputs and algorithms for implementation.

Uploaded by

sree302005
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)
10 views5 pages

C Programming Loboratory Exp 1 7

The document outlines a series of laboratory exercises for learning C programming, covering topics such as basic syntax, data types, input/output operations, and mathematical calculations. It includes specific tasks like printing variable sizes, using printf and scanf functions, calculating mechanical energy, and converting units. Each task comes with expected outputs and algorithms for implementation.

Uploaded by

sree302005
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/ 5

C Programming

Laboratory – 1:
1. Explore the Linux command
2. Write a program to print the “Hello World”.
Laboratory -2:
1. Write a c program to print the size of an char, long int, float,
long, short, double and long double
Expected output is:
size of char: 1
size of int: 4
size of float: 4
size of long: 8
size of short: 2
size of double: 8
size of long double: 16
(Note use %lu and sizeof())

2. Write a program to explore the printf for all datatypes and print
the below output to monitor (where, c = 'A', bool b = true, f =
3.14, octal = 010, hex = 0x10, signedint = -10;):

Expected output is:


The c variable contains = A
The b variable contains = 1
The f variable contains = 3.140000
The octal variable contains = 10 (octal), 8 (decimal)
The hex variable contains = 10 (hex), 16 (decimal)
The signedInt variable contains = -10
The unsignedInt variable contains = 10

3. Write a program to print the floating-point numbers.

Expected output:
The float value1 with trailing zeros: 3.14
The float value1 in exponential form (lowercase): 3.141590e+00
The float value1 in exponential form (uppercase): 3.141590E+00
The float value2 with trailing zeros: 0.000123457
The float value2 in exponential form (lowercase): 1.234568e-04
The float value2 in exponential form (uppercase): 1.234568E-04
The float value3 with trailing zeros: 123456792.000000000
The float value3 in exponential form (lowercase): 1.234568e+08
The float value3 in exponential form (uppercase): 1.234568E+08

4. Write a program to explore the scanf for all datatypes and print
the below output to monitor (Assume variale c is charater, b is
bool, f is float, octal is octal, hex is hexadecimal number,
signedInt is a signed interger and unsignedInt is unsigned
integer):

Expected input and output:


Enter a character: A
Enter a boolean (0 for false, any other non-zero value for true): 1
Enter a floating-point number: 3.14
Enter an octal number: 17
Enter a hex number: 1A
Enter a signed integer: -42
Enter an unsigned integer: 123

The c variable contains = A


The b variable contains = 1
The f variable contains = 3.140000
The octal variable contains = 17 (octal), 15 (decimal)
The hex variable contains = 1a (hex), 26 (decimal)
The signedInt variable contains = -42
The unsignedInt variable contains = 123
5. Write a C Program to find Mechanical Energy of a particle using E =
mgh+1/2 mv2.

Algorithm to calculate the mechanical energy of a particle using the


following formula:

E = mgh + (1/2)mv^2

Inputs:
- m : Mass of the particle in kilograms (kg).
- h : Height of the particle in meters (m).
- v : Velocity of the particle in meters per second (m/s).
- g : Acceleration due to gravity, assumed to be 9.8 m/s^2.
Output:
- mechanical_energy : Mechanical energy of the particle in joules (J).
Algorithm:
1. Read the mass of the particle 'm' in kilograms.
2. Read the height of the particle 'h' in meters.
3. Read the velocity of the particle 'v' in meters per second.
4. Set the gravitational acceleration 'g' to 9.8 (GA = 9.8 m/s^2).
5. Calculate the potential energy of the particle 'potential_energy'
using the formula mgh.
6. Calculate the kinetic energy of the particle 'kinetic_energy' using
the formula (1/2)mv^2. (use pow() for square and pow() is a part of
math.h library)
ex: 5^2 can be written as pow(5, 2)
7. Calculate the mechanical energy of the particle 'mechanical_energy'
by adding potential_energy and kinetic_energy.
8. Display the mechanical energy 'mechanical_energy' in joules (J).

Note: Use –lm option for attaching math.h library during comiplation
Ex: gcc file.c –o file –lm
6. Write a C Program to convert Kilometers into Meters and
Centimeters.

Expected Output:

Enter distance in kilometers: 40


Choose the conversion unit:
1. Centimeters
2. Meters
3. Miles
Enter your choice (1-3): 2
Distance in meters: 40000.00

Algorithm:

1. Display a menu with conversion options (centimeters, meters, and


miles)
2. Ask the user to choose a conversion option by entering a number
(1-3)
3. Read the user's choice from the input
4. If choice equals 1, convert kilometers to centimeters
a. Multiply kilometers by 100000 to get centimeters
b. Display the result (distance in centimeters)
5. Else if choice equals 2, convert kilometers to meters
a. Multiply kilometers by 1000 to get meters
b. Display the result (distance in meters)
6. Else if choice equals 3, convert kilometers to miles
a. Multiply kilometers by 0.621371 to get miles
b. Display the result (distance in miles)
7. Else, display an error message (invalid choice)
7. C Program to Check the Given Character is Lowercase or Uppercase
or Special Character.

Expected output:

Enter a character: B
The entered character is an uppercase letter.

Algorithm:
1. Get a character input from the usercopy code
3. Read the character into a variable (ch)
4. Use a switch case statement to check the type of character (ch)
5. If ch is in the range of lowercase letters ('a' to 'z'):
(Syntax: case 'a' ... 'z': )
a. Display "The entered character is a lowercase letter"
6. If ch is in the range of uppercase letters ('A' to 'Z'):
(Syntax: case 'A' ... 'Z': )
a. Display "The entered character is an uppercase letter"
7. Otherwise: (Syntax: default: )
a. Display "The entered character is a special character"
8. End the program

You might also like