introduction to c-3
introduction to c-3
Lab Programs
Program1:
C Program to find Mechanical Energy of a particle using E = mgh+1/2 mv2.
Algorithm:
STEP 1: START
STEP 6: [Compute the values of Potential Energy, Kinetic Energy and Mechanical Energy]
START
ACC_GRAV=9.806
READ Mass
Height and
Velocity
dKinEng=dMass*dVelocity* dVelocity/2
dEng=dPotEng+dKinEng
STOP
Program:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
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("*************************************************************");
return 0;
}
Output:
************************************************************
* Program to find Mechanical Energy of a body *
*************************************************************
Enter the mass (in kg) of the body: 80
Algorithm:
STEP 1: START
PRINT Kilometers
PRINT Meters
PRINT Centimeters
STEP 5: STOP
FLOWCHART:
START
READ Kilometers
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*************************************************************");
#include <stdio.h>
int main(void)
{
char cChar;
printf("Enter a character to be checked : ");
scanf("%c", &cChar);
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;
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)