0% found this document useful (0 votes)
12 views

Cpps Lab Maunal

LAB AMUAL CPPS VTU

Uploaded by

Nidhi Charate
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Cpps Lab Maunal

LAB AMUAL CPPS VTU

Uploaded by

Nidhi Charate
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

VISVESVARAYA TECHNOLOGICAL UNIVERSITY

BELAGAVI

C PROGRAMMING LABORATORY
(18CPL27)
(As per Visvesvaraya Technological University Syllabus)

Compiled By:

Prof. C K Marigowda Prof. Arun K H


Associate Professor, HOD, Dept. of ISE Assistant Professor, Dept. of ISE

DEPARTMENT OF INFORMATION SCIENCE AND ENGINEERING


ACHARYA INSTITUTE OF TECHNOLOGY
Affiliated to VTU, Belagavi, Approved by AICTE, New Delhi and Govt. of Karnataka),
Acharya Dr. Sarvepalli Radhakrishnan Road, Bangalore-560107.
Ph : 91-080-28396011, 23723466, 28376431
URL: www.acharya.ac.in

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:

Author(s): 1. Prof. C K Marigowda.


2. Prof. Arun K H.

Department: Information Science & Engineering


Contact Email(s): [email protected]
[email protected]
C Programming for Problem Solving Dept. of ISE, AcIT-Bangalore

C PROGRAMMING LABORATORY
[As per Choice Based Credit System (CBCS) scheme - Effective from the academic year 2018-19]

Semester : I/II I.A. Marks : 40


Course Code : 18CPL17/27 Exam Marks : 60
Total Hours / Week (L : T : P) : 0:0:2 Exam Hours : 03

Course Learning Objectives:


This course (18CPL17/27) will enable students to:
 Write flowcharts, algorithms and programs.
 Familiarize the processes of debugging and execution.
 Implement basics of C programming language.
 Illustrate solutions to the laboratory programs.

Descriptions (if any):


 The laboratoryshould be preceded or followed by a tutorial to explain the approach or algorithm being
implemented or implemented for the problems given.
 Note that experiment 1 is mandatory and written in the journal.
 Questions related with experiment 1, need to be asked during viva-voce for all experiments.
 Everyexperiment should have algorithm and flowchart be written before writing the program.
 Code should be traced using minimum two test cases which should be recorded.
 It is preferred to implement using Linux and GCC.

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.

© Copyright Acharya Institutes Page 1


C Programming for Problem Solving Dept. of ISE, AcIT-Bangalore

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.

© Copyright Acharya Institutes Page 2


C Programming for Problem Solving Dept. of ISE, AcIT-Bangalore

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.

Conduct of Practical Examination:


 All laboratory experiments, excluding the first, are to be included for practical
examination.
 Experiment distribution
o For questions having only one part: Students are allowed to pick one experiment
from the lot and are given equal opportunity.
o For questions having part A and B: Students are allowed to pick one experiment
from part A and one experiment from part B and are given equal opportunity.
 Strictly follow the instructions as printed on the cover page of answer script for breakup of
marks
 Change of experiment is allowed only once and marks allotted for procedure part to be
made zero.
 Marks Distribution (Subjected to change in accordance with university regulations)
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

© Copyright Acharya Institutes Page 3


C Programming for Problem Solving Dept. of ISE, AcIT-Bangalore

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;

printf("Enter the two operands \n");


scanf("%f%f",&a, &b);

printf("Enter the operator \n");


scanf("%d",&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);
}

printf(" Result = %f \n", res);


return 0;
}

© Copyright Acharya Institutes Page 4


C Programming for Problem Solving Dept. of ISE, AcIT-Bangalore

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

© Copyright Acharya Institutes Page 5


C Programming for Problem Solving Dept. of ISE, AcIT-Bangalore

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,

printf("Enter the number\n");


scanf("%d", &n);

num = n;
rev=0;

while(num!=0)
{
digit = num % 10;
rev = rev*10 + digit;
num = num /10;
}

printf("The reverse of %d is %d\n", n, rev);

if(n = = rev)
printf("%d is a palindrome\n", n);
else
printf("%d is not a palindrome\n", n);
return 0;
}

© Copyright Acharya Institutes Page 6


C Programming for Problem Solving Dept. of ISE, AcIT-Bangalore

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;

printf("Enter the name of the customer \n");


scanf("%s", name);
printf("Enter the total number of units consumed \n");
scanf("%d", &units);

if(units < 0)
{
printf("Invalid input\n");
}

else if(units = = 0)
{
charges = 100;
}

else if(units > 0 and units <=200)


{
charges = 100 + units*0.80;
}

© Copyright Acharya Institutes Page 7


C Programming for Problem Solving Dept. of ISE, AcIT-Bangalore

else if(units > 200 and units <=300)


{
charges = 100 + 200 * 0.80 + (units - 200) * 0.90;
}
else if(units > 300)
{
charges = 100 + 200 * 0.80 + 100 * 0.90 + (units - 300) * 1.00;
}

if(charges > 400)


{
surcharge = charges * 0.15;
charges = charges + surcharge;
}
printf("Name of the customer is %s\n", name);
printf("Number of units consumer is %d\n", units);
printf("Total amount to be payed is %f\n", charges);
return 0;
}

© Copyright Acharya Institutes Page 8


C Programming for Problem Solving Dept. of ISE, AcIT-Bangalore

6. Introduce 1D Array manipulation and implement Binary search.

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

© Copyright Acharya Institutes Page 9


C Programming for Problem Solving Dept. of ISE, AcIT-Bangalore

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;

if( n== 0 || n==1)


{
printf("Number is neither prime nor composite\n");
exit(0);
}
for(i=2; i<=n/2; i++)
{
if( n % i = = 0 )
{
printf("Number is not prime\n");
exit(0);
}
}
printf("Number is prime\n");

main( )
{
int n;

printf("Enter the value of n\n");


scanf("%d", &n);

checkprime( n );
return 0;
}

© Copyright Acharya Institutes Page 10


C Programming for Problem Solving Dept. of ISE, AcIT-Bangalore

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

printf(“Enter the number of rows in the matrix A\n”);


scanf(“%d”, &m);

printf(“Enter the number of columns in the matrix A\n”);


scanf(“%d”, &n);

printf(“Enter the number of rows in the matrix B\n”);


scanf(“%d”, &p);

printf(“Enter the number of columns in the matrix B\n”);


scanf(“%d”, &q);

if(n!=p)
{
printf("Multiplication is not possible\n");
exit(0);
}

printf(“Enter the elements of matrix A\n”);


for( i = 0; i < m ; i + + )
{
for( j = 0; j < n ; j + + )
{
scanf(“%d”, &a[i][j] );
}
}

printf(“Enter the elements of matrix B\n”);


for( i = 0; i < p ; i + + )
{
for( j = 0; j < q ; j + + )
{
scanf(“%d”, &b[i][j] );
}
}

printf(“The matrix A elements are\n”);


for( i = 0; i < m ; i + + )
{

© Copyright Acharya Institutes Page 11


C Programming for Problem Solving Dept. of ISE, AcIT-Bangalore

for( j = 0; j < b ; j + + )
{
printf(“%d \t”,a[i][j] );
}
printf(“\n”);
}

printf(“The matrix B elements are \n”);


for( i = 0; i < p ; i + + )
{
for( j = 0; j < q ; j + + )
{
printf(“%d \t”,b[i][j] );
}
printf(“\n”);
}

// To multilpy the two matrices


for( i = 0; i < m ; i + + )
{
for( j = 0; j < q ; j + + )
{
c[i][j] = 0;
for( k = 0; k < n ; k + + )
{
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}
}

printf(“The resultant matrix after addition is\n”);


for( i = 0; i < m ; i + + )
{
for( j = 0; j < q ; j + + )
{
printf(“%d \t”,c[i][j] );
}
printf(“\n”);
}
return 0;
}

© Copyright Acharya Institutes Page 12


C Programming for Problem Solving Dept. of ISE, AcIT-Bangalore

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;

float x, sum, term;

printf(“Enter the value in degrees:\n”);

scanf(“%d”, &degree);

x=degree * (PI / 180);

term = x;

sum = term;

i = 3;

while( fabs(term)>=0.00001)

term = -term * x * x / (i*(i-1));

sum = sum+term;

i = i + 2;

printf(“The sine of %d is %f\n”, degree, sum);

printf(“The sine function of %d is %f\n” degree, sin(x));

return 0;

© Copyright Acharya Institutes Page 13


C Programming for Problem Solving Dept. of ISE, AcIT-Bangalore

10. Write functions to implement string operations such as compare, concatenate, string length.
Convince the parameter passing techniques.

#include<stdio.h>

int my_strcmp(char str1[ ], char str2[ ])


{
int i = 0;

while( str1[i] != '\0' && str2[i] != '\0')


{
if(str1[i] > str2[i])
return 1;
else if(str1[i] < str2[i])
return -1;
else
i + +;
}
if(str1[i] != '\0' && str2[i] = = '\0')
return 1;

if(str1[i] = = '\0' && str2[i] != '\0')


return -1;

return 0;
}

void my_strcat(char str1[ ], char str2[ ])


{
int i, j;

i = 0;
while( str1[i] != '\0')
{
i + +;
}

j=0;
while( str2[j] != '\0')
{
str1[i] = str2[j];
i + +;
j + +;
}
str1[i] = '\0';
}

© Copyright Acharya Institutes Page 14


C Programming for Problem Solving Dept. of ISE, AcIT-Bangalore

int my_strlen(char str[ ])


{
int i = 0;

while( str[i] != '\0')


{
i + +;
}
return i;
}

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;

case 2: printf("Enter the string1\n");


scanf("%[^\n]", str1); // gets(str1);
printf("Enter the string2\n");
scanf("%[^\n]", str2); // gets(str2);
my_strcat(str1, str2);
printf("Concatenated string = %s\n", str1);
break;

© Copyright Acharya Institutes Page 15


C Programming for Problem Solving Dept. of ISE, AcIT-Bangalore

case 3: printf("Enter the string\n");


scanf("%[^\n]", str); // gets(str);
len = my_strlen(str);
printf("Length of the string = %d\n",len);
break;
case 4: exit(0);
}

}
return 0;
}

© Copyright Acharya Institutes Page 16


C Programming for Problem Solving Dept. of ISE, AcIT-Bangalore

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

printf("Enter the value for n:\n");


scanf("%d",&n);

printf("Enter the array elements\n");


for(i=0;i<n;i++)
scanf("%d",&a[i]);

printf("The array elements before sorting are\n");


for(i=0;i<n;i++)
printf("%d\n",a[i]);

for(j=1; j<n; j++)


{
for(i=0 ; i< n-j ; i++)
{
if(a[i]>a[i+1])
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
}
}

© Copyright Acharya Institutes Page 17


C Programming for Problem Solving Dept. of ISE, AcIT-Bangalore

printf("The elements after sorting are\n");


for(i=0;i<n;i++)
printf("%d\n",a[i]);
return 0;
}

© Copyright Acharya Institutes Page 18


C Programming for Problem Solving Dept. of ISE, AcIT-Bangalore

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;

printf(“Enter the value of n\n”);


scanf(“%f”, &n);

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;

printf(“Square root of a given number %f is %f\”, n, s);


printf(“Using Built in function Square root of a given number %f is %f\”, n, sqrt(n));

return 0;
}

© Copyright Acharya Institutes Page 19


C Programming for Problem Solving Dept. of ISE, AcIT-Bangalore

OR

#include<stdio.h>
main( )
{
float n, x1, x2;

printf("Enter the number\n");


scanf("%f", &n);

x2 = 0;
x1 = (n + (n/n)) / 2;

while(x1 != x2)
{
x2 = (x1 + (n/x1)) / 2;
x1 = (x2 + (n/x2)) / 2;
}

printf("Square root of %f is %f\n", n, x1);


printf(“Using Built in function Square root of a given number %f is %f\”, n, sqrt(n));

return 0;
}

© Copyright Acharya Institutes Page 20


C Programming for Problem Solving Dept. of ISE, AcIT-Bangalore

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;

printf("Enter the number of students\n");


scanf("%d", &n);
// To read the details of 'n' students
printf("Enter the student details\n");
for(i=0; i<n; i++)
{
printf("Enter the name\n");
scanf("%s", s[i]. name);
printf("Enter the rollno\n");
scanf("%d", &s[i]. rollno);
printf("Enter the marks in three tests\n");
scanf("%d", &s[i].m1, &s[i].m2, &s[i].m3);
}
// To compute the average marks of each student
for(i=0; i<n; i++)
{
s[i].avg = ( s[i].m1 + s[i].m2 + s[i].m3) / 3;
}

// To compute the average marks of class


for(i=0; i<n; i++)
{
sum = sum + s[i].avg;
}
class_avg = sum / n;

printf("The average marks of class is %d\n", class_avg);

© Copyright Acharya Institutes Page 21


C Programming for Problem Solving Dept. of ISE, AcIT-Bangalore

// To print the names of students scoring above average


printf("Above average students\n");
for(i=0; i<n; i++)
{
if( s[i].avg > = class_avg)
{
printf("%d\t", s[i].rollno);
printf("%s\n", s[i].name);
}
}

// To print the names of students scoring below average


printf("Below average students\n");
for(i=0; i<n; i++)
{
if( s[i].avg < class_avg)
{
printf("%d\t", s[i].rollno);
printf("%s\n", s[i].name);
}
}
return 0;
}

© Copyright Acharya Institutes Page 22


C Programming for Problem Solving Dept. of ISE, AcIT-Bangalore

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;

printf(“Enter the number of elements\n”);


scanf(“%d”, &n);

printf(“Enter the elements\n”);


for( i = 0; i < n ; i + + )
{
scanf(“%f”, &a[i] );
}

p = a; // p = &a[0];

// To find sum and mean


sum=0.0;
for( i = 0; i < n ; i + + )
{
sum = sum + *(p+i);
}
mean = sum/n;

// To find variance
temp=0.0
for( i = 0; i < n ; i + + )
{
temp = temp + (*(p+i) - mean) * (*(p+i) - mean);
}
var = temp/n;

// To find standard deviation


sd = sqrt(var);

© Copyright Acharya Institutes Page 23


C Programming for Problem Solving Dept. of ISE, AcIT-Bangalore

printf("sum = %f\n", sum);


printf("mean = %f\n", mean);
printf("variance = %f\n", var);
printf("standard deviation = %f\n", sd);
return 0;
}

© Copyright Acharya Institutes Page 24


C Programming for Problem Solving Dept. of ISE, AcIT-Bangalore

15. Implement Recursive functions for Binary to Decimal Conversion.

#include<stdio.h>

int binarytodecimal(int n)

if(n = = 0)

return 0;

else

return( n%10 + binarytodecimal (n/10) * 2 );

main( )

int decnum, binnum;

printf(“enter the binary number : only 0s and 1s”);

scanf(“%d”,&binnum);

decnum= binarytodecimal (binnum);

printf(“the decimal equivalent =%d”, decnum);

return 0;
}

© Copyright Acharya Institutes Page 25

You might also like