Final CPL 18CPL17 LAB PROGRAMS Updated
Final CPL 18CPL17 LAB PROGRAMS Updated
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();
}
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();
}