C Programming Lab Manual
C Programming Lab Manual
LAB MANUAL
PRINCIPLES OF PROGRAMMING USING C
BPOPS103
Program 1
Simulation of a Simple Calculator.
#include<stdio.h>
#include<math.h>
int main()
{
for(;;)
{
printf("\nEnter the arithmetic expression(Do not add spaces in the expression)\n");
scanf("%d%c%d", &iOperand1, &cOperator, &iOperand2);
switch(cOperator)
{
case ’+’: printf("\nResult = %d", iOperand1 + iOperand2);
break;
case ’-’: printf("\nResult = %d", iOperand1 - iOperand2);
break;
case ’*’: printf("\nResult = %d", iOperand1 * iOperand2);
break;
case ’/’: printf("\nResult = %g", (float)iOperand1 / iOperand2);
break;
case ’%’: printf("\nResult = %d", iOperand1 % iOperand2);
break;
}
printf("\nPress 1 to continue and 0 to quit : ");
scanf("%d", &iChoice);
if(0==iChoice)
{
break;
}
}
return 0;
}
OUTPUT
------------------------------------------------------------------
4+6
Result = 10
2-9
Result = -7
Enter the arithmetic expression expression(Do not add spaces in the expression)
5*2
Result = 10
Enter the arithmetic expression expression(Do not add spaces in the expression)
Enter the arithmetic expression expression(Do not add spaces in the expression)
8/4
Result = 2
Enter the arithmetic expression expression(Do not add spaces in the expression)
15%4
Result = 3
------------------------------------------------------------------
Program 2
Compute the roots of a quadratic equation by accepting the coefficients. Print appropriate messages.
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
float fA,fB,fC,fDesc,fX1,fX2,fRealp,fImagp;
int iState;
printf("\n*************************************************************");
printf("\n*\tPROGRAM TO FIND ROOTS OF A QUADRATIC EQUATION\t *\n");
printf("*************************************************************");
OUTPUT
*************************************************************
* PROGRAM TO FIND ROOTS OF A QUADRATIC EQUATION *
*************************************************************
Enter the coefficients of a,b,c
1 -5 6
The Roots are Real and distinct, they are
Root1 = 3 and Root2 = 2
*************************************************************
* PROGRAM TO FIND ROOTS OF A QUADRATIC EQUATION *
*************************************************************
Enter the coefficients of a,b,c
144
Roots are equal and the Roots are
Root1 = -2 and Root2 = -2
*************************************************************
* PROGRAM TO FIND ROOTS OF A QUADRATIC EQUATION *
*************************************************************
Enter the coefficients of a,b,c
133
The Roots are imaginary and they are
Root1 = -1.5+i0.866025
Root2 = -1.5-i0.866025
*************************************************************
* PROGRAM TO FIND ROOTS OF A QUADRATIC EQUATION *
*************************************************************
Enter the coefficients of a,b,c
012
Invalid input, not a quadratic equation - try again
***************************************
Program 3
An electricity board charges the following rates for the use of electricity: for the first 200 units 80 paise per unit:
for the next 100 units 90 paise per unit: beyond 300 units Rs 1 per unit. All users are charged a minimum of Rs.
100 as meter charge. If the total amount is more than Rs 400, then an additional surcharge of 15% of total
amount is charged. Write a program to read the name of the user, number of units consumed and print out the
charges.
#include<stdio.h>
#include<stdlib.h>
int main( )
{
char cName[30];
int iUnits;
const int iMinCharge = 100;
const double dSlab1 = 0.8;
const double dSlab2 = 0.9;
const double dSlab3 = 1.0;
const double dSurcharge = 0.15;
double dBillAmount = 0.0;
}
else if(iUnits > 200 && iUnits <= 300)
{
dBillAmount += (200*dSlab1)+((iUnits-200)*dSlab2);
}
else
{
dBillAmount += (200*dSlab1)+(100*dSlab2)+((iUnits-300)*dSlab3);
}
if(dBillAmount > 400)
{
printf("\nElectricity Bill\n===================================");
printf("\nCustomer Name\t: %s", cName);
printf("\nUnits Consumed\t: %d", iUnits);
printf("\nBill Amount\t: %0.2lf Rupees\n\n", dBillAmount);
return 0;
OUTPUT
/***************************************
Enter the name of the customer : Ramesh
Enter the number of units consumed : 457
Electricity Bill
===================================
Customer Name : Ramesh
Units Consumed : 457
Bill Amount : 583.05
Program 4
Write a C Program to display the following by reading the number of rows as input.
1
121
12321
1234321
nth row
#include <stdio.h>
void main()
{
int i, j, n;
printf("Input number of rows: ");
scanf("%d", &n);
for(i = 0; i <= n; i++)
{
for(j = 1; j <= n - i; j++)
printf(" ");
for(j = 1; j <= i; j++)
printf("%d", j);
for(j = i - 1; j >= 1; j--)
printf("%d", j);
printf("\n");
}
}
OUTPUT
Enter the number of rows: 3
121
12321
121
12321
1234321
121
12321
1234321
123454321
Program 5
Implement Binary Search on Integers.
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int iaArr[100],iNum,i,iMid,iLow,iHigh,iFound,iKey;
printf("\nEnter the number of elements\n");
scanf("%d",&iNum);
printf("\nEnter the elements in ascending order\n");
for(i=0;i<iNum;i++)
scanf("%d",&iaArr[i]);
printf("\nEnter the key element\n");
scanf("%d",&iKey);
iFound = 0;
iLow = 0;
iHigh = iNum-1;
while(iLow <= iHigh)
{
iMid = (iLow+iHigh)/2;
if(iKey == iaArr[iMid])
{
iFound = 1;
break;
}
else if(iKey < iaArr[iMid])
{
iHigh = iMid-1;
}
else
{
iLow = iMid+1;
}
}
if(iFound)
printf("\nKey element %d found at position %d\n",iKey,iMid+1);
else
printf("\nKey element not found\n");
return 0;
}
OUTPUT
Enter the number of elements 5
Program 6
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*PROGRAM TO IMPLEMENT MATRIX MULIPLICATION*\n");
printf("*********************************************************");
if( iN != iP)
{
printf("\nMatrix Multiplication not possible\n");
exit(0);
}
for(i=0;i<iM;i++)
{
for(j=0;j<iQ;j++)
{
for(k=0;k<iN;k++)
{
for(i=0;i<iM;i++)
{
for(j=0;j<iN;j++)
{
printf("%d\t",iaMat1[i][j]);
}
printf("\n");
}
printf("\n");
printf("\nMatrix 2\n");
for(i=0;i<iP;i++)
{
for(j=0;j<iQ;j++)
{
printf("%d\t",iaMat2[i][j]);
}
printf("\n");
}
printf("\n");
for(i=0;i<iM;i++)
{
for(j=0;j<iQ;j++)
{
printf("%d\t",iaProd[i][j]);
}
printf("\n");
}
printf("\n");
return 0;
}
OUTPUT
*********************************************************
*PROGRAM TO IMPLEMENT MATRIX MULIPLICATION*
*********************************************************
Enter the order of Matrix1
2 3
4 5
*********************************************************
2 3
3 2
1 2 3
4 5 6
1 2
3 4
5 6
Matrix 1
1 2 3
4 5 6
Matrix 2
1 2
3 4
5 6
22 28
49 64
*********************************************************
Matrix 1
1 2
3 4
Matrix 2
1 0
0 1
Program 7
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 = %g\n",fAngD);
printf("No of terms = %d\n",iNum);
fAngR= (fAngD*M_PI)/180 ;
return 0;
}
OUTPUT
/***************************************
Input Angle = 60
No of terms = 12
Calculated value is :
Sin(60)/Cos(60) = 1.73205
Input Angle = 30
No of terms = 3
Calculated value is :
Sin(30)/Cos(30) = 0.577334
Input Angle = 45
No of terms = 11
Calculated value is :
Sin(45)/Cos(45) = 1
Program 8
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;
}
for(i=0;i<iNum;i++)
printf("%d\t",iaArr[i]);
printf("\n");
return 0;
}
OUTPUT
*************************************************
* PROGRAM TO IMPLEMENT BUBBLE SORT *
*************************************************
Enter no of elements 5
*************************************************
* PROGRAM TO IMPLEMENT BUBBLE SORT *
*************************************************
Enter no of elements 6
Program 9
Write functions to implement string operations such as compare, concatenate, and find string length. Use 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("\n1.String Compare\n2.String Concatenate\n3.String 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;
}
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]!='\0'&& s2[k]!='\0'; k++);
OUTPUT
===================== STRING OPERATIONS =====================
String 1 : shiva
String 2 : shankar
1.String Compare
2.String Concatenate
3.String Length
String 1 : ramesh
String 2 : sumesh
1.String Compare
2.String Concatenate
3.String Length
String 1 : sam
String 2 : samantha
1.String Compare
2.String Concatenate
3.String Length
Program 10
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;
for(i=0;i<iNum;i++)
{
printf("\n=========================================");
printf("\nName\t: %s", students[i].cName);
printf("\nUSN\t: %s", students[i].cUSN);
printf("\nMarks\t: %d", students[i].iMarks);
OUTPUT
/***************************************
Enter the number of students : 4
Enter the Student details
=========================================
Name : Raju
USN : 1SI17CS036
Marks : 67
=========================================
Name : Michael
USN : 1SI17CS045
Marks : 87
=========================================
Name : Sahana
USN : 1SI17CS405
Marks : 77
=========================================
Name : Jonathan
USN : 1SI17CS025
Marks : 83
The average marks for the class is : 78.5
=========================================
Name : Raju
USN : 1SI17CS036
Marks : 67
The student has scored below average
=========================================
Name : Michael
USN : 1SI17CS045
Marks : 87
The student has scored above average
=========================================
Name : Sahana
USN : 1SI17CS405
Marks : 77
The student has scored below average
=========================================
Name : Jonathan
USN : 1SI17CS025
Marks : 83
The student has scored above average
Program 11
Develop a 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("\n**************************************\n");
printf("\n**************************************\n");
return 0;
}
OUTPUT
/***************************************
Enter the number of Values : 4
Enter 4 values
1.1 2.2 3.3 4.4
**************************************
Sum = 11
Mean = 2.75
Variance = 1.5125
Standard Deviation = 1.22984
**************************************
==========================================================
Enter the number of Values : 5
Enter 5 values
5.345 6.765 7.234 8.675 9.765
**************************************
Sum = 37.784
Mean = 7.5568
Variance = 2.34995
Standard Deviation = 1.53295
**************************************
Program 12
Write a C program to copy a text file to another, read both the input file name and target file name.
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
FILE *fp1,*fp2;
int ch;
char fname1[100], fname2[100];
printf("\nEnter File name to be copied\n");
scanf("%s",fname1);
fp1 = fopen(fname1,"r");
if(fp1 == NULL)
{
printf("\nInput File %s doesn't exist\n", fname1);
exit(0);
}
fp2 = fopen(fname2,"w");
while((ch=fgetc(fp1)) != EOF)
{
fputc(ch,fp2);
}
printf("\nFile %s successfully created\n",fname2);
fclose(fp1);
fclose(fp2);
return 0;
}
OUTPUT
/**************************************************
Enter File name to be copied
out9.c
===============================================
**************************************************/
VIVA
VIVA
VIVA
VIVA
VIVA
VIVA
VIVA
VIVA
VIVA
VIVA
VIVA
VIVA
LAB MANUAL
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;
}
OUTPUT
*************************************************************
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;
}
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
***********************************************************************
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);
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 an upper case character
***************************************
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.
#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 = %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;
}
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=2y=2p=2q=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=2y=3p=4q=5
b1 = 6 b2 = 5 b3 = 3
Balanced Equation is now:
6*2 + 5*3 ==> 3(4,5)
***************************************
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];
}
}
}
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");
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
22 28
49 64
*********************************************************
* PROGRAM TO IMPLEMENT MATRIX MULIPLICATION *
*********************************************************
Enter the order of Matrix1
2 2
Enter the order of Matrix2
2 2
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;
}
fTerm = fNum/fDen;
}
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
=========================================
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);
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;
}
for(i=0;i<iNum;i++)
printf("%d\t",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
***************************************
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("\n1.String Comparen2.String Concatenaten3.String 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;
break;
}
printf("\nPress 1 to continue and 0 to quit : ");
scanf("%d", &iChoice);
if(0==iChoice)
{
break;
}
}
return 0;
}
{
int iLen;
for(iLen=0; str[iLen] != ''; iLen++);
return iLen;
}
OUTPUT
***************************************
=====================
STRING OPERATIONS
=====================
Enter two strings
String 1: shiva
String 2: shankar
1.String Compare
2.String Concatenate
3.String Length
1.String Compare
2.String Concatenate
3.String Length
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;
dAvg /= iNum;
for(i=0;i<iNum;i++)
{
printf("\n###############################");
return 0;
}
OUTPUT
Enter the number of students: 4
Enter the Student details
=========================================
Name: Raju
USN: 1SI17CS036
Marks: 67
=========================================
Name: Michael
USN: 1SI17CS045
Marks: 87
=========================================
Name: Sahana
USN: 1SI17CS405
Marks: 77
=========================================
Name: Jonathan
USN: 1SI17CS025
Marks: 83
The average marks for the class is: 78.5
=========================================
Name : Raju
USN : 1SI17CS036
Marks : 67
The student has scored below average
=========================================
Name : Michael
USN : 1SI17CS045
Marks : 87
The student has scored above average
=========================================
Name : Sahana
USN : 1SI17CS405
Marks : 77
The student has scored below average
=========================================
Name : Jonathan
USN : 1SI17CS025
Marks : 83
The student has scored above average
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;
}
OUTPUT
***************************************
Enter the number of Values: 4
Enter 4 values
1.1 2.2 3.3 4.4
The values entered are 1.1 2.2 3.3 4.4
**************************************
Sum = 11
Mean = 2.75
Variance = 1.5125
Standard Deviation = 1.22984
**************************************
============================================================
Enter the number of Values: 5
Enter 5 values
5.345 6.765 7.234 8.675 9.765
The values entered are 5.345 6.765 7.234 8.675 9.765
**************************************
Sum = 37.784
Mean = 7.5568
Variance = 2.34995
Standard Deviation = 1.53295
**************************************
VIVA
VIVA
VIVA
VIVA
VIVA
VIVA
VIVA
VIVA
VIVA
VIVA