In this blog post, you will find solutions for the Introduction to C Programming Lab Manual (BESCK104E/204E) course work for the I year students of VTU university. The academic syllabus has been changed for the year 2022-23 in VTU. A new subject has been introduced titled “Introduction to C Programming” (Sub Code: BESCK104E/204E) for first year students of VTU. This subject has an integrated lab component. Anybody can contribute and re-share this manual as it is distributed under Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Also note the same subject is part of the autonomous curriculum at Siddaganga Institute of technology with code ESCO5.
All the programs have been compiled using the GNU GCC compiler version 9.4.0 and has been tested on Ubuntu 22.04.4 LTS (Jammy Jellyfish) 64-bit Kernel Linux 5.15.0-56-generic.
These programs can run on any GNU/Linux Operating system or any other OS with GCC installed.
GCC compiler can be obtained from here. A nice tutorial is provided here to get started with GCC.
If you find the command line difficult to handle and looking for an GUI based IDE to work with, the best choice is Code::Blocks. Code::Blocks uses the GNU GCC compiler collection. Two other good IDEs that you can try or Eclipse and Anjuta. I have provided links to this IDEs below you can click and download the necessary software and instructions to set up a working environment.
After getting the necessary development environment setup, Now lets focus on the solutions. You can go to the appropriate program by clicking on the links below.
- Question 01 – Mechanical Energy of a Particle
- Question 02 – Distance Conversion
- Question 03 – Check Character case
- Question 04 – Balancing Chemical Equation
- Question 05 – Matrix Multiplication
- Question 06 – Compute Sine and Cosine of an Angle
- Question 07 – Bubble Sort
- Question 08 – String Operations
- Question 09 – C Structures
- Question 10 – Pointers and Arrays
Question 1
Mechanical Energy of a Particle
Write a C Program to find Mechanical Energy of a particle using E = mgh+1/2 mv2.
C Code
/***************************************************************************
*File : A01MechEnergy.c
*Description : C Program to find Mechanical Energy of a particle using E = mgh+1/2 mv2.
*Author : Prabodh C P
*Compiler : gcc compiler, Ubuntu 22.04
*Date : 02 December 2022
***************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
const double ACCL_GRAV = 9.806;
/***************************************************************************
*Function : main
*Input parameters : no parameters
*RETURNS : 0 on success
***************************************************************************/
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
*************************************************************
Question 2
Distance Conversion
Develop a C Program to convert Kilometers into Meters and Centimeters.
C Code
/***************************************************************************
*File : A02DistConvert.c
*Description : Program to convert Kilometers into Meters and Centimeters.
*Author : Prabodh C P
*Compiler : gcc compiler, Ubuntu 22.04
*Date : 02 December 2022
***************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
/***************************************************************************
*Function : main
*Input parameters : no parameters
*RETURNS : 0 on success
***************************************************************************/
int main(void)
{
double dDistKm, dDistMtr, dDistCm;
printf("n*************************************************************");
printf("n*tProgram to convert Kilometers into Meters and Centimeterst *n");
printf("*************************************************************");
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 Centimeters *
***********************************************************************
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
***********************************************************************
Question 3
Check Character case
Write a C program To Check the Given Character is Lowercase or Uppercase or Special Character.
C Code
/***************************************************************************
*File : A03CheckCharacter.c
*Description : Program to check the given character is Lowercase or Uppercase or Special character.
*Author : Prabodh C P
*Compiler : gcc compiler, Ubuntu 22.04
*Date : 10 August 2022
***************************************************************************/
#include<stdio.h>
#include<stdlib.h>
/***************************************************************************
*Function : main
*Input parameters : no parameters
*RETURNS : 0 on success
***************************************************************************/
int main(void)
{
char cChar;
printf("nEnter a character to be checked : "); scanf("%c", &cChar);
if(cChar >= 'a' && cChar <= 'z')
{
printf("nThe character entered is a lower case charactern");
}
else if(cChar >= 'A' && cChar <= 'Z')
{
printf("nThe character entered is a upper case charactern");
}
else if(cChar >= '0' && cChar <= '9')
{
printf("nThe character entered is a digitn");
}
else
{
printf("nThe character entered is a special charactern");
}
return 0;
}
Output
***************************************
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 : s
The character entered is a lower case character
Enter a character to be checked : S
The character entered is a upper case character
***************************************
Question 4
Balancing Chemical Equation
Write a C 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.
Generic Chemical Equation Form b1 ∗ Ax + b2 ∗ By ⇒ b3 (Ap Bq)
C Code
/***************************************************************************
*File : A04BalanceChemEqn.c
*Description : Program to balance the given Chemical Equation
of the form b1*Ax + b2*By ==> b3(ApBq)
*Author : Prabodh C P
*Compiler : gcc compiler, Ubuntu 22.04
*Date : 26 December 2022
***************************************************************************/
#include<stdio.h>
#include<stdlib.h>
int fnGCD(int, int );
/***************************************************************************
*Function : main
*Input parameters : no parameters
*RETURNS : 0 on success
***************************************************************************/
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)n", b1,x,b2,y,b3,p,q);
return 0;
}
/***************************************************************************
*Function : fnGCD
*Description : function to calculate GCD of two numbers
*Input parameters : iVal1 - non-negative integer
iVal2 - non-negative integer
*RETURNS : greatest common divisor of iVal1 and iVal2
***************************************************************************/
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
x = 2 y = 2 p = 2 q = 1
b1 = 2 b2 = 1 b3 = 2
Balanced Equation is now :
2*2 + 1*2 ==> 2(2,1)
Enter the atomocity(x) of Element1 : 2
Enter the atomocity(y) of Element2 : 3
Enter the atomocity(p) of Element1 in the compound : 4
Enter the atomocity(q) of Element2 in the compound : 5
x = 2 y = 3 p = 4 q = 5
b1 = 6 b2 = 5 b3 = 3
Balanced Equation is now :
6*2 + 5*3 ==> 3(4,5)
***************************************
Question 5
Matrix Multiplication
Write a C program to implement Matrix multiplication and validate the rules of multiplication.
C Code
/***************************************************************************
*File : A05MatrixMul.c
*Description : Program to implement Matrix Multiplication
*Author : Prabodh C P
*Compiler : gcc compiler, Ubuntu 22.04
*Date : 10 August 2022
***************************************************************************/
#include<stdio.h>
#include<stdlib.h>
/***************************************************************************
*Function : main
*Input parameters : no parameters
*RETURNS : 0 on success
***************************************************************************/
int main(void)
{
int iM, iN, iP, iQ, i, j, k, iaMat1[10][10], iaMat2[10][10];
int iaProd[10][10] = {0};
printf("n*********************************************************");
printf("n*tPROGRAM TO IMPLEMENT MATRIX MULIPLICATIONt*n");
printf("*********************************************************");
printf("nEnter the order of Matrix1n");
scanf("%d%d",&iM,&iN);
printf("nEnter the order of Matrix2n");
scanf("%d%d",&iP,&iQ);
if( iN != iP)
{
printf("nMatrix Multiplication not possiblen");
exit(0);
}
printf("nEnter the elements of Matrix 1n");
for(i=0;i<iM;i++)
for(j=0;j<iN;j++)
scanf("%d",&iaMat1[i][j]);
printf("nEnter the elements of Matrix 2n");
for(i=0;i<iP;i++)
for(j=0;j<iQ;j++)
scanf("%d",&iaMat2[i][j]);
for(i=0;i<iM;i++)
{
for(j=0;j<iQ;j++)
{
for(k=0;k<iN;k++)
{
iaProd[i][j] += iaMat1[i][k] * iaMat2[k][j];
}
}
}
/**************************************************************************************************
|*| |*|
a00 a01 a02|*|b00 b01 b02|*|
|*| |*|
a10 a11 a12|*|b10 b11 b12|*|
|*| |*|
a20 a21 a22|*|b20 b21 b22|*|
|*| |*|
(a00*b00+a01*b10+a02*b20) (a00*b01+a01*b11+a02*b21) (a00*b02+a01*b12+a02*b22)
(a10*b00+a11*b10+a12*b20) (a10*b01+a11*b11+a12*b21) (a10*b02+a11*b12+a12*b22)
(a20*b00+a21*b10+a22*b20) (a20*b01+a21*b11+a22*b21) (a20*b02+a21*b12+a22*b22)
**************************************************************************************************/
printf("nMatrix 1n");
for(i=0;i<iM;i++)
{
for(j=0;j<iN;j++)
{
printf("%dt",iaMat1[i][j]);
}
printf("n");
}
printf("n");
printf("nMatrix 2n");
for(i=0;i<iP;i++)
{
for(j=0;j<iQ;j++)
{
printf("%dt",iaMat2[i][j]);
}
printf("n");
}
printf("n");
printf("nThe Product matrix is is n");
for(i=0;i<iM;i++)
{
for(j=0;j<iQ;j++)
{
printf("%dt",iaProd[i][j]);
}
printf("n");
}
printf("n");
return 0;
}
Output
*********************************************************
* PROGRAM TO IMPLEMENT MATRIX MULIPLICATION *
*********************************************************
Enter the order of Matrix1
2 3
Enter the order of Matrix2
4 5
Matrix Multiplication not possible
*********************************************************
* PROGRAM TO IMPLEMENT MATRIX MULIPLICATION *
*********************************************************
Enter the order of Matrix1
2 3
Enter the order of Matrix2
3 2
Enter the elements of Matrix 1
1 2 3
4 5 6
Enter the elements of Matrix 2
1 2
3 4
5 6
Matrix 1
1 2 3
4 5 6
Matrix 2
1 2
3 4
5 6
The Product matrix is is
22 28
49 64
*********************************************************
* PROGRAM TO IMPLEMENT MATRIX MULIPLICATION *
*********************************************************
Enter the order of Matrix1
2 2
Enter the order of Matrix2
2 2
Enter the elements of Matrix 1
1 2
3 4
Enter the elements of Matrix 2
1 0
0 1
Matrix 1
1 2
3 4
Matrix 2
1 0
0 1
The Product matrix is is
1 2
3 4
***************************************
Question 6
Compute Sine and Cosine of an Angle
Write a C program to compute sin(x)/cos(x) using Taylor series approximation. Compare your result with the built-in library function. Print both the results with appropriate inferences.
C Code
/***************************************************************************
*File : A06SineCosAngle.c
*Description : Program to calculate Sin(x)/Cos(x) using Taylor series
*Author : Prabodh C P
*Compiler : gcc compiler, Ubuntu 22.04
*Date : 10 August 2022
***************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include <math.h>
/***************************************************************************
*Function : main
*Input parameters : no parameters
*RETURNS : 0 on success
***************************************************************************/
int main()
{
float fAngD, fAngR;
float fTerm, fNum, fDen, fSVal,fCVal;
int i,iNum;
printf("nEnter the Angle : "); scanf("%f",&fAngD);
printf("nEnter the Number of terms : "); scanf("%d",&iNum);
printf("nInput Angle = %gn",fAngD);
printf("No of terms = %dn",iNum);
fAngR= (fAngD*M_PI)/180 ;
//Calculation of Sine of an angle using Taylor's series
fNum=fAngR;
fDen=1.0;
fSVal =0.0;
fTerm=fNum/fDen;
for(i=1;i<=iNum;i++)
{
fSVal = fSVal + fTerm;
fNum = -fNum * fAngR * fAngR ;
fDen = fDen * (2*i) * (2*i+1);
fTerm = fNum/fDen;
}
//Calculation of Cosine of an angle using Taylor's series
fNum=1.0;
fDen=1.0;
fCVal =0.0;
fTerm=1.0;
for(i=1;i<=iNum;i++)
{
fCVal = fCVal + fTerm;
fNum = -fNum * fAngR * fAngR ;
fDen = fDen * (2*i) * (2*i-1);
fTerm = fNum/fDen;
}
printf("nCalculated value is :nSin(%g)/Cos(%g) = %gn",fAngD, fAngD, fSVal/fCVal);
printf("nBuilt In function value is :nSin(%g)/Cos(%g) = %gn", fAngD, fAngD, sin(fAngR)/cos(fAngR));
return 0;
}
Output
***************************************
Enter the Angle : 60
Enter the Number of terms : 12
Input Angle = 60 No of terms = 12
Calculated value is :
Sin(60)/Cos(60) = 1.73205
Built In function value is :
Sin(60)/Cos(60) = 1.73205
=========================================
Enter the Angle : 30
Enter the Number of terms : 3
Input Angle = 30 No of terms = 3
Calculated value is :
Sin(30)/Cos(30) = 0.577334
Built In function value is :
Sin(30)/Cos(30) = 0.57735
=========================================
Enter the Angle : 45
Enter the Number of terms : 11
Input Angle = 45 No of terms = 11
Calculated value is :
Sin(45)/Cos(45) = 1
Built In function value is :
Sin(45)/Cos(45) = 1
***************************************
Question 7
Bubble Sort
Write a C program to sort the given set of N numbers using Bubble sort.
C Code
/***************************************************************************
*File : A07BubbleSort.c
*Description : Program to implement Bubble Sort Algorithm
*Author : Prabodh C P
*Compiler : gcc compiler, Ubuntu 22.04
*Date : 10 August 2022
***************************************************************************/
#include<stdio.h>
#include<stdlib.h>
/***************************************************************************
*Function : main
*Input parameters : no parameters
*RETURNS : 0 on success
***************************************************************************/
int main(void)
{
int iNum, i, j, iaArr[10], iTemp;
printf("n*************************************************");
printf("n*tPROGRAM TO IMPLEMENT BUBBLE SORTt*n");
printf("*************************************************");
printf("nEnter no of elementsn");
scanf("%d",&iNum);
printf("nEnter the elementsn");
for(i=0;i<iNum;i++)
scanf("%d",&iaArr[i]);
for(i=0;i<iNum;i++)
{
for(j=0;j<iNum-i-1;j++)
{
if(iaArr[j] > iaArr[j+1])
{
iTemp = iaArr[j];
iaArr[j] = iaArr[j+1];
iaArr[j+1] = iTemp;
}
/*Code to show the program trace*/
/* printf("nIteration i=%d, j=%dn",i,j);*/
/* for(k=0;k<iNum;k++)*/
/* printf("%dt",iaArr[k]);*/
}
}
printf("nThe Sorted array is n");
for(i=0;i<iNum;i++)
printf("%dt",iaArr[i]);
printf("n");
return 0;
}
Output
*************************************************
* PROGRAM TO IMPLEMENT BUBBLE SORT *
*************************************************
Enter no of elements
5
Enter the elements
2 1 6 5 7
The Sorted array is
1 2 5 6 7
*************************************************
* PROGRAM TO IMPLEMENT BUBBLE SORT *
*************************************************
Enter no of elements
6
Enter the elements
9 7 5 3 1 0
The Sorted array is
0 1 3 5 7 9
***************************************
Question 8
String Operations
Write functions to implement string operations such as compare, concatenate, string length. Convince the parameter passing techniques.