Professional Documents
Culture Documents
Updated CPL Manual 2019-20
Updated CPL Manual 2019-20
of CSE, AIT-Bangalore
C PROGRAMMING LABORATORY
[As per Choice Based Credit System (CBCS) scheme - Effective from the academic year 2019-20]
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.
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.
PART-A
#include<stdio.h>
void main()
char op;
float a, b, res;
scanf("%c",&op);
scanf("%f%f",&a,&b);
switch(op)
break;
break;
break;
res = a / b;
else
exit(0);
break;
exit(0);
Output:
1. Enter an Operator :
+
Enter two Operands:
20
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
Output
1. Enter the coefficients a,b and c of the quadratic equation
111
Roots are Complex and Distinct
Root1=0.5000000+i0.866025
Root2=0.5000000-i0.866025
4. Develop a program to find the reverse of a positive integer and check for palindrome or
not. Display appropriate messages.
#include<stdio.h>
void 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;
}
Output
1.Enter the number:
1221
The reverse of 1221 is 1221
1221 is a palindrome
2. Enter the number:
123
The reverse of 123 is 321
123 is not a palindrome
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>
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)
{
printf("Invalid input\n");
}
else if(units == 0)
{
charges = 100;
}
else 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;
}
© Copyright Acharya Institutes Page 10
C Programming for Problem Solving Dept. of CSE, AIT-Bangalore
#include<stdio.h>
#include<stdlib.h>
void main( )
{
int n, a[20], key, low, high, mid ;
printf(“Enter the size of an array:\n”);
scanf(“%d”, &n);
printf(“Enter the elements to an array:\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])
{
printf(“Element found at location: %d \n”, mid + 1 );
exit(0);
}
else if(key < a[mid])
high = mid – 1;
else
low = mid +1;
}
printf(“Key not found \n” );
}
Output:
1.Entre the size of array: 5
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;
void main( )
{
int n;
checkprime( n );
}
Output:
1.Enter the value of n: 5
Number is prime
void main( )
{
int m, n, p, q, i, j, a[20] [20], b[20][20], c[20][20];
if(n!=p)
{
printf("Matrix multiplication is not possible\n");
exit(0);
}
for( j = 0; j < n ; j ++ )
{
scanf(“%d”, &a[i][j] );
}
}
Output:
1.Enter the number of rows and columns of matrix A:
23
22
22
22
1111
1111
11
11
11
11
22
22
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
void main( )
{
int i, degree;
do
{
term=num/deno;
num=-num*x*x;
sum=sum+term;
deno = deno*i*(i+1));
i = i + 2;
} while(fabs(term)>=0.00001);
Output:
1. Enter the values in degrees:90
10. Write functions to implement string operations such as compare, concatenate, string
length. Convince the parameter passing techniques.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void strcopy(char str1[30],char tr2[30]);
void strcomp(char str1[30],char str2[30]);
void strcon(char str1[30],char str2[30]);
void strlength(char str1[30]);
void main()
{
int ch;
char str1[30,str2[30];
printf(“Enter your choice:\n1: For string copy\n2:For string compare\n3:For
string concatenation\n4: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);
}
}
void strcopy(char str1[], char str2[])
{
int i;
scanf(“%s”,str2);
i=0;
while(str2[i]!=’\0’)
{
str1[i]=str2[i];
i++;
}
str1[i]=’\0’;
printf(“After copying string1 and string1 are %s %s”, str1,str2);
}
void strcomp(char str1[], char str2[])
{
int i;
printf(“Enter the string1:\n”);
scanf(“%s”,str1);
scanf(“%s”,str2);
for(i=0;str1[i]!=’\0’;i++);
{
if(str1[i]!=str2[i])
{
printf(“Strings are not equal\n”); exit(0);
}
}
printf(“The string are equal\n”);
}
void strcon(char str1[], char str2[])
{
int i,len=0;
printf(“Enter the string1:\n”);
© Copyright Acharya Institutes Page 22
C Programming for Problem Solving Dept. of CSE, AIT-Bangalore
scanf(“%s”,str1);
scanf(“%s”,str2);
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);
}
void strlength(char str1[])
{
int i,len;
printf(“Enter the string1:\n”);
scanf(“%s”,str1);
for(i=0;str1[i]!=’\0’;i++)
len++;
printf(“The length of the given string is: %d\n”,len);
}
Output:
1.Enter your choice:
11. Develop a program to sort the given set of N numbers using Bubble sort.
#include<stdio.h>
void 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("%d\n",a[i]);
return 0;
}
Output:
Enter the value for n: 5
10 90 40 60 20
10 90 40 60 20
10 20 40 60 90
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>
void 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;
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;
}
Output:
Enter the value of n: 16
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;
};
void main( )
{
struct student s[100];
int n, i, sum=0, class_avg =0;
class_avg = sum / n;
Output:
1. Enter the number of students: 3
Raju
20 30 40
Rani
67 89 45
Bhoomi
90 45 66
2 Rani
3 Bhoomi
1 Raju
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>
void main( )
{
int n, i,temp;
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;
Output:
Enter the total number of elements:5
12345
Sum=15.000000
Mean=3.000000
Variance=2.000000
Standard deviation=1.414214
#include<stdio.h>
int binarytodecimal(int n)
{
if(n == 0)
return 0;
else
return( n%10 + binarytodecimal (n/10) * 2 );
}
main( )
{
int decnum, binnum;
scanf(“%d”,&binnum);
return 0;
}
Output:
1. Enter the binary number: only 0s and 1s:
1010
0011