How to Edit, Compile and Run the C Programs:
Step1: Double click the Terminal on the screen make a new directory (only once)
using the command mkdir directory_Name (EIE_urname) and type
cd EIE_urname (the directory name u created)
Step2: type : gedit filename.c press enter
Step3: type the c program in the editor and save the program (ctrl+s) and quit
editor (ctrl+q)
Step 4: Compile the program using: cc filename.c and press enter. If program
contains any error go back to editor using gedit filename.c and make corrections
in the program and again save the program (ctrl+s) and quit editor (ctrl+q) and
type cc filename.c if no errors
Step 5: To check the output type ./[Link] press enter. You can see the output of
the program on the screen.
Programming Exercises
1. C Program to find Mechanical Energy of a particle using E = mgh+1/2 mv2
To calculate the mechanical energy of a particle using the formula E=mgh+1/2mv2,
we need to give the input values as, for mass (m), height (h), gravitational
acceleration (g), and velocity (v).
#include <stdio.h>
int main( )
{
/* declare variables */
float m, g, h, v, E;
/* Initialize gravitational acceleration (approx. value on Earth) */
g = 9.81; // m/s^2
/* input from the user */
printf("Enter the mass of the particle (in kg): ");
scanf("%f", &m);
printf("Enter the height of the particle (in meters): ");
scanf("%f", &h);
printf("Enter the velocity of the particle (in m/s): ");
scanf("%f", &v);
/* calculate mechanical energy */
E = (m * g * h) + (0.5 * m * v * v);
/* Display the result */
printf("The mechanical energy E of the particle is: %.2f Joules\n", E);
return 0;
}
Output1:
Output2:
Output3:
2. C Program to convert Kilometers into Meters and Centimeters
#include <stdio.h>
int main()
{
/* declare variable for kilometres */
float km, mtr, centi_mtrs;
/* input from the user */
printf("Enter distance in kilometers: ");
scanf("%f", &km);
/* Conversion calculations */
mtr = km * 1000; /* 1 kilometer = 1000 meters */
centi_mtrs = km * 100000; /*1 kilometer = 100000 centimeters*/
/* Display the results */
printf("%.2f kilometers is equal to:\n", km);
printf("%.2f meters\n", mtr);
printf("%.2f centimeters\n", centi_mtrs);
return 0;
}
Output1:
Output2:
Output3:
3. C Program To Check the Given Character is Lowercase or Uppercase or
Special Character
#include <stdio.h>
#include <ctype.h> /* for islower, isupper */
int main()
{
char ch;
/* Get user input */
printf("Enter a character: ");
scanf("%c", &ch);
/* Check if the character is uppercase */
if (isupper(ch))
{
printf("'%c' is an uppercase letter.\n", ch);
}
/* Check if the character is lowercase */
else if (islower(ch))
{
printf("'%c' is a lowercase letter.\n", ch);
}
/* If it's neither, it's a special character */
else
{
printf("'%c' is a special character.\n", ch);
}
return 0;
}
Output1:
Output2:
Output3: