PRINCIPLES OF PROGRAMMING USING C LABORATORY [ BPOPS203 ]
Dr. A. Syed Mustafa, HKBKCE. Page 6 of 25 BPOPS Lab Programs
OUTPUT:
`
PRINCIPLES OF PROGRAMMING USING C LABORATORY [ BPOPS203 ]
Dr. A. Syed Mustafa, HKBKCE. Page 7 of 25 BPOPS Lab Programs
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.
PRINCIPLES OF PROGRAMMING USING C LABORATORY [ BPOPS203 ]
Dr. A. Syed Mustafa, HKBKCE. Page 9 of 25 BPOPS Lab Programs
5. Implement Binary Search on Integers.
#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]);
printf("\nEnter the element to be searched:\n");
scanf("%d",&key);
PRINCIPLES OF PROGRAMMING USING C LABORATORY [ BPOPS203 ]
Dr. A. Syed Mustafa, HKBKCE. Page 10 of 25 BPOPS Lab Programs
6. Implement Matrix multiplication and validate the rules of multiplication.
#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("\nEnter the order of the matrix A:\n");
scanf("%d%d",&m,&n);
printf("\nEnter the order of the matrix B :\n");
scanf("%d%d",&p,&q);
if(n!=p) /*comparing colmof Matrix A with row of Matrix B*/
{
printf("Matrix Multiplication is not possible\n");
getch();
exit(0);
} /* end if*/
printf("\nEnter the elements of matrix A\n");
for(i = 0 ; i < m ; i++)
for(j = 0 ; j < n ; j++)
scanf("%d",&a[i][j]);
printf("\nEnter 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];
}/*end for j loop*/
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");
`
PRINCIPLES OF PROGRAMMING USING C LABORATORY [ BPOPS203 ]
Dr. A. Syed Mustafa, HKBKCE. Page 11 of 25 BPOPS Lab Programs
for(i = 0 ; i < p ; i++)
{
for(j = 0 ; j < q ; j++)
printf("%d\t", b[i][j]);
printf("\n");
}/*end for i loop*/
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:
`
PRINCIPLES OF PROGRAMMING USING C LABORATORY [ BPOPS203 ]
Dr. A. Syed Mustafa, HKBKCE. Page 12 of 25 BPOPS Lab Programs
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;
printf("Enter the value of degree:");
scanf("%d",°ree);
x=degree*(3.142/180);/* converting degree to radian*/
nume=x;
deno=1;
i=2;
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*/
printf("the sine of %d is %.2f\n",degree,sum);
printf("the sine function of %d is %.2f:",degree,sin(x));
getch();
}
OUTPUT:
`
PRINCIPLES OF PROGRAMMING USING C LABORATORY [ BPOPS203 ]
Dr. A. Syed Mustafa, HKBKCE. Page 13 of 25 BPOPS Lab Programs
8. Sort the given set of N numbers using Bubble sort.
#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");
for(i = 0 ; i < n ; i++) /*read all elements*/
scanf("%d",&a[i]);
int slength(char s[])
{
int i;
for(i=0;s[i]!=0;i++);
return (i);
} /*end slength*/
void concat(char s1[100],char s2[100])
{
int len,i;
len=slength(s1);
for(i=0;s2[i]!=0;i++)
s1[len+i]=s2[i];
s1[len+i]=0;
}/*end concate*/
void compare(char s1[100],char s2[100])
{
int l1,l2,i;
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*/
printf("String %s is equal with string %s\n",s1,s2);
}/* else*/
}/* end compare*/
`
PRINCIPLES OF PROGRAMMING USING C LABORATORY [ BPOPS203 ]
Dr. A. Syed Mustafa, HKBKCE. Page 15 of 25 BPOPS Lab Programs
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:
`
PRINCIPLES OF PROGRAMMING USING C LABORATORY [ BPOPS203 ]
Dr. A. Syed Mustafa, HKBKCE. Page 16 of 25 BPOPS Lab Programs
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.
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("Average marks is %.2f\n",avg);
printf("Below average Students details:\n");
printf("Rollno\tName\tMarks\n");
for(i=0;i<n;i++)
{
if(s[i].marks<avg)
{
printf("%d\t",s[i].rollno);
`
PRINCIPLES OF PROGRAMMING USING C LABORATORY [ BPOPS203 ]
Dr. A. Syed Mustafa, HKBKCE. Page 17 of 25 BPOPS Lab Programs
printf("%s\t",s[i].name);
printf("%.2f\n",s[i].marks);
} /* end if*/
} /* end for*/
printf("Above average Students details:\n");
printf("Rollno\tName\tMarks\n");
for(i=0;i<n;i++)
{
if(s[i].marks>avg)
{
printf("%d\t",s[i].rollno);
printf("%s\t",s[i].name);
printf("%.2f\n",s[i].marks);
} /* end if*/
} /* end for*/
getch();
} /* end main*/
OUTPUT:
`
PRINCIPLES OF PROGRAMMING USING C LABORATORY [ BPOPS203 ]
Dr. A. Syed Mustafa, HKBKCE. Page 18 of 25 BPOPS Lab Programs
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("Enter the total numbers\n");
scanf("%d",&n);
printf("Enter the real numbers\n");
for(i=0;i<n;i++)
scanf("%f",&a[i]); /* Reading Input Of N Numbers*/
ptr=a; /*assigning address of array to pointer variable*/
for(i=0;i<n;i++)
sum=sum + *ptr++; /* adding real numbers stored in array through pointer*/
mean=sum/n; /* finding average or mean*/
ptr=a; /*assigning address of array to pointer variable*/
for(i=0;i<n;i++)
{
sumstd=sumstd+pow((*ptr-mean),2); /*finding standard deviation*/
ptr++; /* incrementing pointer to fetch next value*/
}
std=sqrt(sumstd/n); /*finding standard deviation*/
PRINCIPLES OF PROGRAMMING USING C LABORATORY [ BPOPS203 ]
Dr. A. Syed Mustafa, HKBKCE. Page 19 of 25 BPOPS Lab Programs
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);
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);
}
`
PRINCIPLES OF PROGRAMMING USING C LABORATORY [ BPOPS203 ]
Dr. A. Syed Mustafa, HKBKCE. Page 22 of 25 BPOPS Lab Programs
OUTPUT:
`
PRINCIPLES OF PROGRAMMING USING C LABORATORY [ BPOPS203 ]
Dr. A. Syed Mustafa, HKBKCE. Page 23 of 25 BPOPS Lab Programs
2. Write a C program to compute Electricity Bill
#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);
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:
`
PRINCIPLES OF PROGRAMMING USING C LABORATORY [ BPOPS203 ]
Dr. A. Syed Mustafa, HKBKCE. Page 24 of 25 BPOPS Lab Programs
3. Write a C program to convert a string of characters to upper case/lower case.
#include<stdio.h>
#include<string.h>
void main()
{
char s1[100],s2[100];
int len,i;
printf("Enter the first string\n");
gets(s1);
for(i=0;s1[i]!=0;i++) /* getting each char from s1*/
if(s1[i]>='A' && s1[i]<='Z')
s2[i]=s1[i]+32; /* convering to lowercase*/
else if(s1[i]>='a' && s1[i]<='z')
s2[i]=s1[i]-32; /* convering to uppercase*/
else
s2[i]=s1[i]; /*copying other than alpha character to s1 at last*/
s2[i]=0; /* adding null value to string*/
printf("string in upper lower case is %s\n",s2);
getch();
}
OUTPUT:
`
PRINCIPLES OF PROGRAMMING USING C LABORATORY [ BPOPS203 ]
Dr. A. Syed Mustafa, HKBKCE. Page 25 of 25 BPOPS Lab Programs
4. Write a C program to check whether a string is palindrome or not.
#include<stdio.h> /* srtring palindrome*/
void main()
{
char s[100];
int len,i,p=1;
printf("Enter the string\n");
scanf("%s",s);