0% found this document useful (0 votes)
11 views12 pages

introduction to c-3

The document provides a series of C programming lab exercises, including programs to calculate mechanical energy, convert kilometers to meters and centimeters, check character cases, and balance chemical equations. Each program includes an algorithm, flowchart, and code implementation with example outputs. The exercises aim to enhance understanding of basic programming concepts and mathematical calculations in C.

Uploaded by

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

introduction to c-3

The document provides a series of C programming lab exercises, including programs to calculate mechanical energy, convert kilometers to meters and centimeters, check character cases, and balance chemical equations. Each program includes an algorithm, flowchart, and code implementation with example outputs. The exercises aim to enhance understanding of basic programming concepts and mathematical calculations in C.

Uploaded by

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

Introduction to C Programming(BESCK104E/204E)

Lab Programs
Program1:
C Program to find Mechanical Energy of a particle using E = mgh+1/2 mv2.
Algorithm:

STEP 1: START

STEP 2: [Intialize] ACC_GRAV=9.806

STEP 3: [Input the mass of the body] read dMass

STEP 4: [Input the displacement of the body] read dHeight

STEP 5: [Input the velocity of the body] read dVelocity

STEP 6: [Compute the values of Potential Energy, Kinetic Energy and Mechanical Energy]

dPotEng=dMass* ACC_GRAV* dHeight


dKinEng=dMass*dVelocity* dVelocity/2
dEng=dPotEng+dKinEng
STEP 7: [Print calculated values]

PRINT Potential Energy

PRINT Kinetic Energy


PRINT Total Energy (Mechanical Energy)
STEP 8: STOP
FLOWCHART:

START

ACC_GRAV=9.806

READ Mass

Height and

Velocity

dPotEng=dMass* ACC_GRAV* dHeight

dKinEng=dMass*dVelocity* dVelocity/2

dEng=dPotEng+dKinEng

PRINT Potential Energy

PRINT Kinetic Energy

PRINT Total Energy


(Mechanical Energy)

STOP
Program:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>

const double ACCL_GRAV = 9.806;

int main()
{
double dMass, dHeight, dVelocity;
double dPotEng, dKinEng, dEng;

printf("\
n*************************************************************");
printf("\n*\tProgram to find Mechanical Energy of a body\t *\n");
printf("*************************************************************");

printf("\nEnter the mass (in kg) of the body: ");


scanf("%lf", &dMass);
printf("\nEnter the height (in metres) of the body: ");
scanf("%lf", &dHeight);
printf("\nEnter the velocity (in meters per second) of the body: ");
scanf("%lf",&dVelocity);

dPotEng = dMass * ACCL_GRAV * dHeight;


dKinEng = dMass * dVelocity * dVelocity / 2;
dEng = dPotEng + dKinEng;

printf("\nPotential energy associated with the body is %0.3lf Joules\n", dPotEng);


printf("\nKinetic energy associated with the body is %0.3lf Joules\n", dKinEng);
printf("\nTotal energy associated with the body is %0.3lf Joules\n", dEng);

return 0;

}
Output:
************************************************************
* Program to find Mechanical Energy of a body *
*************************************************************
Enter the mass (in kg) of the body: 80

Enter the height (in metres) of the body: 10

Enter the velocity (in meters per second) of the body: 10

Potential energy associated with the body is 7844.800 Joules

Kinetic energy associated with the body is 4000.000 Joules

Total energy associated with the body is 11844.800 Joules


Program2:
C Program to convert Kilometers into Meters and Centimeters.

Algorithm:

STEP 1: START

STEP 2: [Input the Kilometers ] read dDistKm

STEP 3: [Compute the value of Meters and Centimeters]

dDistMtr = dDistKm * 1000;


dDistCm = dDistMtr * 100;
STEP 4: [Print calculated values]

PRINT Kilometers

PRINT Meters

PRINT Centimeters
STEP 5: STOP
FLOWCHART:

START

READ Kilometers

Compute the value of Meters and


Centimeters

dDistMtr = dDistKm * 1000;

dDistCm = dDistMtr * 100

PRINT Kilometers

PRINT Meters

PRINT Centimeters

STOP
Program
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main(void)
{
double dDistKm, dDistMtr, dDistCm;

printf("\
n*************************************************************");
printf(\nProgram to convert Kilometers into Meters and Centimeterst \n");
printf("\
n*************************************************************");

printf("\nEnter the distance in kilometers : ");


scanf("%lf",&dDistKm);

dDistMtr = dDistKm * 1000;


dDistCm = dDistMtr * 100;

printf("\nThe distance entered in kilometers is : %0.3lf n", dDistKm);


printf("\nEquivalent distance in meters is : %0.3lf n", dDistMtr);
printf("\nEquivalent distance in centimeters is : %0.3lf n", dDistCm);
return 0;
}
Output:
*************************************************************
Program to convert Kilometers into Meters and Centimeterst
*************************************************************
Enter the distance in kilometers : 63
The distance entered in kilometers is : 63.000
Equivalent distance in meters is : 63000.000
Equivalent distance in centimeters is : 6300000.000
Program 3: To Check the Given Character is Lowercase or Uppercase or Special
Character.

#include <stdio.h>
int main(void)
{
char cChar;
printf("Enter a character to be checked : ");
scanf("%c", &cChar);

if(cChar >= 'a' && cChar <= 'z')


{
printf("The character entered is a lower case character");
}
else if(cChar >= 'A' && cChar <= 'Z')
{
printf("The character entered is a upper case character");
}
else if(cChar >= '0' && cChar <= '9')
{
printf("The character entered is a digit");
}
else
{
printf("The character entered is a special character");
}

return 0;
}
Output:
Enter a character to be checked : a
The character entered is a lower case character
Enter a character to be checked : 1
The character entered is a digit
Enter a character to be checked : #
The character entered is a special character
Enter a character to be checked : A
The character entered is a upper case character
Program 4:
Program to balance the given Chemical Equation values x, y, p, q of a simple chemical
equation of the type: The task is to find the values of constants b1, b2, b3 such that the
equation is balanced on both sides and it must be the reduced form.
#include<stdio.h>
#include<stdlib.h>
int fnGCD(int, int );
int main(void)
{
int x, y, p, q;
int b1, b2, b3;
int iCommDivisor;

printf("Enter the atomocity(x) of Element1 : "); scanf("%d", &x);


printf("Enter the atomocity(y) of Element2 : "); scanf("%d", &y);
printf("Enter the atomocity(p) of Element1 in the compound : "); scanf("%d", &p);
printf("Enter the atomocity(q) of Element2 in the compound : "); scanf("%d", &q);

b1 = p * y;
b2 = q * x;
b3 = x * y;

//if b1, b2 and b3 together have a greatest common divisor divide each one by that
greatest common divisor
iCommDivisor = fnGCD(b1,b2);
iCommDivisor = fnGCD(b3, iCommDivisor);

b1 = b1 / iCommDivisor;
b2 = b2 / iCommDivisor;
b3 = b3 / iCommDivisor;
printf("nx = %dty = %dtp = %dtq = %dn", x, y, p, q);
printf("nb1 = %dtb2 = %dtb3 = %dn", b1, b2,b3);
printf("nBalanced Equation is now :nt%d*%d + %d*%d ==> %d(%d,%d)",
b1,x,b2,y,b3,p,q);

return 0;

}
int fnGCD(int iVal1, int iVal2)
{
if (0 == iVal2)
return iVal1;
return fnGCD(iVal2, iVal1 % iVal2);
}

Output:
Enter the atomocity(x) of Element1 : 2
Enter the atomocity(y) of Element2 : 2
Enter the atomocity(p) of Element1 in the compound : 2
Enter the atomocity(q) of Element2 in the compound : 1
nx = 2ty = 2tp = 2tq = 1nnb1 = 2tb2 = 1tb3 = 2nnBalanced Equation is now :nt2*2 + 1*2
==> 2(2,1)

You might also like