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

Final CPL 18CPL17 LAB PROGRAMS Updated

This document contains a lab manual for the C Programming laboratory course at Cauvery Institute of Technology in Mandya, India. It was prepared by Prof. Sachith B.K of the Computer Science and Engineering department. The manual contains 10 programming problems covering topics like arithmetic operations, solving quadratic equations, checking palindromes, binary search of arrays, prime number checking, matrix multiplication, Taylor series approximation of sine, and string operations. Students are to write C programs to solve these problems and gain experience with basic programming concepts.

Uploaded by

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

Final CPL 18CPL17 LAB PROGRAMS Updated

This document contains a lab manual for the C Programming laboratory course at Cauvery Institute of Technology in Mandya, India. It was prepared by Prof. Sachith B.K of the Computer Science and Engineering department. The manual contains 10 programming problems covering topics like arithmetic operations, solving quadratic equations, checking palindromes, binary search of arrays, prime number checking, matrix multiplication, Taylor series approximation of sine, and string operations. Students are to write C programs to solve these problems and gain experience with basic programming concepts.

Uploaded by

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

Cauvery institute of technology

SiddaiahnaKoppalu Gate, Sundahalli, Mandya.


Approved by AICTE, Affiliated to VTU Belagavi

C Programming laboratory
(18CPL17/27)
B.E I/II- SEMESTER
Lab manual 2018-2019

Prepared by
Prof. Sachith B.K
Dept of CSE
CIT, Mandya
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>
#include<conio.h>
void main()
{
int num1, num2;
float result;
char op;
clrscr();
printf("enter the number1\n");
scanf("%d",&num1);
printf("enter the number2\n");
scanf("%d",&num2);
printf("enter the operation to perform(+,-,*,/,%)");
scanf(" %c",&op);
result=0;
switch(op)
{
case '+':result=num1+num2;
break;
case '-':result=num1-num2;
break;
case '*':result=num1*num2;
break;
case '/':if(num2==0)
{
printf("divide by zero error\n");
getch();
exit(0);
}
else
result=(float)num1/(float)num2;
break;
case '%':result=num1%num2;
break;
default:printf("invalid operator\n");
getch();
exit(0);
}
printf("the result is %d %c %d=%f\n",num1,op,num2,result);
getch();
}

3) Develop a program to compute the roots of a quadratic equation by accepting the co-
efficients. Print appropriate messages.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,b,c,d;
float r1,r2;
clrscr();
printf("enter the value of coefficents\n");
scanf("%d %d %d",&a,&b,&c);
if(a==0)
{
printf("Roots do not exist\n");
getch();
exit(0);
}
d=(b*b)-(4*a*c);
if(d==0)
{
printf("the roots are real and equal\n");
r1=r2=-b/(2*a);
printf("the roots are root1=%f\n root=%f\n",r1,r2);
}
if(d>0)
{
printf("the roots are real and equal\n");
r1=(-b+sqrt(d))/(2*a);
r2=(-b-sqrt(d))/(2*a);
printf("the roots are root1=%f\n root2=%f\n",r1,r2);
}
if(d<0)
{
printf("the roots are imaginary or complex\n");
r1=-b/(2*a);
r2=sqrt(fabs(d))/(2*a);
printf("the roots are root1=%f+i%f\n root2=%f-i%f\n", r1,r2,r1,r2);
}
getch();
}

4) Develop a program to find the reverse of a positive integer and check for palindrome
or not. Display appropriate messages.
#include<stdio.h>
#include<conio.h>
void main()
{
int m,n,rev,digit;
clrscr();
printf("Enter any positive integer\n");
scanf("%d",&n);
if(n>=0)
{
m=n;
rev=0;
while(n!=0)
{
digit=n%10;
n=n/10;
rev=rev*10+digit;
}
printf("The given positive integer is %d\n",m);
printf("The reverse of the positive integer is %d\n",rev);
if(m==rev)
printf("The number is palindrome\n");
else
printf("The number is not a palindrome\n");
}
else
printf("The number is negative, enter positive number\n");
getch();
}

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 subcharge 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>
#include<conio.h>
void main()
{
float units,total_amount;
float charge,surcharge=0;
char name[30];
clrscr();
printf("enter the name\n");
scanf("%s",name);
printf("enter the number of units consumed\n");
scanf("%f",&units);
if(units>300)
{
charge=200*0.80;
charge=charge+(100*0.90);
charge=charge+(units-300)*1;
}
else if(units>200)
{
charge=200*0.80;
charge=charge+(units-200)*0.90;
}
else
charge=units*0.80;
total_amount=charge+100;
if(total_amount>400)
surcharge=total_amount*0.15;
total_amount=total_amount+surcharge;
printf("total user amount is %f\n",total_amount);
getch();
}

6) Introduce 1D array manipulation and implement binary search.


#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],i,n,key,low,high,mid;
clrscr();
printf("enter number of array elements\n");
scanf("%d",&n);
printf("enter array elements in sorted order\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])
break;
if(key<a[mid])
high=mid-1;
if(key>a[mid])
low=mid+1;
}
if(key==a[mid])
printf("%d found at %d position\n",key,mid);
else
printf("%d not found\n",key);
getch();
}
7) Implement using functions to check whether the given number is prime and display
appropriate messages. (No built-in math function).
#include<stdio.h>
#include<conio.h>
int isprime(int m)
{
int i;
for(i=2;i<=m/2;i++)
{
if(m%i==0)
return 0;
}
return 1;
}
void main()
{
int n;
clrscr();
printf("Enter number to check whether it is prime or not\n");
scanf("%d",&n);
if(isprime(n))
printf("%d is a prime number\n",n);
else
printf("%d is not a prime number\n",n);
getch();
}
Part-B
8) Develop a program to introduce 2D array manipulation and implement matrix
multiplication and ensure the rules of multiplication are checked.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20][20],b[20][20],c[20][20],m,n,p,q,i,j,k;
clrscr();
printf("enter order of matrix a\n");
scanf("%d%d",&m,&n);
printf("enter order of matrix b\n");
scanf("%d%d",&p,&q);
if(n!=p)
{
printf("Matrix multiplication is not possible\n");
getch();
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]);
}
}
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("Matrix multiplication table is\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
getch();
}

9) Develop a program to compute sin(x) using taylor series approximation. Compare


your result with built in library function. Print both the results with appropriate
messages.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int degree,n;
float num,den,sum,x,term;
clrscr();
printf("enter the degree to find sine value\n");
scanf("%d",&degree);
x=degree*(3.14/180);
num=x;
den=1;
sum=0;
n=2;
do
{
term=num/den;
num=-num*x*x;
den=den*n*(n+1);
n=n+2;
sum=sum+term;
}while(fabs(term)>=0.0001);
printf("the sine value of %d using taylors series is %0.3f\n",degree,sum);
printf("the sine value of %d using built in function %0.3f\n",degree,sin(x));
getch();
}

10) Write functions to implement string operations such as compare, concatenate,


string length. Convince the parameter passing technique.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void str_cmp(char str1[],char str2[])
{
if(strcmp(str1,str2)==0)
printf("strings are equal\n");
else
printf("strings are not equal\n");
return;
}
void str_len(char str1[])
{
int n;
n=strlen(str1);
printf("The length of the string is %d\n",n);
return;
}
void str_con(char str1[],char str2[])
{
strcat(str1,str2);
printf("The concatenated string is %s\n",str1);
return;
}
void main()
{
char str1[20],str2[20];
clrscr();
printf("enter the string1\n");
gets(str1);
printf("enter the string2\n");
gets(str2);
str_cmp(str1,str2);
str_len(str1);
str_con(str1,str2);
getch();
}

11) Develop a program to sort the given set of N numbers using bubble sort.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],i,j,temp,n;
clrscr();
printf("enter number of array elements\n");
scanf("%d",&n);
printf("enter array elements\n");
for(i=0;i<n;i++)
scanf("%d", &a[i]);
for(j=1;j<n;j++)
{
for(i=0;i<=n-(j+1);i++)
{
if(a[i]>a[i+1])
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
}
}
printf("The sorted array elements are\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
getch();
}

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>
#include<conio.h>
void main()
{
float i,n,j;
clrscr();
i=0;
j=0.0001;
printf("Enter number to find square root\n");
scanf("%f",&n);
while(i<n)
{
if((i*i)>n)
{
i=i-j;
break;
}
i=i+j;
}
printf("Square root of %f is %0.2f\n",n,i);
getch();
}

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>
#include<conio.h>
struct student
{
int marks;
};
void main()
{
struct student s[20];
int i,n,sum=0;
float average;
clrscr();
printf("enter the number of student\n");
scanf("%d",&n);
printf("enter the marks of the student\n");
for(i=0;i<n;i++)
scanf("%d",&s[i].marks);
printf("The marks of the students are\n");
for(i=0;i<n;i++)
printf("%d\t",s[i].marks);
printf("\n");
for(i=0;i<n;i++)
sum=sum+s[i].marks;
average=(float)sum/n;
for(i=0;i<n;i++)
{
if(s[i].marks>average)
printf("student %d is scoring above average\n",i+1);
else if(s[i].marks<average)
printf("student %d is scoring below average\n",i+1);
else
printf("student %d is scoring exactly average marks\n",i+1);
}
getch();
}

14) Develop a program using pointers to compute sum, mean and standard deviation of
all elements stored in an array of n real numbers.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i,n;
float sum,mean,sum1,variance,sd,a[20];
clrscr();
printf("enter the number of array elements\n");
scanf("%d",&n);
printf("enter array elements\n");
for(i=0;i<n;i++)
scanf("%f",&a[i]);
sum=0;
for(i=0;i<n;i++)
sum=sum+*(a+i);
mean=sum/n;
sum1=0;
for(i=0;i<n;i++)
sum1=sum1+(*(a+i)-mean)*(*(a+i)-mean);
variance=sum1/n;
sd=(float)sqrt(variance);
printf("sum is %f\n",sum);
printf("mean is %f\n",mean);
printf("variance is %f\n",variance);
printf("standard deviation is %f\n",sd);
getch();
}

15) Implement recursive functions for binary to decimal conversion.


#include<stdio.h>
#include<conio.h>
int convert(int bin);
void main()
{
int bin,dec;
clrscr();
printf("enter a binary number to convert\n");
scanf("%d",&bin);
dec=convert(bin);
printf("the decimal equivalent is %d for binary number %d\n",dec,bin);
getch();
}
int convert(int bin)
{
if(bin==0)
return 0;
else
return(bin%10+2*convert(bin/10));
}

You might also like