Cpps Lab Maunal
Cpps Lab Maunal
BELAGAVI
C PROGRAMMING LABORATORY
(18CPL27)
(As per Visvesvaraya Technological University Syllabus)
Compiled By:
20120-21
Disclaimer
The information contained in this document is the proprietary and exclusive property of Acharya
Institutes except as otherwise indicated. No part of this document, in whole or in part, may be reproduced,
stored, transmitted, or used for course material development purposes without the prior written
permission of Acharya Institutes.
The information contained in this document is subject to change without notice. The information in this
document is provided for informational purposes only.
Trademark
Edition: 2020 - 21
Document Owner
The primary contact for questions regarding this document is:
C PROGRAMMING LABORATORY
[As per Choice Based Credit System (CBCS) scheme - Effective from the academic year 2018-19]
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.
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 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.
6. Introduce 1D Array 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.
C orrect 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.
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)
#include<stdio.h>
main( )
{
float a, b, res;
int op;
switch(op)
{
case 1 : res = a + b;
break;
case 2 : res = a - b;
break;
case 3 : res = a * b;
break;
case 4 : if( b != 0)
{
res = a / b;
}
else
{
printf("Division not possible\n");
exit(0);
}
break;
default : printf("invalid operator\");
exit(0);
}
3. Develop a program to compute the roots of a quadratic equation by accepting the coefficients.
Print appropriate messages.
#include<stdio.h>
#include<math.h>
main( )
{
float a, b, c, disc, root1, root2, x, y;
printf("Enter the coefficients a,b and c of the quadratic equation \n");
scanf("%f%f%f",&a, &b, &c);
disc = b * b - 4 * a * c;
if(disc = = 0)
{
printf(" Roots are real and Equal\n");
root1 = root2 = - b / (2 * a);
printf(" Root1 = %f and Root2 = %f\n", root1, root2);
}
else if(disc > 0)
{
printf(" Roots are real and Distinct\n");
root1 = ( - b + sqrt(disc) ) / (2 * a);
root2 = ( - b - sqrt(disc) ) / (2 * a);
printf(" Root1 = %f and Root2 = %f\n", root1, root2);
}
else if(disc < 0)
{
printf(" Roots are Complex and Distinct\n");
x = - b / (2 * a);
y = sqrt(fabs(disc) ) / (2 * a);
printf(" Root1 = %f + i %f \n", x, y);
printf(" Root2 = %f - i %f \n", x, y);
}
return 0;
}
4. Develop a program to find the reverse of a positive integer and check for palindrome or not.
Display appropriate messages.
#include<stdio.h>
main( )
{
int n, num, rev, digit,
num = n;
rev=0;
while(num!=0)
{
digit = num % 10;
rev = rev*10 + digit;
num = num /10;
}
if(n = = rev)
printf("%d is a palindrome\n", n);
else
printf("%d is not a palindrome\n", n);
return 0;
}
5. 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>
main( )
{
char name[20];
int units;
float charges, surcharge;
if(units < 0)
{
printf("Invalid input\n");
}
else if(units = = 0)
{
charges = 100;
}
#include<stdio.h>
main( )
{
int n, a[20], key, low, high, mid ;
printf(“Enter the number of elements\n”);
scanf(“%d”, &n);
printf(“Enter the elements\n”);
for( i = 0; i < n ; i + + )
{
scanf(“%d”, &a[i] );
}
printf(“Enter the key element to be searched\n”);
scanf(“%d”, &key);
low = 0;
high = n-1;
while( low < = high )
{
mid = (low + high) / 2;
if(key = = a[mid])
{
flag=1;
break;
}
if(key < a[mid])
high = mid – 1;
if(key > a[mid])
low = mid +1;
}
if(flag==1)
{
printf(“SUCCESSFUL SEARCH\n” );
printf(“Element found at %d location\n”, mid + 1 );
}
else
printf(“UNSUCCESSFUL SEARCH\n” );
return 0;
}
7. Implement using functions to check whether the given number is prime and display appropriate
messages. (No built-in math function)
#include<stdio.h>
void checkprime( int n )
{
int i;
main( )
{
int n;
checkprime( n );
return 0;
}
8. Develop a program to introduce 2D Array manipulation and implement Matrix multiplication and
ensure the rules of multiplication are checked.
main( )
{
int m, n, p, q, i, j, a[20] [20], b[20][20], c[20][20];
if(n!=p)
{
printf("Multiplication is not possible\n");
exit(0);
}
for( j = 0; j < b ; j + + )
{
printf(“%d \t”,a[i][j] );
}
printf(“\n”);
}
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.
#include<stdio.h>
#define PI 3.142
main ( )
int i, degree;
scanf(“%d”, °ree);
term = x;
sum = term;
i = 3;
while( fabs(term)>=0.00001)
sum = sum+term;
i = i + 2;
return 0;
10. Write functions to implement string operations such as compare, concatenate, string length.
Convince the parameter passing techniques.
#include<stdio.h>
return 0;
}
i = 0;
while( str1[i] != '\0')
{
i + +;
}
j=0;
while( str2[j] != '\0')
{
str1[i] = str2[j];
i + +;
j + +;
}
str1[i] = '\0';
}
main( )
{
int choice, difference, len;
char str1[20], str2[20], str[20];
for( ; ;)
{
printf(“1.Compare 2.Concatenate 3.Length 4.Exit\n”);
printf(“Enter your choice\n”);
scanf(“%d”, &choice);
switch(choice)
{
case 1: printf("Enter the string1\n");
scanf("%[^\n]", str1); // gets(str1);
printf("Enter the string2\n");
scanf("%[^\n]", str2); // gets(str2);
difference = my_strcmp(str1, str2);
if(difference = = 0)
printf("%s = %s\n", str1, str2);
else if (difference = = 1)
printf("%s > %s\n", str1, str2);
else if (difference = = -1)
printf("%s < %s\n", str1, str2);
break;
}
return 0;
}
11. Develop a program to sort the given set of N numbers using Bubble sort.
#include<stdio.h>
main( )
{
int n, i, j, temp, a[100];
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).
#include<stdio.h>
main( )
{
float n, s, d;
s=1;
while(s*s<=1)
{
s=s+1;
}
s = s – 1;
d=0.0001;
while(s*s<=1)
{
s=s+d;
}
s = s – d;
return 0;
}
OR
#include<stdio.h>
main( )
{
float n, x1, x2;
x2 = 0;
x1 = (n + (n/n)) / 2;
while(x1 != x2)
{
x2 = (x1 + (n/x1)) / 2;
x1 = (x2 + (n/x2)) / 2;
}
return 0;
}
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.
#include<stdio.h>
struct student
{
char name[20];
int rollno;
int m1, m2, m3;
int avg;
};
main( )
{
struct student s[100];
int n, i, sum=0, class_avg =0;
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.
#include<stdio.h>
main( )
{
int n, i;
float a[20], sum, mean, var, sd;
float *p;
p = a; // p = &a[0];
// To find variance
temp=0.0
for( i = 0; i < n ; i + + )
{
temp = temp + (*(p+i) - mean) * (*(p+i) - mean);
}
var = temp/n;
#include<stdio.h>
int binarytodecimal(int n)
if(n = = 0)
return 0;
else
main( )
scanf(“%d”,&binnum);
return 0;
}