BPOPS203 PRINCIPLES OF PROGRAMMING USING C LAB Manual
BPOPS203 PRINCIPLES OF PROGRAMMING USING C LAB Manual
PRINCIPLES OF PROGRAMMING
USING C LABORATORY [ BPOPS203]
by:
Dr. A. Syed Mustafa
Prof. Madhusudhana
AND ENGINEERING
HKBK COLLEGE OF E
#include <stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
char op;
printf("Simple Calculator\n");
printf("Enter the Expression\n");
scanf("%d%c%d",&a,&op,&b); /* read input eg. 2+3 */
switch(op)
{
case '+': c=a+b; /*add 2 numbers*/
break;
getch();
}
OUTPUT:
#include <stdio.h>
#include<conio.h>
void main()
{
int a,b,c,d;
float r,r1,r2;
if(a*b*c==0)
{
printf("Please enter non zero values\n");
getch();
exit(0);
} /*end if*/
d=b*b-4*a*c;
r=2*a;
if(d==0)
{
printf("Roots are Real and Equal\n");
r1=r2=-b/r;
printf("Root 1: %.2f, Root 2: %.2f\n",r1,r2);
}
else if(d>0)
{
printf("Roots are Real and Distinct\n");
r1=(-b+sqrt(d))/r;
r2=(-b-sqrt(d))/r;
printf("Root 1: %.2f, Root 2: %.2f\n",r1,r2);
}
else
{
printf("Roots are Imaginary\n");
r1=-b/r;
r2=sqrt(-d)/r;
printf("Root 1: %.2f+i%.2f, Root 2: %.2f-i%.2f\n",r1,r2,r1,r2);
} /*end else*/
getch();
}/* end main*/
OUTPUT:
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>
#include<conio.h>
void main()
{
int n,mc=100;
float bill;
char name[50];
if(n<=200)
bill=n*0.8;
else if(n<=300)
bill=160+(n-200)*0.9;
else if(n>300)
bill=250+(n-300)*1;
if(bill>400)
bill=bill+bill*0.15;
getch();
}
OUTPUT:
4.
#include <stdio.h>
#include<conio.h>
void main()
{
int n,i,j,k,m;
printf("\nEnter the value of n:\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++) /*print spaces*/
printf(" ");
for(k=1; k<=i ; k++) /*print increment numbers*/
printf("%d ",k);
for(m=i-1; m>0; m--) /*print decrement numbers*/
printf("%d ",m);
printf("\n");
} /*end for*/
getch();
}/* end main*/
OUTPUT:
#include <stdio.h>
#include<conio.h>
void main()
{
int a[100],n,s,e,mid,key,f=0,i;
printf("\nEnter the total no. of elements:\n");
scanf("%d",&n);
printf("\nEnter the elements in ascending order\n");
for(i = 0 ; i < n ; i++) /*read all elements*/
scanf("%d",&a[i]);
if(f)
printf("Search Successful\n");
else
printf("Search UnSuccessful\n");
getch();
}/* end main*/
OUTPUT:
#include <stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],c[10][10];
int m,n,p,q,i,j,k;
printf("\nMATRIX A \n");
for(i = 0 ; i < m ; i++)
{
for(j = 0 ; j < n ; j++)
printf("%d\t", a[i][j]);
printf("\n");
}/*end for i loop*/
printf("\nMATRIX B \n");
printf("\nResultant MATRIX\n");
for(i = 0 ; i < m ; i++)
{
for(j = 0 ; j < q ; j++)
printf("%d\t", c[i][j]);
printf("\n");
} /*end for i loop*/
getch();
}/* end main*/
OUTPUT:
7. Compute sin(x)/cos(x) using Taylor series approximation. Compare your result with the
built-in library function. Print both the results with appropriate inferences.
/*Program to compute Sin(x) using Taylor series approximation given by Sin(x) = x -(x3/3!) +
(x5/5!) - (x7/7!) + ....... Compare the result with the built- in Library function and print both the
results.*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i,degree;
float x,sum=0,term,nume,deno;
do
{
term=nume/deno;
nume=-nume*x*x; /*multiply by x twice*/
deno=deno*i*(i+1); /* multiply by next and next value*/
sum=sum+term;
i=i+2;
}while(fabs(term)>=0.00001); /*check float absolute to 0.00001*/
OUTPUT:
#include <stdio.h>
#include<conio.h>
void main()
{
int n,i,j,a[100],temp;
printf("\nEnter the total no. of elements:\n");
scanf("%d",&n);
printf("\nEnter the elements\n");
getch();
}/* end main*/
OUTPUT:
9. Write functions to implement string operations such as compare, concatenate, and find
string length. Use the parameter passing techniques.
s1[len+i]=0;
}/*end concate*/
l1=slength(s1);
l2=slength(s2);
if(l1!=l2)
printf("Strings are not equal\n");
else
{
for(i=0;i<l1;i++)
if(s1[i]>s2[i])
{
printf("String %s is greater than string %s1\n",s1,s2);
return;
}/*end if*/
else if(s1[i]<s2[i])
{
printf("String %s is smaller than string %s1\n",s1,s2);
return;
}/* else if*/
void main()
{
char s1[100],s2[100];
int len;
printf("Enter the first string\n");
gets(s1);
printf("Enter the second string\n");
gets(s2);
len=slength(s1);
printf("Length of '%s' is %d\n",s1,len);
compare(s1,s2);
concat(s1,s2);
printf("concatenated string is %s\n", s1);
getch();
}
OUTPUT:
10. Implement structures to read, write and compute average- marks of the students, list the
students scoring above and below the average marks for a class of N students.
#include<stdio.h>
typedef struct /* defining structure*/
{
int rollno;
char name[50];
float marks;
}student;
void main()
{
int n,i;
float sum=0,avg;
student s[50]; /* declaring 50 students*/
printf("Enter the total no of students\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the student: %d datails:\n",i+1);
printf("Enter the student Rollno:");
scanf("%d",&s[i].rollno);
printf("Enter the student Name:");
scanf("%s",s[i].name);
printf("Enter the marks:");
scanf("%f",&s[i].marks);
sum=sum+s[i].marks;
}
avg=sum/n;
printf("Student details:\n");
printf("Rollno\tName\tMarks\n");
for(i=0;i<n;i++)
{
printf("%d\t",s[i].rollno);
printf("%s\t",s[i].name);
printf("%.2f\n",s[i].marks);
}
printf("%s\t",s[i].name);
printf("%.2f\n",s[i].marks);
} /* end if*/
} /* end for*/
getch();
} /* end main*/
OUTPUT:
11. 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>
#include<conio.h>
#include<math.h>
void main()
{
float a[10],*ptr,mean,std,sum=0,sumstd=0;
int n,i;
printf("sum=%.2f\n",sum);
printf("Mean=%.2f\n",mean);
printf("standard deviation=%.2f\n",std);
getch( );
}
OUTPUT:
12. Write a C program to copy a text file to another, read both the input file name and
target file name.
#include<stdio.h>
#include<conio.h>
void main()
{
char ch, fname1[50], fname2[50];
FILE *f1, *f2;
printf("Enter the file name to copy its content(source file)\n");
scanf("%s",fname1);
printf("Enter the file name to write the copied content(target file)\n");
scanf("%s",fname2);
f1= fopen(fname1, "r");
if (fname1 == NULL)
{
printf("Can not read file\n");
exit(0);
}
f2 = fopen(fname2, "w");
if (fname2== NULL)
{
fclose(f1);
printf("Can not write to file\n");
exit(0);
}
while ((ch = fgetc(f1)) != EOF)
fputc(ch, f2);
getch( );
}
OUTPUT:
Additional Programs
1. Write a C program to compute addition, subtraction, multiplication and division on
Complex Numbers.
#include<stdio.h>
struct complex
{
float rp;
float ip;
};
COMP readcomplex()
{
COMP c;
printf("Enter the details of Complex number:\n");
scanf("%f%f",&c.rp,&c.ip);
return(c);
}
void displaycomplex(COMP c)
{
if(c.ip<0)
printf("%.2f%.2fi\n",c.rp,c.ip);
else
printf("%.2f+%.2fi\n",c.rp,c.ip);
}
int main()
{
struct complex c1,c2,c3,c4,c5,c6;
c1=readcomplex();
c2=readcomplex();
c3=addcomplex(c1,c2);
c4=subcomplex(c1,c2);
c5=multicomplex(c1,c2);
c6=dividecomplex(c1,c2);
printf("Complex number 1 is:\n");
displaycomplex(c1);
printf("Complex number 2 is:\n");
displaycomplex(c2);
printf("Complex number addition is:\n");
displaycomplex(c3);
printf("Complex number subtraction is:\n");
displaycomplex(c4);
printf("Complex number multiplication is:\n");
displaycomplex(c5);
printf("Complex number division is:\n");
displaycomplex(c6);
return(0);
}
OUTPUT:
#include<stdio.h>
typedef struct
{
long telno;
char cname[50];
int units;
float bill;
}Tbill;
int main()
{
Tbill t1;
int fc=250;
printf("Enter the Telephone number\n");
scanf("%d",&t1.telno);
printf("Enter the consumer name\n");
scanf("%s",t1.cname);
printf("Enter the no of units used\n");
scanf("%d",&t1.units);
if(t1.units<=50)
t1.bill=t1.units*1;
else
t1.bill=t1.units*1.5;
t1.bill=fc+t1.bill;
printf("Telephone Bill Details:\n");
printf("Telephone no: %ld\n",t1.telno);
printf("conusmer Name: %s\n",t1.cname);
printf("No of units used: %d\n",t1.units);
printf("Bill Amount: %.2f\n",t1.bill);
return 0;
}
OUTPUT:
#include<stdio.h>
#include<string.h>
void main()
{
char s1[100],s2[100];
int len,i;
printf("Enter the first string\n");
gets(s1);
getch();
}
OUTPUT:
void main()
{
char s[100];
int len,i,p=1;
printf("Enter the string\n");
scanf("%s",s);
for(i=0;s[i]!=0;i++);
len=i;
for(i=0;i<len/2;i++)
if(s[i]!=s[len-1-i])
{
p=0;
break;
}
if(p)
printf("string %s is palaindrome\n",s);
else
printf("string %s is not a palaindrome\n",s);
getch();
}
OUTPUT: