BPOPS103 Border
BPOPS103 Border
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
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 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)
{
OUTPUT
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);
}
}
printf("\n");
iSp_cnt--;
iNum_cnt += 2;
}
return 0;
}
OUTPUT
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
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++)
{
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");
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 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
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 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;
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
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("\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;
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);
if(students[i].iMarks < dAvg)
printf("\nThe student has scored below average\n");
else
printf("\nThe student has scored above average\n");
}
return 0;
}
OUTPUT
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
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
OUTPUT
OUTPUT
OUTPUT
OUTPUT
OUTPUT
OUTPUT
OUTPUT
OUTPUT