0% found this document useful (0 votes)
16 views42 pages

BPOPS103 Border

The document outlines a series of programming assignments for a course on C programming, including tasks such as creating a simple calculator, computing roots of quadratic equations, and implementing binary search. Each assignment includes specific requirements and example code to guide students in their implementation. The document serves as a comprehensive guide for students to practice and enhance their programming skills in C.

Uploaded by

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

BPOPS103 Border

The document outlines a series of programming assignments for a course on C programming, including tasks such as creating a simple calculator, computing roots of quadratic equations, and implementing binary search. Each assignment includes specific requirements and example code to guide students in their implementation. The document serves as a comprehensive guide for students to practice and enhance their programming skills in C.

Uploaded by

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

Principles of Programming using C BPOPS103/203

Department of CSE, Seshadripuram Institute of Technology


Mysuru-571311
Principles of Programming using C BPOPS103/203

List of Programming Assignments


1. Simulation of a Simple Calculator.
2. Compute the roots of a quadratic equation by accepting the coefficients.
Print appropriate messages.
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.
4. Write a C Program to display the following by reading the number of
rows as input
1
121
12321
1234321
---------------------------
nth row
5. Implement Binary Search on Integers.
6. Implement Matrix multiplication and validate the rules of multiplication.
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.
8. Sort the given set of N numbers using Bubble sort.
9. Write functions to implement string operations such as compare,
concatenate, and find string length. Use the parameter passing techniques.
10. Implement structures to read, write and compute average- marks of the
students, list the students scoring above and below the average marks for
a class of N students.
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.
12. Write a C program to copy a text file to another, read both the input file
name and target file name.

Department of CSE, Seshadripuram Institute of Technology


Mysuru-571311
Principles of Programming using C BPOPS103/203

Program 1
Simulation of a Simple Calculator.

#include<stdio.h>
#include<math.h>
int main()
{

int iChoice, iOperand1, iOperand2;


char cOperator;

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;
}

Department of CSE, Seshadripuram Institute of Technology


Mysuru-571311
Principles of Programming using C BPOPS103/203

OUTPUT

Department of CSE, Seshadripuram Institute of Technology


Mysuru-571311
Principles of Programming using C BPOPS103/203

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("***********************************************************
**");

printf("\nEnter the coefficients of a,b,c \n");


scanf("%f%f%f",&fA,&fB,&fC);
if(0 == fA)
{
printf("\nInvalid input, not a quadratic equation - try again\n");
exit(0);
}

/*COMPUTE THE DESCRIMINANT*/


fDesc = fB * fB - 4 * fA * fC;
((0 == fDesc) ? (iState = 1):((fDesc > 0) ? (iState = 2) : (iState = 3)));
switch(iState)
{
case 1:fX1 = fX2 = -fB/(2*fA);
printf("\nRoots are equal and the Roots are \n");
printf("\nRoot1 = %g and Root2 = %g\n",fX1,fX2);
break;
case 2: fX1 = (-fB+sqrt(fDesc))/(2*fA);
fX2 = (-fB-sqrt(fDesc))/(2*fA);
printf("\nThe Roots are Real and distinct, they are \n");
printf("\nRoot1 = %g and Root2 = %g\n",fX1,fX2);
break;
case 3:fRealp = -fB / (2*fA);
fImagp = sqrt(fabs(fDesc))/(2*fA);

Department of CSE, Seshadripuram Institute of Technology


Mysuru-571311
Principles of Programming using C BPOPS103/203

printf("\nThe Roots are imaginary and they are\n");


printf("\nRoot1 = %g+i%g\n",fRealp,fImagp);
printf("\nRoot2 = %g-i%g\n",fRealp,fImagp);
}
return 0;
}

Department of CSE, Seshadripuram Institute of Technology


Mysuru-571311
Principles of Programming using C BPOPS103/203

OUTPUT

Department of CSE, Seshadripuram Institute of Technology


Mysuru-571311
Principles of Programming using C BPOPS103/203

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;

printf("\nEnter the name of the customer : "); scanf("%s", cName);


printf("\nEnter the number of units consumed : "); scanf("%d", &iUnits);
dBillAmount += iMinCharge;
if(iUnits <= 200)
{
dBillAmount += iUnits*dSlab1;

}
else if(iUnits > 200 && iUnits <= 300)
{
dBillAmount += (200*dSlab1)+((iUnits-200)*dSlab2);
}
else
{
dBillAmount += (200*dSlab1)+(100*dSlab2)+((iUnits-
300)*dSlab3);
}
if(dBillAmount > 400)
{

Department of CSE, Seshadripuram Institute of Technology


Mysuru-571311
Principles of Programming using C BPOPS103/203

dBillAmount += dBillAmount * dSurcharge;


}
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;

Department of CSE, Seshadripuram Institute of Technology


Mysuru-571311
Principles of Programming using C BPOPS103/203

OUTPUT

Department of CSE, Seshadripuram Institute of Technology


Mysuru-571311
Principles of Programming using C BPOPS103/203

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>
#include<stdlib.h>
int main()
{
int iNum, iSp_cnt, iNum_cnt, iVal, i, j, k;
printf("Enter the number of rows : ");
scanf("%d", &iNum);
iSp_cnt = iNum - 1;
iNum_cnt = 1;

for(i=0;i<iNum;i++)
{
iVal = 1;
for(j=0;j<iSp_cnt;j++)
{
printf(" ");
}
for(k=0;k<iNum_cnt;k++)
{
if(k <= iNum_cnt/2)
{
printf("%d ", iVal);
iVal++;
}
else
{
iVal--;
printf("%d ", iVal-1);

Department of CSE, Seshadripuram Institute of Technology


Mysuru-571311
Principles of Programming using C BPOPS103/203

}
}
printf("\n");
iSp_cnt--;
iNum_cnt += 2;
}
return 0;
}

Department of CSE, Seshadripuram Institute of Technology


Mysuru-571311
Principles of Programming using C BPOPS103/203

OUTPUT

Department of CSE, Seshadripuram Institute of Technology


Mysuru-571311
Principles of Programming using C BPOPS103/203

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;
}

Department of CSE, Seshadripuram Institute of Technology


Mysuru-571311
Principles of Programming using C BPOPS103/203

OUTPUT

Department of CSE, Seshadripuram Institute of Technology


Mysuru-571311
Principles of Programming using C BPOPS103/203

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("*********************************************************");

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++)

Department of CSE, Seshadripuram Institute of Technology


Mysuru-571311
Principles of Programming using C BPOPS103/203

{
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("%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");

printf("\nThe Product matrix is is \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;
}

Department of CSE, Seshadripuram Institute of Technology


Mysuru-571311
Principles of Programming using C BPOPS103/203

OUTPUT

Department of CSE, Seshadripuram Institute of Technology


Mysuru-571311
Principles of Programming using C BPOPS103/203

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 ;

//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;
}

Department of CSE, Seshadripuram Institute of Technology


Mysuru-571311
Principles of Programming using C BPOPS103/203

printf("\nCalculated value is :\nSin(%g)/Cos(%g) = %g\n",fAngD, fAngD,


fSVal/fCVal);
printf("\nBuilt In function value is :\nSin(%g)/Cos(%g) = %g\n", fAngD, fAngD,
sin(fAngR)/cos(fAngR));

return 0;
}

Department of CSE, Seshadripuram Institute of Technology


Mysuru-571311
Principles of Programming using C BPOPS103/203

OUTPUT

Department of CSE, Seshadripuram Institute of Technology


Mysuru-571311
Principles of Programming using C BPOPS103/203

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;
}

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
Principles of Programming using C BPOPS103/203

OUTPUT

Department of CSE, Seshadripuram Institute of Technology


Mysuru-571311
Principles of Programming using C BPOPS103/203

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;

case 2: fnMyStrCat(acStr1, acStr2);


printf("\nConcatenated String is\n%s", acStr1);
break;

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);

Department of CSE, Seshadripuram Institute of Technology


Mysuru-571311
Principles of Programming using C BPOPS103/203

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++);

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;
dest_len = fnMyStrLen(dest);
for (i = 0 ; src[i] != '\0' ; i++)
dest[dest_len + i] = src[i];
dest[dest_len + i] = '\0';
}
int fnMyStrLen(const char *str)
{
int iLen;
for(iLen=0; str[iLen] != '\0'; iLen++);
return iLen;
}

Department of CSE, Seshadripuram Institute of Technology


Mysuru-571311
Principles of Programming using C BPOPS103/203

OUTPUT

Department of CSE, Seshadripuram Institute of Technology


Mysuru-571311
Principles of Programming using C BPOPS103/203

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;

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
Principles of Programming using C BPOPS103/203

printf("\n=========================================");
printf("\nName\t: %s", students[i].cName);
printf("\nUSN\t: %s", students[i].cUSN);
printf("\nMarks\t: %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
Principles of Programming using C BPOPS103/203

OUTPUT

Department of CSE, Seshadripuram Institute of Technology


Mysuru-571311
Principles of Programming using C BPOPS103/203

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("\nEnter the number of Values : ");


scanf("%d",&iNum);
fptr = faArray;
/* fptr = (float*)malloc(iNum*sizeof(float));*/
printf("\nEnter %d values\n", 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");

Department of CSE, Seshadripuram Institute of Technology


Mysuru-571311
Principles of Programming using C BPOPS103/203

printf("\tSum\t = \t%g\n\tMean\t = \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
Principles of Programming using C BPOPS103/203

OUTPUT

Department of CSE, Seshadripuram Institute of Technology


Mysuru-571311
Principles of Programming using C BPOPS103/203

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);
}

printf("\nEnter target File name\n");


scanf("%s",fname2);

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;
}

Department of CSE, Seshadripuram Institute of Technology


Mysuru-571311
Principles of Programming using C BPOPS103/203

OUTPUT

Department of CSE, Seshadripuram Institute of Technology


Mysuru-571311
Principles of Programming using C BPOPS103/203

OUTPUT

Department of CSE, Seshadripuram Institute of Technology


Mysuru-571311
Principles of Programming using C BPOPS103/203

OUTPUT

Department of CSE, Seshadripuram Institute of Technology


Mysuru-571311
Principles of Programming using C BPOPS103/203

OUTPUT

Department of CSE, Seshadripuram Institute of Technology


Mysuru-571311
Principles of Programming using C BPOPS103/203

OUTPUT

Department of CSE, Seshadripuram Institute of Technology


Mysuru-571311
Principles of Programming using C BPOPS103/203

OUTPUT

Department of CSE, Seshadripuram Institute of Technology


Mysuru-571311
Principles of Programming using C BPOPS103/203

OUTPUT

Department of CSE, Seshadripuram Institute of Technology


Mysuru-571311
Principles of Programming using C BPOPS103/203

OUTPUT

Department of CSE, Seshadripuram Institute of Technology


Mysuru-571311
Principles of Programming using C BPOPS103/203

OUTPUT

Department of CSE, Seshadripuram Institute of Technology


Mysuru-571311

You might also like