0% found this document useful (0 votes)
5 views26 pages

Laboratory Program1

The document outlines several laboratory programs for a C programming course, including a simple calculator, quadratic equation solver, electricity charge calculator, pattern display, binary search, matrix multiplication, Taylor series approximation for sine, and bubble sort. Each program includes code snippets, sample outputs, and explanations of the functionality. The document serves as a practical guide for students to implement and understand various programming concepts.

Uploaded by

Shreeta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views26 pages

Laboratory Program1

The document outlines several laboratory programs for a C programming course, including a simple calculator, quadratic equation solver, electricity charge calculator, pattern display, binary search, matrix multiplication, Taylor series approximation for sine, and bubble sort. Each program includes code snippets, sample outputs, and explanations of the functionality. The document serves as a practical guide for students to implement and understand various programming concepts.

Uploaded by

Shreeta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

PRINCIPLES OF PROGRAMMING USING C BPOPS103

Laboratory Program1
SIMULATION OFA SIMPLE CALCULATOR

PROGRAM

#include<stdio.h>
#include<conio.h>
void main()
{
intnum1,num2,result;
float divresult;
char op;
clrscr();
printf("Enter the operation to be done\n");
scanf("%c", &op);
printf("Enter the value of num1 and num2 \n");
scanf("%d%d",&num1,&num2);
switch(op)
{
case '+':result = num1+ num2;
printf(“Addition=%d\n”,result); break;
case '-': result = num1 - num2;
printf(“Subtraction=%d\n”,result);
break;
case '*': result = num1 * num2;
printf(“Multiplication=%d\n”,result);
break;
case'/':divresult=num1/(float)num2;
printf(“Division=%.2f\n”,divresult);
break;
default:
printf("InvalidInput,TryAgain\n");
}
}

VTU, Belagavi Page 1


PRINCIPLES OF PROGRAMMING USING C BPOPS103

OUTPUT

First Run:
Enter the operation to be done
+
Enter the value of num1and num2 10
20
Addition=30

Second Run:
Enter the operation to be done
-
Enter the value of num1 and num2 10
20
Subtraction=-10

Third Run:
Enter the operation to be done
*
Enter the value of num1 and num2 20
30
Multiplication=600

Fourth Run:
Enter the operation to be done
/
Enter the value of num1 and num2 40
50
Division=0.80

Fifth Run:
Enter the operation to be done @
Enter the value of num1 and num2 40
50
Invalid Input ,Try Again

VTU, Belagavi Page 2


PRINCIPLES OF PROGRAMMING USING C BPOPS103

Laboratory Program-2
COMPUTE THE ROOTS OF A QUADRATIC EQUATION BYACCEPTING
THE COEFFICIENTS. PRINT APPROPRIATE MESSAGES.

PROGRAM

#include <stdio.h>
#include<conio.h>
#include <math.h>
#include<stdlib.h>

Void main()
{
floata,b,c,root1,root2,realp,imagp,disc;
clrscr();
printf("\nEnter the value of coefficient a:");
scanf("%f",&a);
if(a==0)
{
printf("\n Invalid input...Retry again"); exit(0);
}
printf("Enter the value of coefficients b and c:\n");
scanf("%f%f", &b, &c);
disc=b*b-4*a*c;//compute discriminate

if(disc==0)
{
printf("The roots are real and equal\n");
root1 = root2 = -b / (2.0*a);
printf(" Root1=Root2=%.2f\n",root1);
}
else
{
if(disc>0)
{
printf("The roots are real and distinct\n");
root1 = (-b + sqrt(disc))/(2.0*a);
root2=(-b- sqrt(disc))/(2.0*a);
printf("Root1=%.2f\n",root1);
printf("Root2=%.2f\n",root2);
}
else

VTU, Belagavi Page 3


PRINCIPLES OF PROGRAMMING USING C BPOPS103

{
printf("The roots are complex\n");
realp = -b/(2.0*a);
disc=-disc;
imagp=sqrt(disc)/(2.0*a);
printf("Root1 =%.2f+ i%.2f\n",realp,imagp); printf("Root2=%.2f-i%.2f\n",realp,imagp);
}
}
getch();
}

VTU, Belagavi Page 4


PRINCIPLES OF PROGRAMMING USING C BPOPS103

OUTPUT

First Run:
Enter the value of coefficient a: 1 Enter the value of coefficients b and c: 4 4
The roots are real and equal Root1 = Root2 = -2.00

Second Run:
Enter the value of coefficient a: 1 Enter the value of coefficients b and c:
-710
The roots are real and distinct Root1 = 5.00
Root2=2.00

Third Run:
Enter the value of coefficient a: 2
Enter the value of coefficients b and c:
-36
The roots are complex Root1 = 0.75 + i 1.56
Root2=0.75- i 1.56

Fourth Run:
Enter the value of coefficienta:0
Invalid input...Retry again

VTU, Belagavi Page 5


PRINCIPLES OF PROGRAMMING USING C BPOPS103

Laboratory Program3

AN ELECTRICITY BOARD CHARGES THE FOLLOWING RATES FOR THE USE


OF ELECTRICITY: FOR THE FIRST 200 UNITS 80 PAISE PER UNIT: FOR THE NEXT
100UNITS90PAISEPERUNIT:BEYOND300UNITS RUPEES1PERUNIT.ALLUSERS ARE
CHARGED A MINIMUM OF RUPEES 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 NAMEOF THE
USER, NUMBER OF UNITS CONSUMED AND PRINT OUT THE CHARGES.

PROGRAM

#include<stdio.h>
#include<conio.h>
void main()
{
char name[10];
float unit, amt;
clrscr();
printf("Enter you name and unit Consumed:");
scanf("%s %f", &name,&unit);
if(unit<=200) amt=unit*0.80+100;
else if((unit>200)&&(unit<=300)) amt=200*0.80+((unit-200)*0.90)+100;
else
amt=200*0.80+100*0.90+((unit-300)*1)+100;
if(amt>400)

amt=1.15*amt;
printf("Name:%s\nUnit=%f\ncharge=%f",name,unit,amt); getch();
}

OUTPUT
First Run:
Enter your name and unit Consumed: Siri 52 Name:
Siri Unit=52 charge=141.600000Second Run:
Enter your name and unit Consumed:
Rajesh 460 Name: Rajesh Unit=460 charge=586.500000

VTU, Belagavi Page 6


PRINCIPLES OF PROGRAMMING USING C BPOPS103

Laboratory Program - 4

WRITE A CPROGRAM TO DISPLAY THE FOLLOWING BY READING THE NUMBER


OF ROWS AS INPUT,
1
121
12321
1234321
nth row

PROGRAM

#include <stdio.h>
#include<conio.h>
void main()
{
int i,j,n;
clrscr();
printf("Input
number of rows:");
scanf("%d", &n);
for(i=0; i<=n; i++)
{
for(j=1;j<=n-i; j++)
{
printf(" ");
}
for(j=1;j<=i;j++)
{
printf("%d",j);
}
for(j=i-1;j>=1;j--)
{
printf("%d",j);
}
printf("\n");
}
getch();
}

OUTPUT

VTU, Belagavi Page 7


PRINCIPLES OF PROGRAMMING USING C BPOPS103

First Run:

Input number of rows:4


1
121
12321
1234321

Second Run:

Inputnumberofrows:6

1
121
12321
1234321
123454321
12345654321

VTU, Belagavi Page 8


PRINCIPLES OF PROGRAMMING USING C BPOPS103

Laboratory Program - 4
IMPLEMENT BINARY SEARCH ON INTEGERS

PROGRAM
#include <stdio.h>
#include<conio.h>
void main()
{
int n,a[100],i,key,high,low,mid,loc=-1;
clrscr( );
printf("Enter the size of the array\n");
scanf("%d",&n);
printf("Enter the elements of array insorted 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])
{
loc=mid+1; break;
}
else
{
if(key<a[mid]) high=mid-1;
else low=mid+1;
}
}
if(loc>0)
printf("\nThe element %d is found at %d", key,loc);
else
printf("\nThe search is unsuccessful");
getch();
}

VTU, Belagavi Page 9


PRINCIPLES OF PROGRAMMING USING C BPOPS103

OUTPUT
First Run:
Enter the size of the array5
Enter the elements of array insorted order 10 20
30
40
50
Enter the element to be searched 40
The element 40 is found at 4 Second Run:
Enter the size of the array 4
Enter the elements of array insorted order 4
6
8
9
Enter the key element to be searched 2
The search is unsuccessful

VTU, Belagavi Page 10


PRINCIPLES OF PROGRAMMING USING C BPOPS103

Laboratory Program6

IMPLEMENT MATRIX MULTIPLICATION AND VALID ATE THE RULES


OF MULTIPLICATION.

PROGRAM

#include<stdio.h>
#include<conio.h>
void main()
{
int a[5][5],b[5][5],c[5][5],m,n,p,q,i,j,k;
clrscr();
printf("Enter the size of first matrix\n");
scanf("%d %d",&m,&n);
printf("Enter the size of second matrix\n");
scanf("%d %d",&p,&q);
if(n!=p)
printf(“Matrix multiplication is not possible”);
else
{
printf("Enter the elements of first matrix\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("Enter the elements of the second matrix\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("\nA-matrixis\n");

for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%d\t",a[i][j]);

VTU, Belagavi Page 11


PRINCIPLES OF PROGRAMMING USING C BPOPS103

printf("\n");
}
printf("\nB-matrixis\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
printf("%d\t",b[i][j]);
printf("\n");
}
printf("The product of two matricesis\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
printf("%d\t",c[i][j]);
printf("\n");
}
}
getch();
}
OUTPUT:
Enter the size of first matrix 2 3
Enter the size of second matrix 3 2
Enter the elements of first matrix 1 2 3 4 5 6
Enter the elements of the second matrix 1 2 3 4 5 6
A- matrixis
1 2 3
4 5 6
B-matrixis
1 2
3 4
5 6
The product of two matricesis
22 28
49 64

VTU, Belagavi Page 12


PRINCIPLES OF PROGRAMMING USING C BPOPS103

Laboratory Program- 7
COMPUTE SIN(X)/COS(X) USING TAYLOR SERIES APPROXIMATION.COMPARE YOUR
RESULT WITH THE BUILT-INLIBRARY FUNCTION. PRINT BOTH THE RESULTS
WITH APPROPRIATE INFERENCES.

PROGRAM

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
int fact(int m)
{
int i,f=1;
for(i=1;i<=m;i++)
{
f=f*i;
}
Return f;
}
Void main()
{
int x, n,i;
float rad, res,sum=0;
clrscr();
printf("Enter degree\n");
scanf("%d",&x);
printf("Enter number of terms\n");
scanf("%d",&n);
rad=x*3.14/180;
for(i=1;i<=n;i+=2)
{
if((i-1)%4==0)
sum=sum+pow(rad,i)/fact(i); else
sum=sum-pow(rad,i)/fact(i);
}
printf("Calculate sin(%d) = %f", x,sum);
printf("\nLibrarysin(%d)=%f",x,sin(rad));
getch();
}

VTU, Belagavi Page 13


PRINCIPLES OF PROGRAMMING USING C BPOPS103

OUTPUT

First Run:
Enter degree 30
Enter number of terms 5
Calculate sin(30)=0.499772 Library sin(30) = 0.499770

Second Run:
Enter degree 60
Enter number of terms 2
Calculate sin(60)=0.866029 Library sin(60) = 0.865760

VTU, Belagavi Page 14


PRINCIPLES OF PROGRAMMING USING C BPOPS103

Laboratory Program -8

SORT THE GIVEN SET OF N NUMBERS USING BUBBLE SORT.

PROGRAM

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,a[10],temp;
clrscr();
printf("Enter the no. of elements: \n");
scanf("%d",&n);
printf("Enter the array elements\n");
for(i = 0 ; i < n ; i++)
scanf("%d", &a[i]);
printf("The original elements are\n");
for(i = 0 ; i < n ; i++)
printf("%d", a[i]);
for(i=0;i< n-1;i++)
{
for(j= 0 ;j<(n-i)-1;j++)
{
if(a[j] > a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1]=temp;
}
}
}
printf("\nThe Sorted elements are\n");
for(i = 0 ; i < n ; i++)
printf("%d",a[i]);
getch();
}

VTU, Belagavi Page 15


PRINCIPLES OF PROGRAMMING USING C BPOPS103

OUTPUT

First Run:
Enter the no.of elements:5
Enter the array elements
3010502040
The original elements are 30 10 50 20 40
The Sorted elements are 10 20 30 40 50
Second Run:
Enter the no.of elements:6
Enter the array elements
654321
The original elements are 6 5 4 3 2 1
The Sorted elements are 1 2 3 4 5 6

VTU, Belagavi Page 16


PRINCIPLES OF PROGRAMMING USING C BPOPS103

Laboratory Program-9
WRITE FUNCTIONS TO IMPLEMENT STRING OPERATIONS SUCH AS COMPARE,
CONCATENATE, STRING LENGTH. USE THE PARAMETER PASSING TECHNIQUES.

#include <stdio.h>
// Function to find the length of a string
int stringLength(char str[])
{
int length = 0;
while (str[length] != '\0')
{
length++;
}
return length;
// Correctly returns the length of the string
}
// Function to compare two strings
int compareStrings(char str1[], char str2[])
{
for (int i = 0; str1[i] != '\0' || str2[i] != '\0'; i++)
{
if (str1[i] < str2[i]) {
return -1; // str1 is less than str2
} else if (str1[i] > str2[i]) {
return 1; // str1 is greater than str2
}
}
return 0; // Strings are equal
}
// Function to concatenate two strings
void concatenateStrings(char str1[], char str2[], char result[]) {
int i = 0, j = 0;
// Copy str1 to result
while (str1[i] != '\0') {
result[i] = str1[i];
i++;
}

VTU, Belagavi Page 17


PRINCIPLES OF PROGRAMMING USING C BPOPS103

OUTPUT

First Run:
Enter the source string1: good
Enter the source string2: night
String length of string1 is: 4
String length of string2 is: 5
strings are different concatenated string is: goodnight

Second Run: Enter the source string1: good


Enter the source string2: good
String length of string1 is: 4
String length of string2 is: 4
Both strings are same concatenated string is: goodgood

VTU, Belagavi Page 18


PRINCIPLES OF PROGRAMMING USING C BPOPS103

Laboratory Program-10

IMPLEMENTSTRUCTURES TO READ, WRITEAND COMPUTEAVERAGE- MARKS OF


THE STUDENTS,LIST THE STUDENTS SCORING ABOVE AND BELOW THE AVERAGE
MARKS FOR A CLASS OF N STUDENTS.

PROGRAM
#include<stdio.h>
#include<conio.h>
struct student
{
char usn[10];
charname[10];
int m1,m2,m3;
floatavg,total;
};
Void main()
{
Struct students[20];
int n,i;
Floattavg,sum=0.0;
clrscr();
printf("Enter the number of students");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the detail of %d students\n",i+1);
printf("\n Enter USN=");

VTU, Belagavi Page 19


PRINCIPLES OF PROGRAMMING USING C BPOPS103

scanf("%s",s[i].usn);
printf("\nEnterName=");
scanf("%s",s[i].name);
printf("\nEnterthethreesubjectsmarks\n"); scanf("%d%d
%d",&s[i].m1,&s[i].m2,&s[i].m3);
s[i].total=s[i].m1+s[i].m2+s[i].m3;
s[i].avg=s[i].total/3;
}
for(i=0;i<n;i++)
{
if(s[i].avg>=35)
printf("\n%s has scored above the average marks", s[i].name);
else
printf("\n%s has scored below the average marks", s[i].name);
}
getch();
}

VTU, Belagavi Page 20


PRINCIPLES OF PROGRAMMING USING C BPOPS103

OUTPUT
Enter the number of students2
Enter the detail of student 1
Enter USN=1
Enter Name=Arun
Enter the three-subject score
23 45 67
Enter the detail of student2
Enter USN=2
Enter Name=Tharun
Enter the three-subject score
532
Arun has scored above the average marks
Tharun has scored below the average marks

VTU, Belagavi Page 21


PRINCIPLES OF PROGRAMMING USING C BPOPS103

Laboratory Program11
DEVELOP A PROGRAM USING POINTERS TO COMPUTE THE SUM, MEAN AND
STANDARD DEVIATION OF ALL ELEMENTS STORED IN AN ARRAY OF N REAL
NUMBERS.

PROGRAM
#include<std
io.h>
#include<conio.h>
#include<math.h>

int main()
{
int n,i;
float x[20],sum,mean;
floatvariance,deviation;
clrscr();
printf("Enterthevalueofn\n");
scanf("%d",&n);
printf("enter%drealvalues\n",n);
for (i=0;i<n;i++)
{
scanf("%f",(x+i));
}
sum=0;
for(i=0;i<n;i++)
{
sum=sum+*(x+i);
}
printf("sum=%f\n",sum);
mean=sum/n;

VTU, Belagavi Page 22


PRINCIPLES OF PROGRAMMING USING C BPOPS103

sum=0;
for(i=0;i<n;i++)
{
sum=sum+(*(x+i)-mean)*(*(x+i)-mean);
}
variance=sum/n;
deviation=sqrt(variance);
printf("mean(Average)=%f\n",mean);
printf("variance=%f\n",variance);
printf("standard deviation=%f\n",deviation);
getch();
}

Output:
Enter the value of n
5
Enter the 5 real values
3
7
23
1
4
Sum=38.0000
Mean ( Average ) = 7.6000
Variance = 63.039997
Standard deviation=7.9397

VTU, Belagavi Page 23


PRINCIPLES OF PROGRAMMING USING C BPOPS103

Laboratory Program12

WRITE A C PROGRAM TO COPY A TEXT FILE TO ANOTHER, READ BOTH THE INPUT
FILE NAME AND TARGET FILE NAME.

PROGRAM

#include <stdio.h>
#include<stdlib.h>
#include<conio.h>
void main()
{
FILE*fptr1,*fptr2;
charch,fname1[20],fname2[20];
clrscr();

printf("\n\nCopy a file in another name:\n");


printf(" \n");

printf("Input the source file name:");


scanf("%s",fname1);

fptr1=fopen(fname1,"r");
if(fptr1==NULL)
{
printf("File does not found or error in opening.!!");
exit(1);
}
printf("Input then file name:");
scanf("%s",fname2);
fptr2=fopen(fname2, "w");
if(fptr2==NULL)
{
printf("File does not found or error in opening.!!");
fclose(fptr1);
exit(2);
}
VTU, Belagavi Page 24
PRINCIPLES OF PROGRAMMING USING C BPOPS103

while(1)
{
ch=fgetc(fptr1);
if(ch==EOF)
{
break;
}
else

{
fputc(ch, fptr2);
}
}
printf("The file %s copied successfully in the file %s. \n\n",fname1,fname2);
fclose(fptr1);
fclose(fptr2);
getchar();
}

VTU, Belagavi Page 25


PRINCIPLES OF PROGRAMMING USING C BPOPS103

OUTPUT:

Copy a file in another name:

Input the source file name:test.txt


Input the new file name : test1.txt
The file test.txt copied successfully in the file test1.txt.

VTU, Belagavi Page 26

You might also like