Principles of Programming Using C Laboratory
Principles of Programming Using C Laboratory
of CSE, AIT-Bangalore
[As per Choice Based Credit System (CBCS) scheme - Effective from the academic year 2021-2022]
COMPUTER PROGRAMMING
LABORATORY
Course Code 22POP13/2 CIE Marks 50
3
6[As per
Compute
Choice sin(x)/cos(x) using (CBCS)
Based Credit System Taylor series
schemeapproximation. Compare
- Effective from the your
academic year 2021-2022]
resultwith the built-in library function. Print both the results with
[As per Choice Based Credit System (CBCS) scheme - Effective from the academic year 2021-2022]
appropriate inferences.
Course Objectives:
1. Explain problem statements and identify appropriate solutions
2. Demonstrate the use of IDE, C Compiler, and identify and rectify the
syntax andsyntactic errors during programming.
3. Development of algorithms and programs using constructs of C programming language
4. Reporting the observations
Course Outcomes:
The student should be able to:
At the end of the course the student will be able to:
[As per Choice Based Credit System (CBCS) scheme - Effective from the academic year 2021-2022]
Conduct of Practical Examination:
The practical examinations are to be conducted as per the timetable of the University in batch-
wise with the strength of students not more than 10-15 per batch.
Students can pick one experiment from the questions lot of PART A with equal choice to all
the students in a batch.
For PART B examiners should frame a question for each batch, the student should develop
an algorithm, program, execute and demonstrate the results with appropriate output for the
given problem.
Change of experiment is allowed only once for part A and 15% Marks allotted to the procedure
part to be made zero. However, PART B question is mandatory (change of question is not
allowed).
Marks distribution: procedure (15%) + execution (70%) + viva voce (15%) PART A
(12+56+12 = 80) AND FOR PART B (3+14+3 = 20)
[As per Choice Based Credit System (CBCS) scheme - Effective from the academic year 2021-2022]
PART-A
20 Choice Based Credit System (CBCS) scheme - Effective from the academic year 2021-2022]
[As per
[As per Choice Based Credit System (CBCS) scheme - Effective from the academic year 2021-2022]
23
Result =43.000000
2. Enter an Operator:
-
Enter two Operands:
23
20
Result = 3.000000
3. Enter an Operator:
*
Enter two Operands:
3
2
Result =6.000000
4. Enter an Operator:
/
Enter two Operands:
6
2
Result = 3.0000
5. Enter an Operator :
!
Enter two Operands:
6
2
Invalid Operator
6. Enter an Operator :
/
Enter two Operands:
2
0
Division not possible
[As per Choice Based Credit System (CBCS) scheme - Effective from the academic year 2021-2022]
#include<stdio.h>
#include<math.h>
void main( )
{
float a, b, c, disc, root1, root2, real, img;
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");
real = - b / (2 * a);
img = sqrt(fabs(disc) ) / (2 * a);
printf(" Root1 = %f + i %f \n", real, img);
printf(" Root2 = %f - i %f \n", real, img);
}
}
Output
1. Enter the coefficients a,b and c of the quadratic
equation 1 1 1
Roots are Complex and Distinct
[As per Choice Based Credit System (CBCS) scheme - Effective from the academic year 2021-2022]
Root1=0.5000000+i0.866025
Root2=0.5000000-i0.866025
3. An electricity board charges the following rates for the use of electricity: for the first
200 units 80 paise per unit: for the next 100 units 90 paise per unit: beyond 300 units Rs 1
per unit. All users are charged a minimum of Rs. 100 as meter charge. If the total amount
is more than Rs 400, then an additional surcharge of 15% of total amount is charged.
Write a program to read the name of the user, number of units consumed and print out
the charges.
#include<stdio.h>
void main( )
{
char name[20];
int units;
float charges, subcharge;
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 and units <=200)
{
charges = 100 + units*0.80;
}
else if(units > 200 and units <=300)
{
charges = 100 + 200 * 0.80 + (units - 200) * 0.90;
}
else if(units > 300)
{ Choice Based Credit System (CBCS) scheme - Effective from the academic year 2021-2022]
[As per
[As per Choice Based Credit System (CBCS) scheme - Effective from the academic year 2021-2022]
Output:
1. Enter the name of the customer:
Rani
Enter the total number of units consumed:
0
User Rani need to pay 100.000000 rupees for electricity Bill
[As per Choice Based Credit System (CBCS) scheme - Effective from the academic year 2021-2022]
425
User Suraj need to pay 546.250000 rupees for electricity Bill
Output:
1. Entre the size of array: 5
Enter the elements of an array: 10 20 30 40 50
[As per Choice Based Credit System (CBCS) scheme - Effective from the academic year 2021-2022]
[As per Choice Based Credit System (CBCS) scheme - Effective from the academic year 2021-2022]
[As per Choice Based Credit System (CBCS) scheme - Effective from the academic year 2021-2022]
}
Output:
1. Enter the number of rows and columns of matrix A:
2 3
Enter the number of rows and columns of matrix B:
2 2
Matrix multiplication is not possible
2. Enter the number of rows and columns of matrix A:
2 2
Enter the number of rows and columns of matrix B:
2 2
Enter the elements of matrix A:
1111
Enter the elements of matrix B:
1111
The elements of matrix A are:
1 1
1 1
The elements of matrix B are:
1 1
1 1
The resultant matrix is:
2 2
2 2
6. Compute sin(x)/cos(x) using Taylor series approximation. Compare your resultwith the
built-in library function. Print both the results with appropriate inferences.
#include<stdio.h>
#define PI 3.142
void main( )
int i, degree;
float x, sum=0, term, num, deno;
printf(“Enter the value in degrees:\n”);
scanf(“%d”, °ree);
x=degree * (PI / 180);
num= x;
[As per Choice Based Credit System (CBCS) scheme - Effective from the academic year 2021-2022]
deno=1;
i = 2;
do
{
term=num/deno;
num=-num*x*x;
sum=sum+term;
deno =
deno*i*(i+1)); i = i +
2;
} while(fabs(term)>=0.00001);
printf(“The sine of %d is: %f\n”, degree, sum);
printf(“The sine function of %d is: %f\n” , degree,
sin(x));
}
Output:
{
printf("%d\n",a[i]);
}
[As per Choice Based Credit System (CBCS) scheme - Effective from the academic year 2021-2022]
for(i=0; i<n-1; i++)
{
for(j=0 ; j< n-i-1 ; j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("The elements after sorting are: \n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
}
Output:
char str1[30],str2[30];
printf(“Enter string1 and String2”);
scanf(“%s%s”,str1,str2);
printf(“Enter your choice:\n 1: For string copy\n 2:For string compare\n
3:For string concatenation\n 4:For string length:\n”);
scanf(“%d”,&ch);
switch(ch)
{
case 1: strcopy(str1,str2);
break;
case 2: strcomp(str1,str2);
break;
case 3: strcon(str1,str1);
break;
case 4: strlength(str1);
break;
default: printf(“Invalid choice, try with valid
input”); exit(0);
}
}
}
}
printf(“The string are equal\n”);
}
void strcon(char str1[], char str2[])
{
int i,len;
len=strlen(str1);
for(i=0;str2[i]!=’\0’;i++)
{
str1[len+i]=str2[i];
}
str1[len+i]=’\0’;
printf(“After concatenation String 2 to string 1 is: %s\n”,str1);
}
for(i=0;str1[i[!=’\0’;i++)
{
len1++;
}
printf(“The length of string1 :%d\n ”,len1);
}
Output:
[As per Choice Based Credit System (CBCS) scheme - Effective from the academic year 2021-2022]
The length of String2 is: 9
9. 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;
};
void 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%d%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;
}
© Copyright Acharya Institutes Page 24
C Programming for Problem Solving Dept. of CSE, AIT-Bangalore
[As per Choice Based Credit System (CBCS) scheme - Effective from the academic year 2021-2022]
class_avg = sum / n;
printf("The average marks of class is :%d\n", class_avg);
// 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);
}
}
}
Output:
[As per Choice Based Credit System (CBCS) scheme - Effective from the academic year 2021-2022]
67 89 45
Enter the name:
Bhoomi
Enter the roll no:
3
Enter the marks in three tests:
90 45 66
The average marks of the class is :54
Above average students:
2 Rani
3 Bhoomi
Below average students:
1 Raju
10. 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>
void main( )
{
int n, i,temp;
float a[20], sum, mean, var, sd;
float *p;
printf(“Enter the total number of elements:\n”);
scanf(“%d”, &n);
printf(“Enter the elements to an array:\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);
}
[As per Choice Based Credit System (CBCS) scheme - Effective from the academic year 2021-2022]
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);
printf("Sum = %f\n", sum);
printf("Mean = %f\n", mean);
printf("Variance = %f\n", var);
printf("Standard deviation = %f\n", sd);
}
Output:
#include<stdio.h>
int binarytodecimal(int n)
{
if(n == 0)
return 0;
else
return( n%10 + binarytodecimal (n/10) * 2 );
}
void main( )
{
int decnum, binnum;
[As per Choice Based Credit System (CBCS) scheme - Effective from the academic year 2021-2022]
Output:
1. Enter the binary number: only 0s and
1s: 1010
The decimal equivalent is: 10
2. Enter the binary number: only 0s and
1s: 0011
The decimal equivalent is: 3