Computer Programming Laboratory Manual Updated
Computer Programming Laboratory Manual Updated
PART A-List of problems for which students should develop the program and execute
in the Laboratory
#include<stdio.h>
void main()
{
int a,b,res;
char op;
printf("enter the arithmetic expression");
scanf("%d%c%d",&a,&op,&b);
switch (op)
{
case '+':res=a+b;
break;
case '-':res=a-b;
break;
case '*':res=a*b;
break;
case '%':res=a%b;
break;
case '/':if(b!=0)
res=a/b;
else
{
printf("divide by zero");
exit(0);
}
break;
default:printf("invalid expression");
}
printf("result of %d%c%d=%d",a,op,b,res);
}
2 Compare the roots of Quadratic equations by accepting the coefficient’s.print
appropriate message.
#include<stdio.h>
#include<math.h>
void main()
{
float a,b,c,x1,x2,disc;
printf("enter the values");
scanf("%f%f%f",&a,&b,&c);
disc=b*b-4*a*c;
if(disc==0)
{
printf("roots are equal");
x1=x2=-b/(2*a);
printf("x1=%f\n x2=%f\n",x1,x2);
}
else if(disc>0)
{
printf("roots are distinct");
x1=(-b+sqrt(disc))/(2*a);
x2=(-b-sqrt(disc))/(2*a);
printf("x1=%f\n x2=%f\n",x1,x2);
}
else
{
printf("roots are imaginary");
x1=-b/(2*a);
x2=sqrt(fabs(disc))/(2*a);
printf("the first root=%f+i%f\n",x1,x2);
printf("the second root=%f-i%f\n",x1,x2);
}
}
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 the total amount is
charged. Write a program to read the name of the user, the number of units consumed,
and print out the charges.
#include<stdio.h>
int main()
{
int unit;
float charges=100,amount;
char name[20];
printf("enter the name and unit consumed");
scanf("%s%d",&name,&unit);
if(unit<=200)
amount=amount+(unit*0.8);
else
{
amount=amount+(200*0.8);
unit=unit-200;
if(unit<=100)
amount=amount+(unit*0.9);
else
{
amount=amount+(100*0.9);
unit=unit-100;
amount=amount+(unit*1.0);
}
}
if(amount>400)
amount=amount+(amount*0.15);
printf("name=%s total bill=%f",name,amount);
return 0;
}
5.Binary search on integers/ Names/.
#include<stdio.h>
void main()
{
int num[10],key;
int n,i,low,high,mid;
int count=0,pos;
printf("\n enter the number of elements");
scanf("%d",&n);
printf("\n enter the elements one by one");
for(i=0;i<n;i++)
{
scanf("%d",&num[i]);
}
printf("\n enter the key elements to be searched");
scanf("%d",&key);
low=0;
high=n-1;
while(low<high)
{
mid=(low+high)/2;
if(num[mid]==key)
{
count=1;
pos=mid+1;
break;
}
else if(num[mid]<key)
low=mid+1;
else
high=mid-1;
}
if(count==1)
printf("\n the key elements found at %d",pos);
else
printf("the key element not found");
}
5.Implement the Matrix multiplication and validate the rules of multiplication.
#include<stdio.h>
int main()
{
int a[5][5],b[5][5],c[5][5],m,n,p,q,i,j,k;
printf("\nEnter the size of matrix A:");
scanf("%d%d",&m,&n);
printf("\nEnter the size of matrix B:");
scanf("%d%d",&p,&q);
if(n==p)
{
printf("\nEnter the elements of matrix A:");
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nEnter the elements of matrix B:");
for(i=0; i<p; i++)
{
for(j=0; j<q; j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0; i<n; 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("\nThe product of matrix A*B:\n");
for(i=0; i<m; i++)
{
for(j=0; j<q; j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
}
8 Bubble sort
#include<stdio.h>
void main()
{
int a[10],n,i,j,temp;
printf("enter the size of array");
scanf("%d",&n);
printf("enter the array elements");
for(i=0; i<n; i++)
{
scanf("%d",&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;
}
}
}
printf("sorted list");
for(i=0; i<n; i++)
{
printf("%d\n",a[i]);
}
}
9.Write a functions to implement string operations such as compare, concatenate, string
length, convince the parameter passing techniques.
#include<stdio.h>
#include<stdlib.h>
int string_len(char*p)
{
int c=0;
while(*(p+c)!='\0')
c++;
return c;
}
int string_concat(char s1[],char s2[])
{
int i,j;
for(i=0;s1[i]!='\0';i++)
for(j=0;s2[j]!='\0';j++,i++)
{
s1[i]=s2[j];
}
s1[i]='\0';
}
int string_match(char s1[],char s2[])
{
int i=0;
while(s1[i]==s2[i])
{
if(s1[i]=='\0'&&s2[i]=='\0')
break;
i++;
}
return s1[i]-s2[i];
}
5.Binary search on integers/ Names/.
#include<stdio.h>
void main()
{
int num[10],key;
int n,i,low,high,mid;
int count=0,pos;
printf("\n enter the number of elements");
scanf("%d",&n);
printf("\n enter the elements one by one");
for(i=0;i<n;i++)
{
scanf("%d",&num[i]);
}
printf("\n enter the key elements to be searched");
scanf("%d",&key);
low=0;
high=n-1;
while(low<high)
{
mid=(low+high)/2;
if(num[mid]==key)
{
count=1;
pos=mid+1;
break;
}
else if(num[mid]<key)
low=mid+1;
else
high=mid-1;
}
if(count==1)
printf("\n the key elements found at %d",pos);
else
printf("the key element not found");
}
Prime or not
#include<stdio.h>
int isprime(int n);
void main()
{
int n;
printf("enter the value of n");
scanf("%d",&n);
if(isprime(n))
printf("%d is prime\n",n);
else
printf("%d is not a prime\n",n);
}
int isprime(int n)
{
int i;
for(i=2;i<=n/2;i++)
{
if(n%i==0)
return 0;
}
return 1;
}
Square root
#include<stdio.h>
#include<math.h>
void main()
{
float no,sqroot,temp;
printf("enter the number");
scanf("%f",&no);
sqroot=no/2;
temp=0;
while(sqroot!=temp)
{
temp=sqroot;
sqroot=(no/sqroot+sqroot)/2;
}
printf("square root of %f is %f",no,sqroot);
}
Talyor series
#include<stdio.h>
#include<math.h>
void main()
{
int i,n;
float x,sum,t;
printf("enter the value of x and n");
scanf("%f%d",&x,&n);
x=x*3.14/180;
t=sum=x;
for(i=3;i<=n;i=i+2)
{
t=-(t*(x*x))/(i*(i-1));
sum=sum+t;
}
printf("the value of sin(%f)=%f",x,sum);
printf("the value of sin(%f)=%f",x,sin(x));
}
12. Implement Recursive functions for Binary to Decimal Conversion.
Binary to decimal
#include<stdio.h>
void main()
{
int a,res;
printf("enter the binary numbers");
scanf("%d",&a);
res=btod(a);
printf("decimal num=%d",res);
}
int btod(int num)
{
if(num==0)
return 0;
else
return(num%10+2*btod(num/10));
}
10. 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<string.h>
struct student
{
char name[20];
int rollno;
int m1,m2,m3,total;
char result[10],clas[10];
float avg;
};
void main()
{
struct student st[20];
int n,i;
printf("\n enter the number of students");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n enter the name, rollno, marks1,marks2,marks3:\n");
scanf("%s%d%d%d%d",st[i].name,&st[i].rollno,&st[i].m1,&st[i].m2,&st[i].m3);
}
for(i=0;i<n;i++)
{
st[i].total=st[i].m1+st[i].m2+st[i].m3;
st[i].avg=st[i].total/3.0;
if((st[i].m1<40)&&(st[i].m2<40)&&(st[i].m3<40))
strcpy(st[i].result,"fail");
else
strcpy(st[i].result,"pass");
if(st[i].avg>=80)
strcpy(st[i].clas,"distinction");
else if(st[i].avg>=60)
strcpy(st[i].clas,"First");
else if(st[i].avg>=40)
strcpy(st[i].clas,"Second");
else
strcpy(st[i].clas,"third");
}
printf("STUDENT RESULT\n");
printf("****************");
for(i=0;i<n;i++)
printf("\n%s\t%d\t%d\t%d\t%d\t%d\t%2f\t%s\t
%s",st[i].name,st[i].rollno,st[i].m1,st[i].m2,st[i].m3,st[i].total,st[i].avg,st[i].result,st[i].cla
s);
}
String operation (trisha)
#include<stdio.h>
int main()
{
char str1[20],str2[20];
int res, len1;
printf("\nEnter the first string");
gets(str1);
printf("\nEnter the second string");
gets(str2);
res=strcomp(str1,str2);
if(res==0)
printf("\nTwo strings are equal");
if(res>0||res<0)
printf("\nTwo strings are not equal");
len1=strlen(str1);
printf("\nlength of string1=%d",len1);
printf("\nStrings befor concatenation");
printf("\nString1=%s",str1);
printf("\nString2=%s",str2);
strcon(str1,str2);
}
int strcomp(char str1[],char str2[])
{
int i=0;
while(str1[i]!='\0'&&str2[i]!='\0')
{
if(str1[i]!=str2[i])
return str1[i]-str2[i];
i++;
}
return 0;
}
void strcon(char str1[],char str2[])
{
int i=0, j=0;
while(str1[i]!='\0')
i++;
while(str2[j]!='\0')
{
str1[i++]=str2[j++];
}
str1[i++]='\0';
printf("\nContatenated string is:%s",str1);
}
int strlen( char str[])
{
int i=0;
while(str[i]!='\0')
{
i++;
}
return i;
}
3 palindrome
#include<stdio.h>
#include<math.h>
void main()
{
int n,digit,rev=0,num;
printf("enter the value");
scanf("%d",&n);
num=n;
while(n!=0)
{
digit=n%10;
rev=(rev*10)+digit;
n=n/10;
}
if(num==rev)
printf("num is a palindrome");
else
printf("num is not a palindrome");
}