Top Banner
Free Software Movement Karnataka www.fsmk.org } C PROGRAMMING LABORATORY (18CPL17/27) I\II Semester (common to all branches) LAB MANUAL } Prabodh C P Asst Professor Dept of CSE, SIT Volunteer, FSMK This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. To veiw a copy of this license, visit https://creativecommons.org/licenses/by-nc-sa/4.0/
44

C PROGRAMMING LABORATORY (18CPL17/27)Finally you will be prompted to choose the compiler. Just choose the default options here (Dont change the options). You should be using GNU GCC

Oct 08, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: C PROGRAMMING LABORATORY (18CPL17/27)Finally you will be prompted to choose the compiler. Just choose the default options here (Dont change the options). You should be using GNU GCC

Free Software Movement Karnatakawww.fsmk.org }

C PROGRAMMING LABORATORY (18CPL17/27)

I\II Semester(common to all branches)

LAB MANUAL}Prabodh C PAsst Professor

Dept of CSE, SIT

Volunteer, FSMK

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.To veiw a copy of this license, visit https://creativecommons.org/licenses/by-nc-sa/4.0/

Page 2: C PROGRAMMING LABORATORY (18CPL17/27)Finally you will be prompted to choose the compiler. Just choose the default options here (Dont change the options). You should be using GNU GCC

Contents

I PART A 40.1 Using Code::Blocks to compile and execute a C Program . . . . . . . . . . . . . . . . . . . . . . . . 5

2 Commercial Calculator 112.1 C Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

3 Quadratic Equation 133.1 C Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

4 Palindrome Check 15

5 Electricity Bill 17

6 Binary Search 196.1 C Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

7 Prime Number Check 217.1 C Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

II PART B 23

7 Matrix Multiplication 24

8 Compute Sine of an Angle 28

9 String Operations 309.1 Question . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30

10 Bubble Sort 34

11 Square Root of a Number 36

12 C Structures 38

13 Pointers and Arrays 41

14 Recursion 43

2

Page 3: C PROGRAMMING LABORATORY (18CPL17/27)Finally you will be prompted to choose the compiler. Just choose the default options here (Dont change the options). You should be using GNU GCC

Listings

2.1 A02Calculator.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112.2 out1.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123.1 A03Quadratic.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133.2 out2.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144.1 A04Palindrome.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154.2 out3.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165.1 A05ElectricBill.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175.2 out4.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186.1 A06BinarySearch.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196.2 out5.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207.1 A07isPrimefn.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217.2 out6.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227.1 B08MatrixMul.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247.2 out7.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268.1 B09SineAngle.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288.2 out8.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299.1 B10StringFunctions.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309.2 out9.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3210.1 B11BubbleSort.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3410.2 out10.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3511.1 B12SquareRoot.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3611.2 out11.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3712.1 B13StudentStructure.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3812.2 out12.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3913.1 B14MeanVarianceSD.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4113.2 out13.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4214.1 B15BinarytoDecimal.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4314.2 out14.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44

3

Page 4: C PROGRAMMING LABORATORY (18CPL17/27)Finally you will be prompted to choose the compiler. Just choose the default options here (Dont change the options). You should be using GNU GCC

Part I

PART A

4

Page 5: C PROGRAMMING LABORATORY (18CPL17/27)Finally you will be prompted to choose the compiler. Just choose the default options here (Dont change the options). You should be using GNU GCC

CODE::BLOCKS TUTORIAL

0.1 Using Code::Blocks to compile and execute a C Program

To launch Code::Blocks IDE, Click onApplications → Programming → Code::Blocks IDEYou will get a window as shown below

5

Page 6: C PROGRAMMING LABORATORY (18CPL17/27)Finally you will be prompted to choose the compiler. Just choose the default options here (Dont change the options). You should be using GNU GCC

0.1. USING CODE::BLOCKS TO COMPILE AND EXECUTE A C PROGRAM

Code::Blocks creates what is called a Workspace to keep track of the project you are working on. It is possible foryou to be working on multiple projects within your workspace. A project is a collection of one or more source (aswell as header) files. Source files are the files that contain the source code for your program. If you are developinga C program, you are writing C source code (.c files).

First start a new Project by clicking on Create a new projectOR To create a project, click on the File pull-down menu, open New and then Project.You get this pop-upwindow

Choose Console applicationThen in the next window select the programming language C (and not C++)

In the next step give the Project Title and specify the folder where you want to save your project.(Note : Dont use any special characters or whitespaces for project title and folder names)

Dept of CSE, SIT - CPP 6

Page 7: C PROGRAMMING LABORATORY (18CPL17/27)Finally you will be prompted to choose the compiler. Just choose the default options here (Dont change the options). You should be using GNU GCC

0.1. USING CODE::BLOCKS TO COMPILE AND EXECUTE A C PROGRAM

Finally you will be prompted to choose the compiler. Just choose the default options here (Dont change theoptions). You should be using GNU GCC Compiler. Click Finish to create the new project.

The system will then return to the [MyProgram] window and you are ready to write your program.In theManagement area of the screen (Shift-F2 toggles the Management display), you will see the files that are part ofthe project in the Projects tab. To see the source files, click on the triangle symbol to expand the Workspace andits subdirectories.

Under Sources, there is a file called main.c, which is automatically created for you when you build a consoleapplication. Click on main.c.

Dept of CSE, SIT - CPP 7

Page 8: C PROGRAMMING LABORATORY (18CPL17/27)Finally you will be prompted to choose the compiler. Just choose the default options here (Dont change the options). You should be using GNU GCC

0.1. USING CODE::BLOCKS TO COMPILE AND EXECUTE A C PROGRAM

main.c contains a simple Hello World program which you can edit later to solve a programming problem. Nowlet us see how to compile and execute this main.c program. Just to understand the process of debugging weknowingly introduce an error in the program by removing the semicolon after the printf statement. We will nowcompile the program (To compile a file means to take the instructions that you have written and translate it intomachine code for the computer to understand).Compile the project from the Build pull-down menu by clicking on Build option[Ctrl+F9].

The error messages are shown in the Build messages window at the bottom. Let us now try to understandthese error messages. === Build: Debug in MyProgram (compiler: GNU GCC Compiler) ===main.c In function main:main.c 7 error: expected ; before returnmain.c 8 warning: control reaches end of non-void function [-Wreturn-type]=== Build failed: 1 error(s), 1 warning(s) (0 minute(s), 1 second(s)) ===

The error messages show the errors in syntax (and not the logical errors). It also indicates the line number andthe type of error. Here in this example the error says that before the return statement in line no 7 a semicolon ismissing. The next message is a warning message which has resulted because of the previous error.Now go to line number 6 and add a semicolon at the end. Now build your project again.

Dept of CSE, SIT - CPP 8

Page 9: C PROGRAMMING LABORATORY (18CPL17/27)Finally you will be prompted to choose the compiler. Just choose the default options here (Dont change the options). You should be using GNU GCC

0.1. USING CODE::BLOCKS TO COMPILE AND EXECUTE A C PROGRAM

Now the build message window shows the following message. 0 error(s), 0 warning(s) (0 minute(s), 0 second(s)This means that the errors and warnings have been successfully resolved. Now it is time to run the program. Youcan Execute the project from the Build pull-down menu by clicking on Run option[Ctrl+F10].

An output window pops displaying the output of the program. A greeting message Hello world! is printed onto the output console.

Dept of CSE, SIT - CPP 9

Page 10: C PROGRAMMING LABORATORY (18CPL17/27)Finally you will be prompted to choose the compiler. Just choose the default options here (Dont change the options). You should be using GNU GCC

0.1. USING CODE::BLOCKS TO COMPILE AND EXECUTE A C PROGRAM

Installing Code::Blocks IDE

If you want to install Code::Blocks IDE on your system, you can download it from its official webpage.http://www.codeblocks.org/It is available for Linux, Mac, Windows platforms.

On Debian/Ubuntu systems Code::Blocks can be installed by using the apt package manner. You have to beconnected to the internet for necessary files to be downloaded. Run the following command.$sudo apt install codeblocks

For Windows download the binary whose name is like codeblocks-17.12mingw-setup.exe

Dept of CSE, SIT - CPP 10

Page 11: C PROGRAMMING LABORATORY (18CPL17/27)Finally you will be prompted to choose the compiler. Just choose the default options here (Dont change the options). You should be using GNU GCC

Chapter 2

Commercial Calculator

Question

Develop a program to solve simple computational problems using arithmetic expressions anduse of each operator leading to simulation of a commercial calculator. (No built-in math func-tion)

2.1 C Code

1 /***************************************************************************2 *File : A02Calculator.c3 *Description : Program to simulate a commercial calculator4 *Author : Prabodh C P5 *Compiler : gcc compiler, Ubuntu 18.046 *Date : 16 August 20187 ***************************************************************************/8

9 #include<stdio.h>10 #include<stdlib.h>11 #include<math.h>12

13 /***************************************************************************14 *Function : main15 *Input parameters : no parameters16 *RETURNS : 0 on success17 ***************************************************************************/18

19 int main()20 {21 int iChoice, iOperand1, iOperand2;22 char cOperator;23

24 for(;;)25 {26 printf("\nEnter the arithmetic expression\n");27 scanf("%d%c%d", &iOperand1, &cOperator, &iOperand2);28 switch(cOperator)29 {30 case ’+’: printf("\nResult = %d", iOperand1 + iOperand2);31 break;32

33 case ’-’: printf("\nResult = %d", iOperand1 - iOperand2);34 break;35

36 case ’*’: printf("\nResult = %d", iOperand1 * iOperand2);37 break;

11

Page 12: C PROGRAMMING LABORATORY (18CPL17/27)Finally you will be prompted to choose the compiler. Just choose the default options here (Dont change the options). You should be using GNU GCC

2.1. C CODE CHAPTER 2. COMMERCIAL CALCULATOR

38

39 case ’/’: printf("\nResult = %g", (float)iOperand1 / iOperand2);40 break;41 case ’%’: printf("\nResult = %d", iOperand1 % iOperand2);42 break;43

44 }45 printf("\nPress 1 to continue and 0 to quit : ");46 scanf("%d", &iChoice);47 if(0==iChoice)48 {49 break;50 }51 }52 return 0;53 }

Listing 2.1: A02Calculator.c

Output

=================================

1 /***************************************2 Enter the arithmetic expression3 4+64

5 Result = 106 Press 1 to continue and 0 to quit : 17

8 Enter the arithmetic expression9 2-9

10

11 Result = -712 Press 1 to continue and 0 to quit : 113

14 Enter the arithmetic expression15 5*216

17 Result = 1018 Press 1 to continue and 0 to quit : 119

20 Enter the arithmetic expression21 4/522

23 Result = 0.824 Press 1 to continue and 0 to quit : 125

26 Enter the arithmetic expression27 8/428

29 Result = 230 Press 1 to continue and 0 to quit : 131

32 Enter the arithmetic expression33 15%434

35 Result = 336 Press 1 to continue and 0 to quit : 037 ***************************************/

Listing 2.2: out1.c

Dept of CSE, SIT - CPP 12

Page 13: C PROGRAMMING LABORATORY (18CPL17/27)Finally you will be prompted to choose the compiler. Just choose the default options here (Dont change the options). You should be using GNU GCC

Chapter 3

Quadratic Equation

Question

Develop a program to compute the roots of a quadratic equation by accepting the coefficients.Print appropriate messages.

3.1 C Code

1 /***************************************************************************2 *File : A03Quadratic.c3 *Description : Program to find the roots of a Quadratic Equation4 *Author : Prabodh C P5 *Compiler : gcc compiler, Ubuntu 18.046 *Date : 16 August 20187 ***************************************************************************/8

9 #include<stdio.h>10 #include<stdlib.h>11 #include<math.h>12 /***************************************************************************13 *Function : main14 *Input parameters : no parameters15 *RETURNS : 0 on success16 ***************************************************************************/17 int main(void)18 {19 float fA,fB,fC,fDesc,fX1,fX2,fRealp,fImagp;20 int iState;21

22 printf("\n*************************************************************");23 printf("\n*\tPROGRAM TO FIND ROOTS OF A QUADRATIC EQUATION\t *\n");24 printf("*************************************************************");25

26 printf("\nEnter the coefficients of a,b,c \n");27 scanf("%f%f%f",&fA,&fB,&fC);28 if(0 == fA)29 {30 printf("\nInvalid input, not a quadratic equation - try again\n");31 exit(0);32 }33

34 /*COMPUTE THE DESCRIMINANT*/35 fDesc = fB * fB - 4 * fA * fC;36 ((0 == fDesc) ? (iState = 1):((fDesc > 0) ? (iState = 2) : (iState = 3)));37 switch(iState)38 {

13

Page 14: C PROGRAMMING LABORATORY (18CPL17/27)Finally you will be prompted to choose the compiler. Just choose the default options here (Dont change the options). You should be using GNU GCC

3.1. C CODE CHAPTER 3. QUADRATIC EQUATION

39 case 1:40 fX1 = fX2 = -fB/(2*fA);41 printf("\nRoots are equal and the Roots are \n");42 printf("\nRoot1 = %g and Root2 = %g\n",fX1,fX2);43 break;44 case 2:45 fX1 = (-fB+sqrt(fDesc))/(2*fA);46 fX2 = (-fB-sqrt(fDesc))/(2*fA);47 printf("\nThe Roots are Real and distinct, they are \n");48 printf("\nRoot1 = %g and Root2 = %g\n",fX1,fX2);49 break;50 case 3:51 fRealp = -fB / (2*fA);52 fImagp = sqrt(fabs(fDesc))/(2*fA);53 printf("\nThe Roots are imaginary and they are\n");54 printf("\nRoot1 = %g+i%g\n",fRealp,fImagp);55 printf("\nRoot2 = %g-i%g\n",fRealp,fImagp);56 }57 return 0;58 }

Listing 3.1: A03Quadratic.c

Output

1 /***************************************2 *************************************************************3 * PROGRAM TO FIND ROOTS OF A QUADRATIC EQUATION *4 *************************************************************5 Enter the coefficients of a,b,c6 1 -5 67

8 The Roots are Real and distinct, they are9

10 Root1 = 3 and Root2 = 211 *************************************************************12 * PROGRAM TO FIND ROOTS OF A QUADRATIC EQUATION *13 *************************************************************14 Enter the coefficients of a,b,c15 1 4 416

17 Roots are equal and the Roots are18

19 Root1 = -2 and Root2 = -220 *************************************************************21 * PROGRAM TO FIND ROOTS OF A QUADRATIC EQUATION *22 *************************************************************23 Enter the coefficients of a,b,c24 1 3 325

26 The Roots are imaginary and they are27

28 Root1 = -1.5+i0.866025 Root2 = -1.5-i0.86602529 *************************************************************30 * PROGRAM TO FIND ROOTS OF A QUADRATIC EQUATION *31 *************************************************************32 Enter the coefficients of a,b,c33 0 1 234

35 Invalid input, not a quadratic equation - try again36 ***************************************/

Listing 3.2: out2.c

Dept of CSE, SIT - CPP 14

Page 15: C PROGRAMMING LABORATORY (18CPL17/27)Finally you will be prompted to choose the compiler. Just choose the default options here (Dont change the options). You should be using GNU GCC

Chapter 4

Palindrome Check

Question

Develop a program to find the reverse of a positive integer and check for palindrome or not.Display appropriate messages.

C Code

1 /***************************************************************************2 *File : A04Palindrome.c3 *Description : Program to check whether the given integer is a Palindrome or

not4 *Author : Prabodh C P5 *Compiler : gcc compiler, Ubuntu 18.046 *Date : 16 August 20187 ***************************************************************************/8

9 #include<stdio.h>10 #include<stdlib.h>11

12 /***************************************************************************13 *Function : main14 *Input parameters : no parameters15 *RETURNS : 0 on success16 ***************************************************************************/17

18 int main(void)19 {20 int iNum,iRev = 0,iTemp,iRem;21

22 printf("\n**************************************************************************");

23 printf("\n*\tPROGRAM TO CHECK WHETHER AN INTEGER IS A PALINDROME OR NOT\t *\n");

24 printf("**************************************************************************");

25

26 printf("\nEnter a number\n");27 scanf("%d",&iNum);28

29 iTemp = iNum;30

31 while(iNum!=0)32 {33 iRem = iNum % 10;34 iRev = iRev * 10 + iRem;

15

Page 16: C PROGRAMMING LABORATORY (18CPL17/27)Finally you will be prompted to choose the compiler. Just choose the default options here (Dont change the options). You should be using GNU GCC

CHAPTER 4. PALINDROME CHECK

35 iNum = iNum/10;36 }37 printf("\nReverse is %d",iRev);38

39 if(iRev == iTemp)40 printf("\nNumber %d is a palindrome\n",iTemp);41 else42 printf("\nNumber %d is not a palindrome\n",iTemp);43

44 return 0;45 }

Listing 4.1: A04Palindrome.c

Output

1 /***************************************2 **************************************************************************3 * PROGRAM TO CHECK WHETHER AN INTEGER IS A PALINDROME OR NOT *4 **************************************************************************5 Enter a number6 4567

8 Reverse is 6549 Number 456 is not a palindrome

10

11 **************************************************************************12 * PROGRAM TO CHECK WHETHER AN INTEGER IS A PALINDROME OR NOT *13 **************************************************************************14 Enter a number15 4565416

17 Reverse is 4565418 Number 45654 is a palindrome19

20 **************************************************************************21 * PROGRAM TO CHECK WHETHER AN INTEGER IS A PALINDROME OR NOT *22 **************************************************************************23 Enter a number24 34325

26 Reverse is 34327 Number 343 is a palindrome28

29

30 Infix Expression is (a*(b-c)/(a+b*c))31

32 Postfix Expression is abc-*abc*+/33

34 ***************************************/

Listing 4.2: out3.c

Dept of CSE, SIT - CPP 16

Page 17: C PROGRAMMING LABORATORY (18CPL17/27)Finally you will be prompted to choose the compiler. Just choose the default options here (Dont change the options). You should be using GNU GCC

Chapter 5

Electricity Bill

Question

An electricity board charges the following rates for the use of electricity: for the first 200 units80 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 thanRs 400, then an additional surcharge of 15% of total amount is charged. Write a program toread the name of the user, number of units consumed and print out the charges.

C Code

1 /***************************************************************************2 *File : A05ElectricBill.c3 *Description : Program to perform a binary search on 1D Array4 *Author : Prabodh C P5 *Compiler : gcc compiler, Ubuntu 18.046 *Date : 16 August 20187 ***************************************************************************/8 #include<stdio.h>9 #include<stdlib.h>

10 /***************************************************************************11 *Function : main12 *Input parameters : no parameters13 *RETURNS : 0 on success14 ***************************************************************************/15 int main(void)16 {17 char cName[30];18 int iUnits;19 const int iMinCharge = 100;20 const double dSlab1 = 0.8;21 const double dSlab2 = 0.9;22 const double dSlab3 = 1.0;23 const double dSurcharge = 0.15;24 double dBillAmount = 0.0;25

26 printf("\nEnter the name of the customer : "); scanf("%s", cName);27 printf("\nEnter the number of units consumed : "); scanf("%d", &iUnits);28 dBillAmount += iMinCharge;29 if(iUnits <= 200)30 {31 dBillAmount += iUnits*dSlab1;32 }33 else if(iUnits > 200 && iUnits <= 300)34 {35 dBillAmount += (200*dSlab1)+((iUnits-200)*dSlab2);

17

Page 18: C PROGRAMMING LABORATORY (18CPL17/27)Finally you will be prompted to choose the compiler. Just choose the default options here (Dont change the options). You should be using GNU GCC

CHAPTER 5. ELECTRICITY BILL

36 }37 else38 {39 dBillAmount += (200*dSlab1)+(100*dSlab2)+((iUnits-300)*dSlab3);40 }41 if(dBillAmount > 400)42 {43 dBillAmount += dBillAmount * dSurcharge;44 }45 printf("\nElectricity Bill\n===================================");46 printf("\nCustomer Name\t: %s", cName);47 printf("\nUnits Consumed\t: %d", iUnits);48 printf("\nBill Amount\t: %0.2lf Rupees\n\n", dBillAmount);49 return 0;50 }

Listing 5.1: A05ElectricBill.c

Output

1 /***************************************2 Enter the name of the customer : Ramesh3 Enter the number of units consumed : 4574

5 Electricity Bill6 ===================================7 Customer Name : Ramesh8 Units Consumed : 4579 Bill Amount : 583.05 Rupees

10

11 Enter the name of the customer : Sayeed12 Enter the number of units consumed : 15013

14 Electricity Bill15 ===================================16 Customer Name : Sayeed17 Units Consumed : 15018 Bill Amount : 220.00 Rupees19

20 Enter the name of the customer : Jaswinder21 Enter the number of units consumed : 30022

23 Electricity Bill24 ===================================25 Customer Name : Jaswinder26 Units Consumed : 30027 Bill Amount : 350.00 Rupees28

29 Enter the name of the customer : Shyam30 Enter the number of units consumed : 18231

32 Electricity Bill33 ===================================34 Customer Name : Shyam35 Units Consumed : 18236 Bill Amount : 245.60 Rupees37 ***************************************/

Listing 5.2: out4.c

Dept of CSE, SIT - CPP 18

Page 19: C PROGRAMMING LABORATORY (18CPL17/27)Finally you will be prompted to choose the compiler. Just choose the default options here (Dont change the options). You should be using GNU GCC

Chapter 6

Binary Search

Question

Introduce 1D Array manipulation and implement Binary search.

6.1 C Code

=================================

1 /***************************************************************************2 *File : A06BinarySearch.c3 *Description : Program to perform a binary search on 1D Array4 *Author : Prabodh C P5 *Compiler : gcc compiler, Ubuntu 18.046 *Date : 16 August 20187 ***************************************************************************/8

9 #include<stdio.h>10 #include<stdlib.h>11

12 /***************************************************************************13 *Function : main14 *Input parameters : no parameters15 *RETURNS : 0 on success16 ***************************************************************************/17

18 int main(void)19 {20 int iaArr[100],iNum,i,iMid,iLow,iHigh,iFound,iKey;21

22 printf("\nEnter the number of elements\n");23 scanf("%d",&iNum);24

25 printf("\nEnter the elements in ascending order\n");26 for(i=0;i<iNum;i++)27 scanf("%d",&iaArr[i]);28

29 printf("\nEnter the key element\n");30 scanf("%d",&iKey);31

32 iFound = 0;33 iLow = 0;34 iHigh = iNum-1;35 while(iLow <= iHigh)36 {37 iMid = (iLow+iHigh)/2;38 if(iKey == iaArr[iMid])

19

Page 20: C PROGRAMMING LABORATORY (18CPL17/27)Finally you will be prompted to choose the compiler. Just choose the default options here (Dont change the options). You should be using GNU GCC

6.1. C CODE CHAPTER 6. BINARY SEARCH

39 {40 iFound = 1;41 break;42 }43 else if(iKey < iaArr[iMid])44 {45 iHigh = iMid-1;46 }47 else48 {49 iLow = iMid+1;50 }51 }52

53 if(iFound)54 printf("\nKey element %d found at position %d\n",iKey,iMid+1);55 else56 printf("\nKey element not found\n");57

58 return 0;59 }

Listing 6.1: A06BinarySearch.c

Output

=================================

1 /****************************2 Enter the number of elements3 54

5 Enter the elements in ascending order6 1 3 5 7 97

8 Enter the key element9 8

10

11 Key element not found12 ===============================================13 Enter the number of elements14 415

16 Enter the elements in ascending order17 1 4 5 718

19 Enter the key element20 521

22 Key element 5 found at position 323 ===============================================24 Enter the number of elements25 626 Enter the elements in ascending order27 2 4 6 8 9 1028

29 Enter the key element30 931

32 Key element 9 found at position 533 ***************************************/

Listing 6.2: out5.c

Dept of CSE, SIT - CPP 20

Page 21: C PROGRAMMING LABORATORY (18CPL17/27)Finally you will be prompted to choose the compiler. Just choose the default options here (Dont change the options). You should be using GNU GCC

Chapter 7

Prime Number Check

Question

Implement using functions to check whether the given number is prime and display appropri-ate messages. (No built-in math function)

7.1 C Code

1 /***************************************************************************2 *File : A07isPrime.c3 *Description : Program to check whether the given integer is a Prime or not4 *Author : Prabodh C P5 *Compiler : gcc compiler, Ubuntu 18.046 *Date : 16 August 20187 ***************************************************************************/8 #include<stdio.h>9 #include<stdlib.h>

10 #include<stdbool.h>11

12 bool fnisPrime(int);13 /***************************************************************************14 *Function : main15 *Input parameters : no parameters16 *RETURNS : 0 on success17 ***************************************************************************/18 int main(void)19 {20 int iVal,iFlag;21 printf("\n******************************************************");22 printf("\n*\tPROGRAM TO IMPLEMENT TEST OF PRIMALITY\t *\n");23 printf("******************************************************");24

25 printf("\nEnter the value to be checked\n");26 scanf("%d",&iVal);27

28 iFlag = fnisPrime(iVal);29 if(iFlag)30 {31 printf("\nThe entered value %d is a prime number\n",iVal);32 }33 else34 {35 printf("\nThe entered value %d is not a prime number\n",iVal);36 }37 return 0;38 }

21

Page 22: C PROGRAMMING LABORATORY (18CPL17/27)Finally you will be prompted to choose the compiler. Just choose the default options here (Dont change the options). You should be using GNU GCC

7.1. C CODE CHAPTER 7. PRIME NUMBER CHECK

39 /***************************************************************************40 *Function : fnisPrime41 *Description : Function to check whether a number is prime or not42 *Input parameters :43 * int iX - value to be checked whether prime or not44 *RETURNS :45 true if the number is prime and false otherwise46 ***************************************************************************/47 bool fnisPrime(int iX)48 {49 int i;50 if(1 == iX)51 {52 printf("\n1 is neither prime nor composite\n");53 exit(0);54 }55 for(i = 2; i*i <= iX ;i++)56 {57 if( 0 == iX % i )58 {59 return false;60 }61 }62 return true;63 }

Listing 7.1: A07isPrimefn.c

Output

1 /***************************************2 ******************************************************3 * PROGRAM TO IMPLEMENT TEST OF PRIMALITY *4 ******************************************************5 Enter the value to be checked6 457 The entered value 45 is not a prime number8

9 ******************************************************10 * PROGRAM TO IMPLEMENT TEST OF PRIMALITY *11 ******************************************************12 Enter the value to be checked13 4114 The entered value 41 is a prime number15

16 ******************************************************17 * PROGRAM TO IMPLEMENT TEST OF PRIMALITY *18 ******************************************************19 Enter the value to be checked20 1721 The entered value 17 is a prime number22

23 ******************************************************24 * PROGRAM TO IMPLEMENT TEST OF PRIMALITY *25 ******************************************************26 Enter the value to be checked27 1528 The entered value 15 is not a prime number29

30 ***************************************/

Listing 7.2: out6.c

Dept of CSE, SIT - CPP 22

Page 23: C PROGRAMMING LABORATORY (18CPL17/27)Finally you will be prompted to choose the compiler. Just choose the default options here (Dont change the options). You should be using GNU GCC

Part II

PART B

23

Page 24: C PROGRAMMING LABORATORY (18CPL17/27)Finally you will be prompted to choose the compiler. Just choose the default options here (Dont change the options). You should be using GNU GCC

Chapter 7

Matrix Multiplication

Question

Develop a program to introduce 2D Array manipulation and implement Matrix multiplicationand ensure the rules of multiplication are checked.

C Code

1 /***************************************************************************2 *File : B08MatrixMul.c3 *Description : Program to implement Matrix Multiplication4 *Author : Prabodh C P5 *Compiler : gcc compiler, Ubuntu 18.046 *Date : 16 August 20187 ***************************************************************************/8

9 #include<stdio.h>10 #include<stdlib.h>11

12 /***************************************************************************13 *Function : main14 *Input parameters : no parameters15 *RETURNS : 0 on success16 ***************************************************************************/17

18 int main(void)19 {20 int iM, iN, iP, iQ, i, j, k, iaMat1[10][10], iaMat2[10][10];21 int iaProd[10][10] = {0};22

23

24 printf("\n*********************************************************");25 printf("\n*\tPROGRAM TO IMPLEMENT MATRIX MULIPLICATION\t*\n");26 printf("*********************************************************");27

28 printf("\nEnter the order of Matrix1\n");29 scanf("%d%d",&iM,&iN);30

31 printf("\nEnter the order of Matrix2\n");32 scanf("%d%d",&iP,&iQ);33

34 if( iN != iP)35 {36 printf("\nMatrix Multiplication not possible\n");37 exit(0);38 }39

24

Page 25: C PROGRAMMING LABORATORY (18CPL17/27)Finally you will be prompted to choose the compiler. Just choose the default options here (Dont change the options). You should be using GNU GCC

CHAPTER 7. MATRIX MULTIPLICATION

40

41 printf("\nEnter the elements of Matrix 1\n");42 for(i=0;i<iM;i++)43 for(j=0;j<iN;j++)44 scanf("%d",&iaMat1[i][j]);45

46 printf("\nEnter the elements of Matrix 2\n");47 for(i=0;i<iP;i++)48 for(j=0;j<iQ;j++)49 scanf("%d",&iaMat2[i][j]);50

51

52 for(i=0;i<iM;i++)53 {54 for(j=0;j<iQ;j++)55 {56 for(k=0;k<iN;k++)57 {58 iaProd[i][j] += iaMat1[i][k] * iaMat2[k][j];59 }60 }61 }62

63 /**************************************************************************************************

64 |*| |*|65 a00 a01 a02|*|b00 b01 b02|*|66 |*| |*|67 a10 a11 a12|*|b10 b11 b12|*|68 |*| |*|69 a20 a21 a22|*|b20 b21 b22|*|70 |*| |*|71

72 (a00*b00+a01*b10+a02*b20) (a00*b01+a01*b11+a02*b21) (a00*b02+a01*b12+a02*b22)73 (a10*b00+a11*b10+a12*b20) (a10*b01+a11*b11+a12*b21) (a10*b02+a11*b12+a12*b22)74 (a20*b00+a21*b10+a22*b20) (a20*b01+a21*b11+a22*b21) (a20*b02+a21*b12+a22*b22)75 *************************************************************************************************

*/76

77

78 printf("\nMatrix 1\n");79

80 for(i=0;i<iM;i++)81 {82 for(j=0;j<iN;j++)83 {84 printf("%d\t",iaMat1[i][j]);85 }86 printf("\n");87 }88 printf("\n");89

90 printf("\nMatrix 2\n");91

92 for(i=0;i<iP;i++)93 {94 for(j=0;j<iQ;j++)95 {96 printf("%d\t",iaMat2[i][j]);97 }98 printf("\n");

Dept of CSE, SIT - CPP 25

Page 26: C PROGRAMMING LABORATORY (18CPL17/27)Finally you will be prompted to choose the compiler. Just choose the default options here (Dont change the options). You should be using GNU GCC

CHAPTER 7. MATRIX MULTIPLICATION

99 }100 printf("\n");101

102 printf("\nThe Product matrix is is \n");103

104 for(i=0;i<iM;i++)105 {106 for(j=0;j<iQ;j++)107 {108 printf("%d\t",iaProd[i][j]);109 }110 printf("\n");111 }112 printf("\n");113 return 0;114 }

Listing 7.1: B08MatrixMul.c

Output

=================================

1 /***************************************2

3 *********************************************************4 * PROGRAM TO IMPLEMENT MATRIX MULIPLICATION *5 *********************************************************6 Enter the order of Matrix17 2 38

9 Enter the order of Matrix210 4 511

12 Matrix Multiplication not possible13

14 *********************************************************15 * PROGRAM TO IMPLEMENT MATRIX MULIPLICATION *16 *********************************************************17 Enter the order of Matrix118 2 319

20 Enter the order of Matrix221 3 222

23 Enter the elements of Matrix 124 1 2 325 4 5 626

27 Enter the elements of Matrix 228 1 229 3 430 5 631

32 Matrix 133 1 2 334 4 5 635

36 Matrix 237 1 238 3 439 5 640

Dept of CSE, SIT - CPP 26

Page 27: C PROGRAMMING LABORATORY (18CPL17/27)Finally you will be prompted to choose the compiler. Just choose the default options here (Dont change the options). You should be using GNU GCC

CHAPTER 7. MATRIX MULTIPLICATION

41 The Product matrix is is42 22 2843 49 6444

45 *********************************************************46 * PROGRAM TO IMPLEMENT MATRIX MULIPLICATION *47 *********************************************************48 Enter the order of Matrix149 2 250

51 Enter the order of Matrix252 2 253

54 Enter the elements of Matrix 155 1 256 3 457

58 Enter the elements of Matrix 259 1 060 0 161

62 Matrix 163 1 264 3 465

66 Matrix 267 1 068 0 169

70 The Product matrix is is71 1 272 3 473 ***************************************/

Listing 7.2: out7.c

Dept of CSE, SIT - CPP 27

Page 28: C PROGRAMMING LABORATORY (18CPL17/27)Finally you will be prompted to choose the compiler. Just choose the default options here (Dont change the options). You should be using GNU GCC

Chapter 8

Compute Sine of an Angle

Question

Develop a Program to compute Sin(x) using Taylor series approximation. Compare your resultwith the built- in Library function. Print both the results with appropriate messages.

C Code

1 /***************************************************************************2 *File : B09SineAngle.c3 *Description : Program to calculate Sin(x) using Taylor series4 *Author : Prabodh C P5 *Compiler : gcc compiler, Ubuntu 18.046 *Date : 16 August 20187 ***************************************************************************/8 #include<stdio.h>9 #include<stdlib.h>

10 #include <math.h>11 /***************************************************************************12 *Function : main13 *Input parameters : no parameters14 *RETURNS : 0 on success15 ***************************************************************************/16 int main()17 {18 float fAngD, fAngR;19 float fTerm, fNum, fDen, fVal;20 int i,iNum;21 printf("\nEnter the Angle : "); scanf("%f",&fAngD);22 printf("\nEnter the Number of terms : "); scanf("%d",&iNum);23 printf("\nInput Angle = %g\n",fAngD);24 printf("No of terms = %d\n",iNum);25

26 fAngR= (fAngD*M_PI)/180 ;27 fNum=fAngR;28 fDen=1.0;29 fVal =0.0;30 fTerm=fNum/fDen;31 for(i=1;i<=iNum;i++)32 {33 fVal = fVal + fTerm;34 fNum = -fNum * fAngR * fAngR ;35 fDen = fDen * (2*i) * (2*i+1);36 fTerm = fNum/fDen;37 }38 printf("\nCalculated value is :\nSin(%g) = %g\n",fAngD,fVal);39 printf("\nBuilt In function value is :\nSin(%g) = %g\n",fAngD, sin(fAngR));

28

Page 29: C PROGRAMMING LABORATORY (18CPL17/27)Finally you will be prompted to choose the compiler. Just choose the default options here (Dont change the options). You should be using GNU GCC

CHAPTER 8. COMPUTE SINE OF AN ANGLE

40 return 0;41 }

Listing 8.1: B09SineAngle.c

Output

=================================

1 /***************************************2 Enter the Angle : 603 Enter the Number of terms : 34

5 Input Angle = 606 No of terms = 37

8 Calculated value is :9 Sin(60) = 0.866295

10

11 Built In function value is :12 Sin(60) = 0.86602513

14 =========================================15 Enter the Angle : 6016 Enter the Number of terms : 917

18 Input Angle = 6019 No of terms = 920

21 Calculated value is :22 Sin(60) = 0.86602523

24 Built In function value is :25 Sin(60) = 0.86602526

27 =========================================28 Enter the Angle : 3029 Enter the Number of terms : 230

31 Input Angle = 3032 No of terms = 233

34 Calculated value is :35 Sin(30) = 0.49967436

37 Built In function value is :38 Sin(30) = 0.539

40 =========================================41 Enter the Angle : 3042 Enter the Number of terms : 843

44 Input Angle = 3045 No of terms = 846

47 Calculated value is :48 Sin(30) = 0.549

50 Built In function value is :51 Sin(30) = 0.552 ***************************************/

Listing 8.2: out8.c

Dept of CSE, SIT - CPP 29

Page 30: C PROGRAMMING LABORATORY (18CPL17/27)Finally you will be prompted to choose the compiler. Just choose the default options here (Dont change the options). You should be using GNU GCC

Chapter 9

String Operations

9.1 Question

=================================Write functions to implement string operations such as compare, concatenate, string length.

Convince the parameter passing techniques.

C Code

1 /***************************************************************************2 *File : B10StringFunctions.c3 *Description : Program to implement string operations as functions4 *Author : Prabodh C P5 *Compiler : gcc compiler, Ubuntu 18.046 *Date : 16 August 20187 ***************************************************************************/8

9 #include<stdio.h>10 #include<stdlib.h>11

12 int fnMyStrCmp(const char*, const char*);13 void fnMyStrCat(char*, const char*);14 int fnMyStrLen(const char*);15

16 /***************************************************************************17 *Function : main18 *Input parameters : no parameters19 *RETURNS : 0 on success20 ***************************************************************************/21

22 int main()23 {24 int iChoice;25 char acStr1[30], acStr2[30];26 int iLen;27 printf("\n=====================\n");28 printf("STRING OPERATIONS");29 printf("\n=====================\n");30 for(;;)31 {32 printf("\nEnter two strings\n");33 printf("\nString 1 : "); scanf("%s", acStr1);34 printf("\nString 2 : "); scanf("%s", acStr2);35 printf("\n1.String Compare\n2.String Concatenate\n3.String Length");36 printf("\nEnter your choice : "); scanf("%d", &iChoice);37 switch(iChoice)

30

Page 31: C PROGRAMMING LABORATORY (18CPL17/27)Finally you will be prompted to choose the compiler. Just choose the default options here (Dont change the options). You should be using GNU GCC

9.1. QUESTION CHAPTER 9. STRING OPERATIONS

38 {39 case 1: if(fnMyStrCmp(acStr1, acStr2) == 0)40 printf("\nTwo strings are equal");41 else if(fnMyStrCmp(acStr1, acStr2) > 0)42 printf("\nString %s is greater than String %s", acStr1,

acStr2);43 else44 printf("\nString %s is greater than String %s", acStr2,

acStr1);45 break;46

47 case 2: fnMyStrCat(acStr1, acStr2);48 printf("\nConcatenated String is\n%s", acStr1);49 break;50

51 case 3: iLen = fnMyStrLen(acStr1);52 printf("\nLength of String %s is %d\n", acStr1, iLen);53 iLen = fnMyStrLen(acStr2);54 printf("\nLength of String %s is %d\n", acStr2, iLen);55 break;56

57 }58 printf("\nPress 1 to continue and 0 to quit : ");59 scanf("%d", &iChoice);60 if(0==iChoice)61 {62 break;63 }64 }65 return 0;66 }67

68 /***************************************************************************69 *Function : fnMyStrCmp70 *Description : Function that compares the two strings s1 and s2.71 *Input parameters :72 * const char *s1, const char *s2 - two strings to be compared73 *RETURNS :74 * 1 if s1 is greater than s2.75 * 0 if s1 matches s2.76 * -1 if s1 is less than s2.77 ***************************************************************************/78

79 int fnMyStrCmp(const char *s1, const char *s2)80 {81 int k;82 for(k=0; s1[k] == s2[k] && s1[k]!=’\0’&& s2[k]!=’\0’; k++);83

84 if( k==(fnMyStrLen(s1)) && k==(fnMyStrLen(s2)) )85 {86 return 0;87 }88 else if(s1[k] > s2[k])89 {90 return 1;91 }92 else93 {94 return -1;95 }96 }97

Dept of CSE, SIT - CPP 31

Page 32: C PROGRAMMING LABORATORY (18CPL17/27)Finally you will be prompted to choose the compiler. Just choose the default options here (Dont change the options). You should be using GNU GCC

9.1. QUESTION CHAPTER 9. STRING OPERATIONS

98 /***************************************************************************99 *Function : fnMyStrCat

100 *Description : function that appends the src string to the dest string101 *Input parameters :102 * char *dest - first string103 * const char *src - second string104 *RETURNS : nothing105 ***************************************************************************/106

107 void fnMyStrCat(char *dest, const char *src)108 {109 int dest_len, i;110 dest_len = fnMyStrLen(dest);111 for (i = 0 ; src[i] != ’\0’ ; i++)112 dest[dest_len + i] = src[i];113 dest[dest_len + i] = ’\0’;114 }115

116 /***************************************************************************117 *Function : fnMyStrLen118 *Description : function that calculates the length of a string119 *Input parameters :120 * const char *str - string whose length needs to be found121 *RETURNS :122 * integer which is the length of the string123 ***************************************************************************/124

125 int fnMyStrLen(const char *str)126 {127 int iLen;128 for(iLen=0; str[iLen] != ’\0’; iLen++);129 return iLen;130 }

Listing 9.1: B10StringFunctions.c

Output

=================================

1 /***************************************2 =====================3 STRING OPERATIONS4 =====================5

6 Enter two strings7

8 String 1 : shiva9

10 String 2 : shankar11

12 1.String Compare13 2.String Concatenate14 3.String Length15 Enter your choice : 216

17 Concatenated String is18 shivashankar19 Press 1 to continue and 0 to quit : 120

21 Enter two strings22

23 String 1 : ramesh

Dept of CSE, SIT - CPP 32

Page 33: C PROGRAMMING LABORATORY (18CPL17/27)Finally you will be prompted to choose the compiler. Just choose the default options here (Dont change the options). You should be using GNU GCC

9.1. QUESTION CHAPTER 9. STRING OPERATIONS

24

25 String 2 : sumesh26

27 1.String Compare28 2.String Concatenate29 3.String Length30 Enter your choice : 131

32 String sumesh is greater than String ramesh33 Press 1 to continue and 0 to quit : 134

35 Enter two strings36

37 String 1 : sam38

39 String 2 : samantha40

41 1.String Compare42 2.String Concatenate43 3.String Length44 Enter your choice : 345

46 Length of String sam is 347

48 Length of String samantha is 849

50 Press 1 to continue and 0 to quit : 051 ***************************************/

Listing 9.2: out9.c

Dept of CSE, SIT - CPP 33

Page 34: C PROGRAMMING LABORATORY (18CPL17/27)Finally you will be prompted to choose the compiler. Just choose the default options here (Dont change the options). You should be using GNU GCC

Chapter 10

Bubble Sort

Question

Develop a program to sort the given set of N numbers using Bubble sort.

C Code

1 /***************************************************************************2 *File : B11BubbleSort.c3 *Description : Program to implement Bubble Sort Algorithm4 *Author : Prabodh C P5 *Compiler : gcc compiler, Ubuntu 18.046 *Date : 16 August 20187 ***************************************************************************/8

9 #include<stdio.h>10 #include<stdlib.h>11

12 /***************************************************************************13 *Function : main14 *Input parameters : no parameters15 *RETURNS : 0 on success16 ***************************************************************************/17

18 int main(void)19 {20 int iNum, i, j, iaArr[10], iTemp;21

22

23 printf("\n*************************************************");24 printf("\n*\tPROGRAM TO IMPLEMENT BUBBLE SORT\t*\n");25 printf("*************************************************");26

27 printf("\nEnter no of elements\n");28 scanf("%d",&iNum);29

30 printf("\nEnter the elements\n");31 for(i=0;i<iNum;i++)32 scanf("%d",&iaArr[i]);33

34 for(i=0;i<iNum;i++)35 {36 for(j=0;j<iNum-i-1;j++)37 {38 if(iaArr[j] > iaArr[j+1])39 {40 iTemp = iaArr[j];

34

Page 35: C PROGRAMMING LABORATORY (18CPL17/27)Finally you will be prompted to choose the compiler. Just choose the default options here (Dont change the options). You should be using GNU GCC

CHAPTER 10. BUBBLE SORT

41 iaArr[j] = iaArr[j+1];42 iaArr[j+1] = iTemp;43 }44

45 /*Code to show the program trace*/46 /* printf("\nIteration i=%d, j=%d\n",i,j);*/47 /* for(k=0;k<iNum;k++)*/48 /* printf("%d\t",iaArr[k]);*/49 }50

51 }52

53 printf("\nThe Sorted array is \n");54

55 for(i=0;i<iNum;i++)56 printf("%d\t",iaArr[i]);57

58 printf("\n");59 return 0;60 }

Listing 10.1: B11BubbleSort.c

Output

=================================

1 /***************************************2 *************************************************3 * PROGRAM TO IMPLEMENT BUBBLE SORT *4 *************************************************5 Enter no of elements6 57

8 Enter the elements9 2 1 6 5 7

10

11 The Sorted array is12 1 2 5 6 713

14 *************************************************15 * PROGRAM TO IMPLEMENT BUBBLE SORT *16 *************************************************17 Enter no of elements18 619

20 Enter the elements21 9 7 5 3 1 022

23 The Sorted array is24 0 1 3 5 7 925 ***************************************/

Listing 10.2: out10.c

Dept of CSE, SIT - CPP 35

Page 36: C PROGRAMMING LABORATORY (18CPL17/27)Finally you will be prompted to choose the compiler. Just choose the default options here (Dont change the options). You should be using GNU GCC

Chapter 11

Square Root of a Number

Question

Develop a program to find the square root of a given number N and execute for all possibleinputs with appropriate messages. Note: Don’t use library function sqrt(n).

C Code

1 /***************************************************************************2 *File : B12SquareRoot.c3 *Description : Program to find the square root of a given number4 *Author : Prabodh C P5 *Compiler : gcc compiler, Ubuntu 18.046 *Date : 16 August 20187 ***************************************************************************/8

9 #include<stdio.h>10 #include<stdlib.h>11 #include <math.h>12 #include <float.h>13 /***************************************************************************14 *Function : main15 *Input parameters : no parameters16 *RETURNS : 0 on success17 ***************************************************************************/18 int main()19 {20 float fVal, fNextGuess, fLastGuess = 1.0f, fDiff ;21 printf("\nEnter a value whose square root has to be calculated\n");22 scanf("%f", &fVal);23 do24 {25 fNextGuess = 0.5 * (fLastGuess + (fVal/fLastGuess));26 fDiff = fabs(fNextGuess - fLastGuess);27 fLastGuess = fNextGuess;28 }while (fDiff > FLT_EPSILON);29 /* }while (fDiff > 0.0001);*/30

31 printf("\nSquare root of %g = %g\n", fVal, fNextGuess);32 //Comparison with built in function33 //printf("\nSquare root of %g = %g\n", fVal, sqrt(fVal));34 return 0;35 }

Listing 11.1: B12SquareRoot.c

36

Page 37: C PROGRAMMING LABORATORY (18CPL17/27)Finally you will be prompted to choose the compiler. Just choose the default options here (Dont change the options). You should be using GNU GCC

CHAPTER 11. SQUARE ROOT OF A NUMBER

Output

=================================

1 /***************************************2 Enter a value whose square root has to be calculated3 454

5 Square root of 45 = 6.70826 =====================================================7 Enter a value whose square root has to be calculated8 499

10 Square root of 49 = 711 =====================================================12 Enter a value whose square root has to be calculated13 214

15 Square root of 2 = 1.4142116 =====================================================17 Enter a value whose square root has to be calculated18 5619

20 Square root of 56 = 7.4833121

22 ***************************************/

Listing 11.2: out11.c

Dept of CSE, SIT - CPP 37

Page 38: C PROGRAMMING LABORATORY (18CPL17/27)Finally you will be prompted to choose the compiler. Just choose the default options here (Dont change the options). You should be using GNU GCC

Chapter 12

C Structures

Question

Implement structures to read, write and compute average- marks and the students scoringabove and below the average marks for a class of N students.

C Code

1 /***************************************************************************2 *File : B13StudentStructure.c3 *Description : Program to implement structure and compute average marks4 *Author : Prabodh C P5 *Compiler : gcc compiler, Ubuntu 18.046 *Date : 16 August 20187 ***************************************************************************/8

9 #include<stdio.h>10 #include<stdlib.h>11 #define STRSIZE 3012

13 typedef struct14 {15 char cName[STRSIZE];16 char cUSN[11];17 int iMarks;18 }STUDENT_TYPE;19

20 /***************************************************************************21 *Function : main22 *Input parameters : no parameters23 *RETURNS : 0 on success24 ***************************************************************************/25

26 int main(void)27 {28 STUDENT_TYPE students[100];29 int iNum, i;30 double dAvg = 0.0;31

32 printf("\nEnter the number of students : ");33 scanf("%d", &iNum);34

35 printf("\nEnter the Student details\n");36 for(i=0;i<iNum;i++)37 {38 printf("\n###############################");39 printf("\nName : "); scanf("%s", students[i].cName);

38

Page 39: C PROGRAMMING LABORATORY (18CPL17/27)Finally you will be prompted to choose the compiler. Just choose the default options here (Dont change the options). You should be using GNU GCC

CHAPTER 12. C STRUCTURES

40 printf("\nUSN : "); scanf("%s", students[i].cUSN);41 printf("\nMarks : "); scanf("%d", &students[i].iMarks);42 dAvg += students[i].iMarks;43 }44

45 dAvg /= iNum;46

47 printf("\nThe average marks for the class is : %g\n", dAvg);48

49 for(i=0;i<iNum;i++)50 {51 printf("\n###############################");52 printf("\nName\t: %s", students[i].cName);53 printf("\nUSN\t: %s", students[i].cUSN);54 printf("\nMarks\t: %d", students[i].iMarks);55 if(students[i].iMarks < dAvg)56 printf("\nThe student has scored below average\n");57 else58 printf("\nThe student has scored above average\n");59 }60

61 return 0;62 }

Listing 12.1: B13StudentStructure.c

Output

=================================

1 /***************************************2 Enter the number of students : 43

4 Enter the Student details5

6 ###############################7 Name : Raju8

9 USN : 1SI17CS03610

11 Marks : 6712

13 ###############################14 Name : Michael15

16 USN : 1SI17CS04517

18 Marks : 8719

20 ###############################21 Name : Sahana22

23 USN : 1SI17CS40524

25 Marks : 7726

27 ###############################28 Name : Jonathan29

30 USN : 1SI17CS02531

32 Marks : 8333

Dept of CSE, SIT - CPP 39

Page 40: C PROGRAMMING LABORATORY (18CPL17/27)Finally you will be prompted to choose the compiler. Just choose the default options here (Dont change the options). You should be using GNU GCC

CHAPTER 12. C STRUCTURES

34 The average marks for the class is : 78.535

36 ###############################37 Name : Raju38 USN : 1SI17CS03639 Marks : 6740 The student has scored below average41

42 ###############################43 Name : Michael44 USN : 1SI17CS04545 Marks : 8746 The student has scored above average47

48 ###############################49 Name : Sahana50 USN : 1SI17CS40551 Marks : 7752 The student has scored below average53

54 ###############################55 Name : Jonathan56 USN : 1SI17CS02557 Marks : 8358 The student has scored above average59 ***************************************/

Listing 12.2: out12.c

Dept of CSE, SIT - CPP 40

Page 41: C PROGRAMMING LABORATORY (18CPL17/27)Finally you will be prompted to choose the compiler. Just choose the default options here (Dont change the options). You should be using GNU GCC

Chapter 13

Pointers and Arrays

Question

Develop a program using pointers to compute the sum, mean and standard deviation of allelements stored in an array of n real numbers.

C Code

1 /***************************************************************************2 *File : B14MeanVarianceSD.c3 *Description : Program to compute Mean, Variance and Standard Deviation4 using pointer to an array5 *Author : Prabodh C P6 *Compiler : gcc compiler, Ubuntu 18.047 *Date : 16 August 20188 ***************************************************************************/9 #include<stdio.h>

10 #include<stdlib.h>11 #include<math.h>12 /***************************************************************************13 *Function : main14 *Input parameters : no parameters15 *RETURNS : 0 on success16 ***************************************************************************/17 int main(void)18 {19 int i,iNum;20 float fMean = 0.0f, fVariance = 0.0f, fSd = 0.0f,faArray[100],fSum=0.0f;21 float *fptr;22

23 fptr = faArray;24 printf("\nEnter the number of Values : ");25 scanf("%d",&iNum);26 printf("\nEnter %d values\n", iNum);27 for(i=0; i<iNum; i++)28 {29 scanf("%f",fptr+i);30 fSum += *(fptr+i); //fSum += fptr[i]; this is also valid31 }32 fMean = fSum/iNum;33

34 for(i=0; i<iNum; i++)35 {36 fVariance += (fptr[i] - fMean)*(fptr[i] - fMean);37 //fVariance += (*(fptr+i) - fMean)*(*(fptr+i) - fMean);38 }

41

Page 42: C PROGRAMMING LABORATORY (18CPL17/27)Finally you will be prompted to choose the compiler. Just choose the default options here (Dont change the options). You should be using GNU GCC

CHAPTER 13. POINTERS AND ARRAYS

39 fVariance /= iNum;40 fSd = sqrt(fVariance);41 printf("\nThe values entered are");42 for(i=0; i<iNum; i++)43 {44 printf("\n\t%g",fptr[i]); //printf("\n\t%f",*(fptr+i));45 }46

47 printf("\n**************************************\n");48 printf("\n\tMean = \t%g\n\tVariance = \t%g\n\tStandard Deviation = \t%g\n",

fMean,fVariance,fSd);49 printf("\n**************************************\n");50 return 0;51 }

Listing 13.1: B14MeanVarianceSD.c

Output

=================================

1 /***************************************2 Enter the number of Values : 43

4 Enter 4 values5 1.1 2.2 3.3 4.46

7 The values entered are8 1.19 2.2

10 3.311 4.412 **************************************13

14 Mean = 2.7515 Variance = 1.512516 Standard Deviation = 1.2298417

18 **************************************19

20 Enter the number of Values : 521

22 Enter 5 values23 5.345 6.765 7.234 8.675 9.76524

25 The values entered are26 5.34527 6.76528 7.23429 8.67530 9.76531 **************************************32

33 Mean = 7.556834 Variance = 2.3499535 Standard Deviation = 1.5329536

37 **************************************38 ***************************************/

Listing 13.2: out13.c

Dept of CSE, SIT - CPP 42

Page 43: C PROGRAMMING LABORATORY (18CPL17/27)Finally you will be prompted to choose the compiler. Just choose the default options here (Dont change the options). You should be using GNU GCC

Chapter 14

Recursion

Question

Implement Recursive functions for Binary to Decimal Conversion.

C Code

1 /***************************************************************************2 *File : B15BinarytoDecimal.c3 *Description : Program to compute Mean, Variance and Standard Deviation4 * using pointer to an array5 *Author : Prabodh C P6 *Compiler : gcc compiler, Ubuntu 18.047 *Date : 16 August 20188 ***************************************************************************/9

10 #include<stdio.h>11 #include<stdlib.h>12

13 unsigned long fnBin2Dec(unsigned long);14

15 /***************************************************************************16 *Function : main17 *Input parameters : no parameters18 *RETURNS : 0 on success19 ***************************************************************************/20 int main(void)21 {22 unsigned long iBinVal, iDecVal;23

24 printf("\nEnter the binary value : ");25 scanf("%lu", &iBinVal);26

27 iDecVal = fnBin2Dec(iBinVal);28

29 printf("\nDecimal equivalent of %lu is %lu\n", iBinVal, iDecVal);30 return 0;31 }32

33 /***************************************************************************34 *Function : fnBin2Dec35 *Description : Function to convert a binary value to its36 * equivalent decimal value37 *Input parameters : unsigned long iVal - binary value to be converted38 *RETURNS : equivalent decimal value39 ***************************************************************************/

43

Page 44: C PROGRAMMING LABORATORY (18CPL17/27)Finally you will be prompted to choose the compiler. Just choose the default options here (Dont change the options). You should be using GNU GCC

CHAPTER 14. RECURSION

40

41 unsigned long fnBin2Dec(unsigned long iVal)42 {43 unsigned long iNewVal, iLastDigit;44 if(1 == iVal || 0 == iVal)45 return iVal;46 else47 {48 iNewVal = iVal/10;49 iLastDigit = iVal%10;50 return fnBin2Dec(iNewVal)* 2 + fnBin2Dec(iLastDigit);51 }52 }

Listing 14.1: B15BinarytoDecimal.c

Output

=================================

1 /**************************************************2 Enter the binary value : 1100011000110013

4 Decimal equivalent of 110001100011001 is 253695 ===================================================6 Enter the binary value : 111111117

8 Decimal equivalent of 11111111 is 2559 ===================================================

10 Enter the binary value : 110111011101110111

12 Decimal equivalent of 1101110111011101 is 5679713 ===================================================14 Enter the binary value : 11100011100011100015

16 Decimal equivalent of 111000111000111000 is 23301617 **************************************************/

Listing 14.2: out14.c

Dept of CSE, SIT - CPP 44