Introduction to C Programming BESCK104E/204E
Department of CSE, Seshadripuram Institute of Technology
Mysuru-571311
Introduction to C Programming BESCK104E/204E
List of Programming Assignments
1. C Program to find Mechanical Energy of a particle using E = mgh+1/2
mv2.
2. C Program to convert Kilometers into Meters and Centimeters.
3. C Program to Check the Given Character is Lowercase or Uppercase
or Special Character.
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.
5. Implement Matrix multiplication and validate the rules of
multiplication.
6. 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.
7. Sort the given set of N numbers using Bubblesort.
8. Write functions to implement string operations such as compare,
concatenate, string length. Convince the parameter passing techniques.
9. Implement structures to read, write and compute average-marks and
the students scoring above and below the average marks for a class of
N students.
10. Develop a program using pointers to compute the sum, mean and
standard deviation of all elements stored in an array of N real numbers.
Department of CSE, Seshadripuram Institute of Technology
Mysuru-571311
Introduction to C Programming BESCK104E/204E
Program 1
Write a C Program to find Mechanical Energy of a particle using E = mgh+1/2 mv2.
#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*1/ 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;
}
Department of CSE, Seshadripuram Institute of Technology
Mysuru-571311
Introduction to C Programming BESCK104E/204E
OUTPUT
Department of CSE, Seshadripuram Institute of Technology
Mysuru-571311
Introduction to C Programming BESCK104E/204E
Program 2
Develop a C Program to convert Kilometers into Meters and Centimeters.
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
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;
}
Department of CSE, Seshadripuram Institute of Technology
Mysuru-571311
Introduction to C Programming BESCK104E/204E
OUTPUT
Department of CSE, Seshadripuram Institute of Technology
Mysuru-571311
Introduction to C Programming BESCK104E/204E
Program 3
Write a C Program to Check the Given Character is Lowercase or Uppercase or
Special Character.
#include<stdio.h>
#include<stdlib.h>
int main()
{
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 character\n");
}
else if(cChar >= 'A' && cChar <= 'Z')
{
printf("\nThe character entered is a upper case character\n");
}
else if(cChar >= '0' && cChar <= '9')
{
printf("\nThe character entered is a digit\n");
}
else
{
printf("\nThe character entered is a special character\n");
}
return 0;
}
Department of CSE, Seshadripuram Institute of Technology
Mysuru-571311
Introduction to C Programming BESCK104E/204E
OUTPUT
Department of CSE, Seshadripuram Institute of Technology
Mysuru-571311
Introduction to C Programming BESCK104E/204E
Program 4
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)
#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("\nEnter the atomocity(x) of Element1 : "); scanf("%d", &x);
printf("\nEnter the atomocity(y) of Element2 : "); scanf("%d", &y);
printf("\nEnter the atomocity(p) of Element1 in the compound : ");
scanf("%d", &p);
printf("\nEnter 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 = %d\n", x, y, p, q);
printf("\nb1 = %dtb2 = %dtb3 = %d\n", b1, b2,b3);
printf("\nBalanced Equation is now :t%d*%d + %d*%d ==> %d(%d,%d)\n",
b1,x,b2,y,b3,p,q);
return 0;
Department of CSE, Seshadripuram Institute of Technology
Mysuru-571311
Introduction to C Programming BESCK104E/204E
int fnGCD(int iVal1, int iVal2)
{
if (0 == iVal2)
return iVal1;
return fnGCD(iVal2, iVal1 % iVal2);
}
Department of CSE, Seshadripuram Institute of Technology
Mysuru-571311
Introduction to C Programming BESCK104E/204E
OUTPUT
Department of CSE, Seshadripuram Institute of Technology
Mysuru-571311
Introduction to C Programming BESCK104E/204E
Program 5
Write a C program to implement Matrix multiplication and validate the rules of
multiplication.
#include<stdio.h>
#include<stdlib.h>
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 Matrix1\n");
scanf("%d%d",&iM,&iN);
printf("nEnter the order of Matrix2\n");
scanf("%d%d",&iP,&iQ);
if( iN != iP)
{
printf("\nMatrix Multiplication not possible\n");
exit(0);
}
printf("\nEnter the elements of Matrix 1\n");
for(i=0;i<iM;i++)
for(j=0;j<iN;j++)
scanf("%d",&iaMat1[i][j]);
printf("\nEnter the elements of Matrix 2\n");
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];
}
}
}
Department of CSE, Seshadripuram Institute of Technology
Mysuru-571311
Introduction to C Programming BESCK104E/204E
printf("\nMatrix 1\n");
for(i=0;i<iM;i++)
{
for(j=0;j<iN;j++)
{
printf("%dt",iaMat1[i][j]);
}
printf("\n");
}
printf("\n");
printf("\nMatrix 2\n");
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 \n");
for(i=0;i<iM;i++)
{
for(j=0;j<iQ;j++)
{
printf("%dt",iaProd[i][j]);
}
printf("\n");
}
printf("\n");
return 0;
}
Department of CSE, Seshadripuram Institute of Technology
Mysuru-571311
Introduction to C Programming BESCK104E/204E
OUTPUT
Department of CSE, Seshadripuram Institute of Technology
Mysuru-571311
Introduction to C Programming BESCK104E/204E
Program 6
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.
#include<stdio.h>
#include<stdlib.h>
#include <math.h>
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 = %f\n",fAngD);
printf("No of terms = %d\n",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++)
{
Department of CSE, Seshadripuram Institute of Technology
Mysuru-571311
Introduction to C Programming BESCK104E/204E
fCVal = fCVal + fTerm;
fNum = -fNum * fAngR * fAngR ;
fDen = fDen * (2*i) * (2*i-1);
fTerm = fNum/fDen;
}
printf("\nCalculated value is :Sin(%f)/Cos(%f) = %f\n",fAngD, fAngD,
fSVal/fCVal);
printf("\nBuilt In function value is :Sin(%f)/Cos(%f) = %f\n", fAngD, fAngD,
sin(fAngR)/cos(fAngR));
return 0;
}
Department of CSE, Seshadripuram Institute of Technology
Mysuru-571311
Introduction to C Programming BESCK104E/204E
OUTPUT
Department of CSE, Seshadripuram Institute of Technology
Mysuru-571311
Introduction to C Programming BESCK104E/204E
Program 7
Write a C program to sort the given set of N numbers using Bubble sort.
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int iNum, i, j, iaArr[10], iTemp;
printf("\n*************************************************");
printf("\n*\tPROGRAM TO IMPLEMENT BUBBLE SORT\t*\n");
printf("*************************************************");
printf("\nEnter no of elements\n");
scanf("%d",&iNum);
printf("\nEnter the elements\n");
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;
}
printf("\nThe Sorted array is \n");
for(i=0;i<iNum;i++)
printf("%d\t",iaArr[i]);
printf("\n");
return 0;
}
Department of CSE, Seshadripuram Institute of Technology
Mysuru-571311
Introduction to C Programming BESCK104E/204E
OUTPUT
Department of CSE, Seshadripuram Institute of Technology
Mysuru-571311
Introduction to C Programming BESCK104E/204E
Program 8
Write functions to implement string operations such as compare, concatenate, string
length. Convince the parameter passing techniques.
#include<stdio.h>
#include<stdlib.h>
int fnMyStrCmp(const char*, const char*);
void fnMyStrCat(char*, const char*);
int fnMyStrLen(const char*);
int main()
{
int iChoice;
char acStr1[30], acStr2[30];
int iLen;
printf("\n=====================\n");
printf("STRING OPERATIONS");
printf("\n=====================\n");
for(;;)
{
printf("\nEnter two strings\n");
printf("\nString 1 : "); scanf("%s", acStr1);
printf("\nString 2 : "); scanf("%s", acStr2);
printf("\[Link] [Link] [Link] Length");
printf("\nEnter your choice : "); scanf("%d", &iChoice);
switch(iChoice)
{
case 1: if(fnMyStrCmp(acStr1, acStr2) == 0)
printf("\nTwo strings are equal");
else if(fnMyStrCmp(acStr1, acStr2) > 0)
printf("\nString %s is greater than String
%s", acStr1, acStr2);
else
printf("\nString %s is greater than String
%s", acStr2, acStr1);
break;
case 2: fnMyStrCat(acStr1, acStr2);
printf("\nConcatenated String is %s", acStr1);
break;
Department of CSE, Seshadripuram Institute of Technology
Mysuru-571311
Introduction to C Programming BESCK104E/204E
case 3: iLen = fnMyStrLen(acStr1);
printf("\nLength of String %s is %d\n", acStr1,
iLen);
iLen = fnMyStrLen(acStr2);
printf("\nLength of String %s is %d\n", acStr2,
iLen);
break;
}
printf("\nPress 1 to continue and 0 to quit : ");
scanf("%d", &iChoice);
if(0==iChoice)
{
break;
}
}
return 0;
}
int fnMyStrCmp(const char *s1, const char *s2)
{
int k;
for(k=0; s1[k] == s2[k] && s1[k]!=''&& s2[k]!=''; k++);
if( k==(fnMyStrLen(s1)) && k==(fnMyStrLen(s2)) )
{
return 0;
}
else if(s1[k] > s2[k])
{
return 1;
}
else
{
return -1;
}
}
void fnMyStrCat(char *dest, const char *src)
{
int dest_len, i;
Department of CSE, Seshadripuram Institute of Technology
Mysuru-571311
Introduction to C Programming BESCK104E/204E
dest_len = fnMyStrLen(dest);
for (i = 0 ; src[i] != '' ; i++)
dest[dest_len + i] = src[i];
dest[dest_len + i] = '';
}
int fnMyStrLen(const char *str)
{
int iLen;
for(iLen=0; str[iLen] != ''; iLen++);
return iLen;
}
Department of CSE, Seshadripuram Institute of Technology
Mysuru-571311
Introduction to C Programming BESCK104E/204E
OUTPUT
Department of CSE, Seshadripuram Institute of Technology
Mysuru-571311
Introduction to C Programming BESCK104E/204E
Program 9
Write a C program to implement structures to read, write and compute average
marks and the students scoring above and below the average marks for a class of N
students.
#include<stdio.h>
#include<stdlib.h>
#define STRSIZE 30
typedef struct
{
char cName[STRSIZE];
char cUSN[11];
int iMarks;
}STUDENT_TYPE;
int main(void)
{
STUDENT_TYPE students[100];
int iNum, i;
double dAvg = 0.0;
printf("\nEnter the number of students : ");
scanf("%d", &iNum);
printf("\nEnter the Student details\n");
for(i=0;i<iNum;i++)
{
printf("\n###############################");
printf("\nName : "); scanf("%s", students[i].cName);
printf("\nUSN : "); scanf("%s", students[i].cUSN);
printf("\nMarks : "); scanf("%d", &students[i].iMarks);
dAvg += students[i].iMarks;
}
dAvg /= iNum;
printf("\nThe average marks for the class is : %g\n", dAvg);
for(i=0;i<iNum;i++)
Department of CSE, Seshadripuram Institute of Technology
Mysuru-571311
Introduction to C Programming BESCK104E/204E
{
printf("\n###############################");
printf("\nNamet: %s", students[i].cName);
printf("\nUSNt: %s", students[i].cUSN);
printf("\nMarkst: %d", students[i].iMarks);
if(students[i].iMarks < dAvg)
printf("\nThe student has scored below average\n");
else
printf("\nThe student has scored above average\n");
}
return 0;
}
Department of CSE, Seshadripuram Institute of Technology
Mysuru-571311
Introduction to C Programming BESCK104E/204E
OUTPUT
Department of CSE, Seshadripuram Institute of Technology
Mysuru-571311
Introduction to C Programming BESCK104E/204E
Program 10
Develop a C program using pointers to compute the sum, mean and standard
deviation of all elements stored in an array of N real numbers.
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main(void)
{
int i,iNum;
float fMean = 0.0f, fVariance = 0.0f, fSd = 0.0f,faArray[100],fSum=0.0f;
float *fptr;
printf("\nEnter the number of Values : ");
scanf("%d",&iNum);
fptr = faArray;
printf("\nEnter %d valuesn", iNum);
for(i=0; i<iNum; i++)
{
scanf("%f",fptr+i);
fSum += *(fptr+i); //fSum += fptr[i]; this is also valid
}
fMean = fSum/iNum;
for(i=0; i<iNum; i++)
{
fVariance += (fptr[i] - fMean)*(fptr[i] - fMean);
//fVariance += (*(fptr+i) - fMean)*(*(fptr+i) - fMean);
}
fVariance /= iNum;
fSd = sqrt(fVariance);
printf("\nThe values entered are");
for(i=0; i<iNum; i++)
{
printf("\t%g",fptr[i]); //printf("\n\t%f",*(fptr+i));
}
printf("\n");
printf("\n**************************************\n");
printf("\tSumt = \t%g\n\tMeant =\t%g\n\tVariance = \t%g\nStandard Deviation =
\t%g", fSum,fMean,fVariance,fSd);
printf("\n**************************************\n");
return 0;
}
Department of CSE, Seshadripuram Institute of Technology
Mysuru-571311
Introduction to C Programming BESCK104E/204E
OUTPUT
Department of CSE, Seshadripuram Institute of Technology
Mysuru-571311
Introduction to C Programming BESCK104E/204E
OUTPUT
Department of CSE, Seshadripuram Institute of Technology
Mysuru-571311
Introduction to C Programming BESCK104E/204E
OUTPUT
Department of CSE, Seshadripuram Institute of Technology
Mysuru-571311
Introduction to C Programming BESCK104E/204E
OUTPUT
Department of CSE, Seshadripuram Institute of Technology
Mysuru-571311
Introduction to C Programming BESCK104E/204E
OUTPUT
Department of CSE, Seshadripuram Institute of Technology
Mysuru-571311