Calender 2018
Calender 2018
LABORATORY MANUAL
for
C PROGRAMMING LAB
( 18CPL17/27)
Department: ________________________________________________
Prepared by :
Mr. Manjunatha Reddy G
Asst. Professor,
Department of CSE.
Laboratory Programs:
1. Familiarization with computer hardware and programming environment, concept of naming the program files, storing, compilation,
execution and debugging, taking any simple C- code. [Not included in the Lab Exam ]
PART A
2. Develop a program to solve simple computational problems using arithmetic expressions and use of each operator leading to simulation of
a commercial calculator. (No built-in math function)
3. Develop a program to compute the roots of a quadratic equation by accepting the coefficients. Print appropriate messages.
4. Develop a program to find the reverse of a positive integer and check for palindrome or not. Display appropriate messages.
5. An electricity board charges the following rates for the use of electricity: for the first 200 units 80 paisa per unit: for the next 100 units 90
paisa 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.
6. Introduce 1D An-ay manipulation and implement Binary search.
7. Implement using functions to check whether the given number is prime and display appropriate messages. (No built-in math function)
PART B
8. Develop a program to introduce 2D Array manipulation and implement Matrix multiplication and ensure the rules of multiplication are
checked.
9. Develop a Program to compute Sin(x) using Taylor series approximation. Compare your result with the built- in Library function. Print
both the results with appropriate messages.
10. Write functions to implement string operations such as compare, concatenate, string length. Convince the parameter passing techniques.
11. Develop a program to sort the given set of N numbers using Bubble sort.
12. Develop a program to find the square root of a given number N and execute for all possible inputs with appropriate messages.
Note: Don't use library function sqrt(n).
13. 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.
14. Develop a program using pointers to compute the sum, mean and standard deviation of all elements stored in an array of n real numbers.
15. Implement Recursive functions for Binary to Decimal Conversion.
Laboratory Outcomes:
The student should be able to:
Write algorithms, flowcharts and program for simple problems.
Correct syntax and logical errors to execute a program.
Write iterative and wherever possible recursive programs.
Demonstrate use of functions, arrays, strings, structures and pointers in problem solving.
a) For questions having only one part — Procedure + Execution + Viva-Voce: 15+70+15 =100 Marks
b) For questions having part A and B
i. Part A— Procedure + Execution + Viva = 4 + 21 + 5 = 30 Marks
ii. Part B — Procedure + Execution + Viva = 10 + 49 + 11 = 70 Marks
INDEX
Obtained Marks
Exp
Date Laboratory Programs in Short A
No P E V Signature
/F
1
PART – A 2 2 21 5
2
PART – B 4 6 49 11
8
10
11
12
13
14
15
After the each experiment execution student must get signature by the Incharge faculty with date.
The faculty as to enter the obtain marks for the each experiment. Maximum marks given for Part-A & Part-B
Note :
1. Every experiment should have algorithm and flowchart be written before writing the program.
2. Code should be traced using minimum two test cases which should be recorded.
Marks Obtain
Part - B
Exp. No. 8 9 10 11 12 13 14 15 Total Average Signature
Max Marks 70 70 70 70 70 70 70 70 560 (Total/8)
Marks Obtain
Program – 1 : Familiarization with computer hardware and programming environment, concept of naming the program files, storing,
compilation, execution and debugging, taking any simple C- code. [Not included in the Lab Exam ]
Generally, the programs created using programming languages like C, C++, Java etc., are written
using high level language like English. But, computer cannot understand the high level language. It
can understand only low level language. So, the program written in high level language needs to be
converted into low level language to make it understandable for the computer. This conversion is
performed using either Interpreter or Compiler.
Popular programming languages like C, C++, Java etc., use compiler to convert high level language
instructions into low level language instructions. Compiler is a program that converts high level
language instructions into low level language instructions. Generally, compiler performs two
things, first it verifies the program errors, if errors are found, it returns list of errors otherwise it
converts the complete code into low level language.
To create and execute C programs in Windows Operating System, we need to install Turbo C
software. We use the following steps to create and execute C programs in Windows OS…
We use a shortcut key Ctrl + F9 to run a C program. Whenever we press Ctrl + F9, the .exe file is
submitted to the CPU. On receiving .exe file, CPU performs the task according to the instruction
written in the file. The result generated from the execution is placed in a window called User
Screen.
The file which contains c program instructions in high level language is said to be source code.
Every c program source file is saved with .c extension, for example Sample.c.
Whenever we press Alt + F9 the source file is submitted to the compiler. Compiler checks for the
errors, if there are any errors, it returns list of errors, otherwise generates object code in a file
18CPL17/27 C Programming Lab
file into object file and generates executable file as Sample.exe. With this compilation process
completes.
Now, we need to Run the executable file (Sample.exe). To run a program we press Ctrl + F9. When
we press Ctrl + F9 the executable file is submitted to the CPU. Then CPU performs the task
according to the instructions written in that program and place the result into UserScreen.
Then we press Alt + F5 to open UserScreen and check the result of the program.
Important Points
C program file (Source file) must save with .c extension.
Compiler converts complete program at a time from high level language to low level
language.
Input to the compiler is .c file and output from the compiler is .exe file, but it also
generates .obj file in this process.
Compiler converts the file only if there are no errors in the source code.
CPU places the result in User Screen window.
Overall Process
Type the program in C editor and save with .c extension (Press F2 to save).
Press Alt + F9 to compile the program.
If there are errors, correct the errors and recompile the program.
If there are no errors, then press Ctrl + F9 to execute / run the program.
Press Alt + F5 to open User Screen and check the result.
When we want to develop a program using any programming language, we follow a sequence of steps.
These steps are called phases in program development. The program development life cycle is a set of steps
or phases that are used to develop a program in any programming language.
Generally, program development life cycle contains 6 phases, they are as follows….
Problem Definition
Problem Analysis
Algorithm Development
Coding & Documentation
Testing & Debugging
Maintenance
1. Problem Definition
In this phase, we define the problem statement and we decide the boundaries of the problem. In this phase
we need to understand the problem statement, what is our requirement, what should be the output of the
problem solution. These are defined in this first phase of the program development life cycle.
2. Problem Analysis
In phase 2, we determine the requirements like variables, functions, etc. to solve the problem. That means
we gather the required resources to solve the problem defined in the problem definition phase. We also
determine the bounds of the solution.
3. Algorithm Development
During this phase, we develop a step by step procedure to solve the problem using the specification given in
the previous phase. This phase is very important for program development. That means we write the
solution in step by step statements.
6. Maintenance
During this phase, the program is actively used by the users. If any enhancements found in this phase, all
the phases are to be repeated again to make the enhancements. That means in this phase, the solution
(program) is used by the end user. If the user encounters any problem or wants any enhancement, then we
need to repeat all the phases from the starting, so that the encountered problem is solved or enhancement
is added.
18CPL17/27 C Programming Lab
Program 2 : PART A
Develop a program to solve simple computational problems using arithmetic expressions and use of each operator leading to
simulation of a commercial calculator. (No built-in math function)
Source Code
#include <stdio.h>
#include<strings.h>
#include<ctype.h>
int main()
{
int res,opr,opd, i;
char exp[20] = "5/2+7";
if (opr == '+')
res += opd;
else if (opr == '-')
res -= opd;
else if (opr == '*')
res *= opd;
else if (opr == '/')
res /= opd;
else
{
printf("Invalid Expression");
return 0;
}
}
Output :
Program 3 : PART A
Develop a program to compute the roots of a quadratic equation by accepting the coefficients. Print appropriate messages.
Source Code
#include<stdio.h>
#include<math.h>
int main ()
{
float a, b, c, d, r1, r2;
Output :
Program 4 : PART A
Develop a program to find the reverse of a positive integer and check for palindrome or not. Display appropriate messages.
Source Code
#include <stdio.h>
int main ()
{
int n, rev = 0, num;
do
{
printf ("\nEnter an Positive Integer Number( > 2 digits): ");
scanf ("%d", &n);
}
while (n < 10);
num = n;
while (n != 0)
{
rev = rev * 10 + n % 10;
n /= 10;
}
if (num == rev)
printf ("%d is a palindrome.", num);
else
printf ("%d is not a palindrome.", num);
return 0;
}
Output :
Program 5 : PART A
An electricity board charges the following rates for the use of electricity: for the first 200 units 80 paisa per unit: for the next 100
units 90 paisa 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.
Source Code
#include <stdio.h>
int main ()
{
char name[20];
float unit, amount, scharge = 0.0;
printf ("\n__________________");
printf("\nName : %s",name);
printf("\nUnit Consumed: %.0f",unit);
Output :
Program 6 : PART A
Introduce 1D An-ay manipulation and implement Binary search.
Source Code
#include <stdio.h>
int main ()
{
int c, first, last, middle, n, search, A[100];
first = 0;
last = n - 1;
}
if (first > last)
printf ("Not found! %d isn't present in the list.\n", search);
return 0;
}
Output :
Program 7 : PART A
Implement using functions to check whether the given number is prime and display appropriate messages. (No built-in math
function).
Source Code
#include <stdio.h>
int isPrime(int n)
{
int i;
return 0;
}
Output :
BTLIT CSE P a g e | 10
18CPL17/27 C Programming Lab
Program 8 : PART- B
Develop a program to introduce 2D Array manipulation and implement Matrix multiplication and ensure the rules of
multiplication are checked.
Source Code
#include <stdio.h>
int main()
{
printf("Enter number of rows and columns of first matrix\n");
scanf("%d%d", &m, &n);
if (n != p)
{
printf("The matrices can't be multiplied with each other.\n");
printf("Error! column of first matrix not equal to row of second.\n\n");
return 0;
}
printf("\n");
}
return 0;
}
Output :
BTLIT CSE P a g e | 11
18CPL17/27 C Programming Lab
Program 9 : PART - B
Develop a Program to compute Sin(x) using Taylor series approximation. Compare your result with the built- in Library function.
Print both the results with appropriate messages.
Source Code
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main ()
{
int n, x1;
x1 = x;
/* Converting degrees to radians */
x = x * (3.142 / 180.0);
term = x;
sinx = term;
n = 1;
do
{
denominator = 2 * n * (2 * n + 1);
n = n + 1;
}
while (accuracy <= fabs (sinval - sinx));
return 0;
}
Output :
BTLIT CSE P a g e | 12
18CPL17/27 C Programming Lab
Program 10 : PART-B
Write functions to implement string operations such as compare, concatenate, string length. Convince the parameter passing
techniques.
Source Code
#include<stdio.h>
return c;
}
s1[i] = '\0';
}
int main ()
{
char string1[20], string2[20];
int choice;
printf ("\n1. Find Length \n2. Concatenate \n3. Compare \n4.Exit\n");
printf ("Enter your choice: ");
scanf ("%d", &choice);
switch (choice)
{
case 1:
printf ("Enter the string: ");
scanf ("%s", string1);
printf ("The length of string is %d", string_length (string1));
break;
case 2:
printf ("Enter two strings: ");
scanf ("%s%s", string1, string2);
join_strings (string1, string2);
printf ("The concatenated string is %s", string1);
break;
BTLIT CSE P a g e | 13
18CPL17/27 C Programming Lab
case 3:
printf ("Enter two strings: ");
scanf ("%s%s", string1, string2);
if (match_strings (string1, string2) == 0)
{
printf ("They are equal");
}
else
{
printf ("They are not equal");
}
break;
case 4:
return 0;
}
return 0;
}
Output :
BTLIT CSE P a g e | 14
18CPL17/27 C Programming Lab
Source Code
#include <stdio.h>
#define MAXSIZE 10
void main()
{
int array[MAXSIZE];
int i, j, num, temp;
Output :
BTLIT CSE P a g e | 15
18CPL17/27 C Programming Lab
Program 12 : PART-B
Develop a program to find the square root of a given number N and execute for all possible inputs with appropriate messages.
Note: Don't use library function sqrt(n).
Source Code
#include<stdio.h>
float squareRoot(float n)
{
/*We are using n itself as first estimate This can surely be better */
float x , y = 1, e = 0.000001; /* e decides the accuracy level*/
x = n ;
while(x - y > e)
{
x = (x + y)/2;
y = n/x;
}
return x;
}
Output :
BTLIT CSE P a g e | 16
18CPL17/27 C Programming Lab
Program 13 : PART-B
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..
Source Code
#include<stdio.h>
struct stud
{
int rollno, s1, s2, tot ;
char name[10] ;
float avg ;
} s[10] ;
int main()
{
int i, n ;
float mean;
printf("Enter the number of students : ") ;
scanf("%d", &n) ;
for(i = 0 ; i < n ; i++)
{
printf("\nEnter the roll number : ") ;
scanf("%d", &s[i].rollno) ;
printf("\nEnter the name : ") ;
scanf("%s", s[i].name) ;
printf("\nEnter the marks in 2 subjects : ") ;
scanf("%d %d", &s[i].s1, &s[i].s2) ;
s[i].tot = s[i].s1 + s[i].s2 ;
s[i].avg = s[i].tot / 2.0 ;
}
mean = compute( s,n);
printf("\nBelow Average Students\n");
printf("\nRoll No. Name \t\tSub1\t Sub2\t Total\t Average\n\n") ;
for(i = 0 ; i < n ; i++)
{
if( s[i].avg < mean )
display(s[i]);
}
BTLIT CSE P a g e | 17
18CPL17/27 C Programming Lab
Program 14 : PART-B
Develop a program using pointers to compute the sum, mean and standard deviation of all elements stored in an array of n real
numbers.
Source Code
#include<stdio.h>
#include<math.h>
int main ()
{
float a[20], sum1 = 0, sum2 = 0, mean, var, dev;
int i, n;
printf ("enter no of ele\n");
scanf ("%d", &n);
printf ("enter array ele\n");
for (i = 0; i < n; i++)
{
scanf ("%f", a + i);
sum1 = sum1 + *(a + i);
}
mean = sum1 / n;
for (i = 0; i < n; i++)
{
sum2 = sum2 + pow ((*(a + i) - mean), 2);
}
var = sum2 / n;
dev = sqrt (var);
printf ("sum is%f\n", sum1);
printf ("mean is%f\n", mean);
printf ("variance is%f\n", var);
printf ("devn is%f\n", dev);
return 0;
}
Output :
BTLIT CSE P a g e | 18
18CPL17/27 C Programming Lab
Program 15 : PART-B
Implement Recursive functions for Binary to Decimal Conversion..
Source Code
#include <stdio.h>
#include <math.h>
int bin_to_dec(long long int num)
{
if (!(num / 10))
return (num);
return (num % 10 + bin_to_dec (num / 10) * 2);
}
int main()
{
long long n;
printf("Enter a Valid binary number < (10 Digits): ");
scanf("%lld", &n);
printf("%lld in binary = %d in decimal", n, bin_to_dec (n));
return 0;
}
Output :
BTLIT CSE P a g e | 19
18CPL17/27 C Programming Lab
Program 2 : PART A
Develop a program to solve simple computational problems using arithmetic expressions and use of each operator leading to
simulation of a commercial calculator. (No built-in math function)
Logic Flow
BTLIT CSE P a g e | 20
18CPL17/27 C Programming Lab
Program 3 : PART A
Develop a program to compute the roots of a quadratic equation by accepting the coefficients. Print appropriate messages.
Logic Flow
BTLIT CSE P a g e | 21
18CPL17/27 C Programming Lab
Program 4 : PART A
Develop a program to find the reverse of a positive integer and check for palindrome or not. Display appropriate messages.
Logic Flow
BTLIT CSE P a g e | 22
18CPL17/27 C Programming Lab
Program 5 : PART A
An electricity board charges the following rates for the use of electricity: for the first 200 units 80 paisa per unit: for the next 100
units 90 paisa 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.
Logic Flow
BTLIT CSE P a g e | 23
18CPL17/27 C Programming Lab
Program 6 : PART A
Introduce 1D An-ay manipulation and implement Binary search.
Logic Flow
BTLIT CSE P a g e | 24
18CPL17/27 C Programming Lab
Program 7 : PART A
Implement using functions to check whether the given number is prime and display appropriate messages. (No built-in math
function).
Logic Flow
BTLIT CSE P a g e | 25
18CPL17/27 C Programming Lab
Program 8 : PART- B
Develop a program to introduce 2D Array manipulation and implement Matrix multiplication and ensure the rules of
multiplication are checked.
Logic Flow
BTLIT CSE P a g e | 26
18CPL17/27 C Programming Lab
Program 9 : PART - B
Develop a Program to compute Sin(x) using Taylor series approximation. Compare your result with the built- in Library function.
Print both the results with appropriate messages.
Logic Flow
BTLIT CSE P a g e | 27
18CPL17/27 C Programming Lab
Program 10 : PART-B
Write functions to implement string operations such as compare, concatenate, string length. Convince the parameter passing
techniques.
Logic Flow
BTLIT CSE P a g e | 28
18CPL17/27 C Programming Lab
Logic Flow
BTLIT CSE P a g e | 29
18CPL17/27 C Programming Lab
Program 12 : PART-B
Develop a program to find the square root of a given number N and execute for all possible inputs with appropriate messages.
Note: Don't use library function sqrt(n).
Logic Flow
BTLIT CSE P a g e | 30
18CPL17/27 C Programming Lab
Program 13 : PART-B
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..
Logic Flow
BTLIT CSE P a g e | 31
18CPL17/27 C Programming Lab
Program 14 : PART-B
Develop a program using pointers to compute the sum, mean and standard deviation of all elements stored in an array of n real
numbers.
Logic Flow
BTLIT CSE P a g e | 32
18CPL17/27 C Programming Lab
Program 15 : PART-B
Implement Recursive functions for Binary to Decimal Conversion..
Logic Flow
BTLIT CSE P a g e | 33