VISVESVARAYA TECHNOLOGICAL UNIVERSITY
BELAGAVI-590018
Principles of Programming using C
(BPOPS103/203)
Department of Computer Science & Engineering
(Accredited by NBA)
MANGALORE INSTITUTE OF TECHNOLOGY &ENGINEERING
Badaga, Mijar, Moodbidri-574225
2022-2023
Principles of Programming using C BPOPS103/203
Principles of Programming using C
Semester: I/II CIE Marks: 50
Course Code: BPOPS103/203 SEE Marks: 50
Teaching Hours/week (L: T: P: S): 0:0:2:0 Exam Hours:03
Course Outcomes:
Elucidate the basic architecture and functionalities of a computer and also recognize the
CO 1. hardware parts.
Apply programming constructs of C language to solve the real-world problem
CO 2.
Explore user-defined data structures like arrays in implementing solutions to problems
CO 3. like searching and sorting.
CO 4. Explore user-defined data structures like structures, unions and pointers in implementing
solutions
CO 5. Design and Develop Solutions to problems using modular programming constructs using
functions
Syllabus:
Laboratory Programs.
PART-A
1. Develop a program to simulate a simple calculator.
2. Develop a program to compute the roots of a quadratic equation by accepting the
coefficients. Print appropriate messages
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.
Dept. of CS&E, MITE, Moodbidri Page i
Principles of Programming using C BPOPS103/203
4. Write a C Program to display the following by reading the number of rows as input,
5. Implement Binary search on Integers/Names.
6. Develop a program to implement Matrix multiplication and validate the rules of
multiplication are checked.
7. Develop a Program to 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.
8. Develop a program to sort the given set of N numbers using Bubble sort.
9. Write functions to implement string operations such as compare, concatenate, string
length. Convince the parameter passing techniques.
10. Implement structures to read, write, compute average- marks and the students scoring
above and below the average marks for a class of N students.
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.
12.Write functions to implement string operations such as compare, concatenate, string length.
Convince the parameter passing techniques.
Dept. of CS&E, MITE, Moodbidri Page ii
Principles of Programming using C BPOPS103/203
Conduct of Practical Examination:
On completion of every experiment/program in the laboratory, the students shall be evaluated and marks
shall be awarded on the same day.
The 15 marks are for conducting the experiment and preparation of the laboratory record, the other 05
marks shall be for the test conducted at the end of the semester.
• The CIE marks awarded in the case of the Practical component shall be based on the continuous
evaluation of the laboratory report. Each experiment report can be evaluated for 10 marks. Marks of all
experiments’ write-ups are added and scaled down to 15 marks.
• The laboratory test (duration 03 hours) at the end of the 15th week of the semester /after completion of
all the experiments (whichever is early) shall be conducted for 50 marks and scaled down to 05 marks.
Scaled-down marks of write-up evaluations and tests added will be CIE marks for the laboratory component
of IC/IPCC for 20 marks.
Dept. of CS&E, MITE, Moodbidri Page iii
Principles of Programming using C BPOPS103/203
Lab Programs
1. 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)
Algorithm:-
Step 1 : Start
Step 2 : Read
a, op, b
Step 3 :
switch (op)
case ‘+’ : res← a + b
Print “res”
goto Step 4
case ‘-’ : res← a - b
Print “res”
goto Step 4
case ‘*’ : res← a * b
Print “res”
goto Step 4
case ‘/’ : if(b==0)
Print “ Divide by Zero error”
goto Step 4
else
res← a / b
Print “res”
goto Step4
end if
Step 4 : Stop
Dept. of CS&E, MITE, Moodbidri Page 4
Principles of Programming using C BPOPS103/203
Program:-
#include<stdio.h>
#include<stdlib.h>
void main()
{
float a,b,res;
char op;
printf("Enter a valid Expression\n");
scanf("%f%c%f",&a,&op,&b);
switch(op)
{
case '+' : res=a+b;
printf("\n The Resultant of %f %c %f = %f\n",a,op,b,res);
break;
case '-' : res=a-b;
printf("\n The Resultant of %f %c %f = %f\n",a,op,b,res);
break;
case '*' : res=a*b;
printf("\n The Resultant of %f %c %f = %f\n",a,op,b,res);
break;
case '/' : if(b==0)
{
printf("Divide by Zero Error\n");
exit(0);
}
else
res=a/b;
printf("\n The Resultant of %f %c %f = %f\n",a,op,b,res);
break;
default : printf("Invalid Expression\n");
break;
}
}
Dept. of CS&E, MITE, Moodbidri Page 5
Principles of Programming using C BPOPS103/203
Output:-
RUN 1 :-
mite@mite-Veriton-Series:~/Desktop/POP$ cc prog1.c
mite@mite-Veriton-Series:~/Desktop/ POP $ ./a.out
Enter a valid Expression
7+8
The Resultant of 7.000000 + 8.000000 = 15.000000
RUN 2 :-
mite@mite-Veriton-Series:~/Desktop/ POP$ ./a.out
Enter a valid Expression
2-9
The Resultant of 2.000000 - 9.000000 = -7.000000
RUN 3 :-
mite@mite-Veriton-Series:~/Desktop/ POP$ ./a.out
Enter a valid Expression
7*3
The Resultant of 7.000000 * 3.000000 = 21.000000
RUN 4 :-
mite@mite-Veriton-Series:~/Desktop/ POP$ ./a.out
Enter a valid Expression
5/3
The Resultant of 5.000000 / 3.000000 = 1.666667
RUN 5 :-
mite@mite-Veriton-Series:~/Desktop/ POP$ ./a.out
Enter a valid Expression
8/0
Divide by Zero Error
mite@mite-Veriton-Series:~/Desktop/ POP$
Dept. of CS&E, MITE, Moodbidri Page 6
Principles of Programming using C BPOPS103/203
2. Develop a program to compute the roots of a quadratic equation by
accepting the coefficients. Print appropriate messages
Algorithm:-
Step 1: Start
Step 2: Read a,b,c
Step 3: if(a==0)
Print “Invalid Input”
end if
Step 4: disc← b*b-4*a*c
Step 5: if (disc>0) then
print “ Roots are Real and Distinct”
x1← (-b) + sqrt(disc)/(2*a)
x2← (-b) – sqrt(disc)/(2*a)
Print “x1,x2”
end if
Step 6 : if (disc==0) then
print “ Roots are Real and Equal”
x1← (-b) / (2*a)
Print “x1”
end if
Step 7: if (disc<0) then
print “ Roots are Imaginary”
p← (-b) /(2*a)
q← sqrt(fabs(disc))/(2*a)
x1← p +i q
x2← p –i q
Print “x1,x2”
end if
Step 8: Stop
Dept. of CS&E, MITE, Moodbidri Page 7
Principles of Programming using C BPOPS103/203
Program:-
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
void main()
{
float a,b,c,x1,x2,disc;
printf("Enter the values of a,b,c\n");
scanf("%f%f%f",&a,&b,&c);
if(a==0)
{
printf("Invalid Input\n");
exit(0);
}
disc=b*b-4*a*c;
if(disc>0)
{
printf("Roots are Real and Distinct\n");
x1=((-b)+sqrt(disc))/(2*a);
x2=((-b)-sqrt(disc))/(2*a);
printf("Root1= %f\n Root2= %f\n",x1,x2);
}
else if(disc==0)
{
printf("Roots are Real and Equal\n ");
x1=(-b)/(2*a);
printf("Root1=Root2=%f\n",x1);
}
else
{
printf("Roots are Imaginary\n");
x1=(-b)/(2*a);
x2=(sqrt(fabs(disc)))/(2*a);
printf("Root1= %f +i %f\n",x1,x2);
printf("Root2= %f -i %f\n",x1,x2);
}
}
Dept. of CS&E, MITE, Moodbidri Page 8
Principles of Programming using C BPOPS103/203
Output:-
RUN 1 :-
mite@mite-Veriton-Series:~/Desktop/ POP$ cc prog3.c -lm
mite@mite-Veriton-Series:~/Desktop/ POP$ ./a.out
Enter the values of a,b,c
121
Roots are Real and Equal
Root1=Root2=-1.000000
RUN 2 :-
mite@mite-Veriton-Series:~/Desktop/ POP $ ./a.out
Enter the values of a,b,c
152
Roots are Real and Distinct
Root1= -0.438447
Root2= -4.561553
RUN 3 :-
mite@mite-Veriton-Series:~/Desktop/ POP$ ./a.out
Enter the values of a,b,c
123
Roots are Imaginary
Root1= -1.000000 +i 1.414214
Root2= -1.000000 -i 1.414214
RUN 4 :-
mite@mite-Veriton-Series:~/Desktop/ POP$ ./a.out
Enter the values of a,b,c
057
Invalid Input
Dept. of CS&E, MITE, Moodbidri Page 9
Principles of Programming using C BPOPS103/203
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.
Algorithm:-
Step 1 : Start
Step 2 : Read name,units
Step 3: if(units<=200) then
ta← (units*0.80)+100
end if
goto Step
Step 4: if(units>200 && units<=300) then
ta← (200*0.80+(units-200)*0.90)+100
end if
goto Step
Step 5 : if(units>300) then
ta← ta← (200*0.80+100*0.90+(units-300)*1)+100
end if
goto Step 6
Step 7 : if(ta>400) then
ta← ta+(ta*0.15)
end if
goto Step8
Step 8 : Print “name and total amount to be paid is ta ”
Step 9: Stop
Dept. of CS&E, MITE, Moodbidri Page 10
Principles of Programming using C BPOPS103/203
Program:-
#include<stdio.h>
void main()
{
char name[20];
int units,mtc=100;
float charge,ta;
printf("Enter the consumer name\n");
scanf("%s",name);
printf("Enter the units consumed\n");
scanf("%d",&units);
if(units<=200)
{
charge=units*0.80;
ta=charge+mtc;
}
else if(units>200&&units<=300)
{
charge=200*0.80+(units-200)*0.90;
ta=charge+mtc;
}
else
{
charge=200*0.80+100*0.90+((units-300)*1);
ta=charge+mtc;
}
if(ta>400)
ta=ta+(ta*0.15);
printf("%s has Consumed %d units\n Hence the total amount to be paid is
%f Rupees only",name,units,ta);
}
Dept. of CS&E, MITE, Moodbidri Page 11
Principles of Programming using C BPOPS103/203
Output:-
RUN 1 :-
mite@mite-Veriton-Series:~/Desktop/ POP $ cc prog3.c
mite@mite-Veriton-Series:~/Desktop/ POP6$ ./a.out
Enter the consumer name
Bob
Enter the units consumed
483
Bob has Consumed 483 units
Hence the total amount to be paid is 612.950012 Rupees only
RUN 2 :-
mite@mite-Veriton-Series:~/Desktop/ POP $ ./a.out
Enter the consumer name
jack
Enter the units consumed
250
jack has Consumed 250 units
Hence the total amount to be paid is 305.000000 Rupees only
RUN 3 :-
mite@mite-Veriton-Series:~/Desktop/ POP $ ./a.out
Enter the consumer name
Alice
Enter the units consumed
180
Alice has Consumed 180 units
Hence the total amount to be paid is 244.000000 Rupees only
Dept. of CS&E, MITE, Moodbidri Page 12
Principles of Programming using C BPOPS103/203
4. Write a C Program to display the following by reading the number of rows as
input,
#include <stdio.h>
void main()
{
int i,j,n;
printf("Input number of rows : ");
scanf("%d",&n);
for(i=0;i<=n;i++)
{
/* print blank spaces */
for(j=1;j<=n-i;j++)
printf(" ");
/* Display number in ascending order upto middle*/
for(j=1;j<=i;j++)
printf("%d",j);
/* Display number in reverse order after middle */
for(j=i-1;j>=1;j--)
printf("%d",j);
printf("\n");
}
}
Dept. of CS&E, MITE, Moodbidri Page 13
Principles of Programming using C BPOPS103/203
RUN 1 :-
mite@mite-Veriton-Series:~/Desktop/ POP $ cc prog4.c
mite@mite-Veriton-Series:~/Desktop/ POP$ ./a.out
Input the number of rows: 5
Dept. of CS&E, MITE, Moodbidri Page 14
Principles of Programming using C BPOPS103/203
5. Implement Binary search on Integers/Names.
Algorithm:-
Step 1 : Start
Step 2 : Read n
Step 3: for i← 0 to n do
Read a[i]
end for
Step 4 : Read key
Step 5: Initialize low← 0, high← n-1
Step 6 : REPEAT
mid← (low+high)/2
if(key==a[mid]) then
flag← 1
goto Step 7
end if
if( key>a[mid]) then
low← mid+1
else
high←mid-1
end if
UNTIL (low<=high)
Step 7 : if(flag==1) then
Print “Element found at mid+1 position”
else
Print “Element not found ”
end if
Step 8 : Stop
Dept. of CS&E, MITE, Moodbidri Page 15
Principles of Programming using C BPOPS103/203
Program:-
#include<stdio.h>
void main()
{
int a[100],n,i,low,high,mid,key,flag=0;
printf("Enter the size of the array\n");
scanf("%d",&n);
printf("Enter %d elements in ascending order\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the element to be searched\n" );
scanf("%d",&key);
low=0;
high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if(key==a[mid])
{
flag=1;
break;
}
else
if(key>a[mid])
low=mid+1;
else
high=mid-1;
}
if(flag==1)
printf("Element found at position %d\n",mid+1);
else
printf("Element not found\n");
}
Dept. of CS&E, MITE, Moodbidri Page 16
Principles of Programming using C BPOPS103/203
Output:-
RUN 1 :-
mite@mite-Veriton-Series:~/Desktop/ POP $ cc prog5.c
mite@mite-Veriton-Series:~/Desktop/ POP $ ./a.out
Enter the size of the array
10
Enter 10 elements in ascending order
2 4 6 8 10 12 14 16 18 20
Enter the element to be searched
14
Element found at position 7
RUN 2 :-
mite@mite-Veriton-Series:~/Desktop/ POP ./a.out Enter
the size of the array
10
Enter 10 elements in ascending order
1 2 3 4 5 6 7 8 9 10
Enter the element to be searched
13
Element not found
Dept. of CS&E, MITE, Moodbidri Page 17
Principles of Programming using C BPOPS103/203
6. Develop a program to Implement Matrix multiplication and ensure
the rules of multiplication are checked.
Algorithm:-
Step 1 : Start
Step 2 : Read m,n,p,q
Step 3 : if(n!=p) then
Print “Matrix Multiplication is not Possible”
end if
goto Step
Step 4 : Read Matrix a
for i←0 to m do
for j←0 to n do
Read a[i][j]
end for
end for
Step 5 : Read Matrix b
for i←0 to p do
for j←0 to q do
Read b[i][j]
end for
end for
Step 6 : for i←0 to m do
for j←0 to q do
c[i][j] ← 0
for k←0 to n do
c[i][j] ← c[i][j]+a[i][k]*b[k][j]
end for
end for
end for
Step 7: Print “Resultant Matrix”
for i←0 to m do
for j←0 to q do
Print “ c[i][j]”
end for
end for
Step 8 : Stop
Dept. of CS&E, MITE, Moodbidri Page 18
Principles of Programming using C BPOPS103/203
Program:-
#include<stdio.h>
void main()
{
int a[50][50],b[50][50],c[50][50];
int m,n,p,q,i,j,k;
printf("Enter the order of Matrix A\n");
scanf("%d%d",&m,&n);
printf("Enter the order of Matrix B\n");
scanf("%d%d",&p,&q);
if(n!=p)
{
printf("Matrix Multiplication Not Possible\n");
}
else
{
printf("Enter the elements of Matrix A\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Matrix A is displayed as\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("Enter the elements of Matrix B\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
scanf("%d",&b[i][j]);
}
Dept. of CS&E, MITE, Moodbidri Page 19
Principles of Programming using C BPOPS103/203
}
printf("Matrix B is displayed as\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
printf("\n***** MATRIX MULTIPLICATION *****\n");
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("The resultant Matrix is\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
}
Dept. of CS&E, MITE, Moodbidri Page 20
Principles of Programming using C BPOPS103/203
Output:-
RUN 1 :-
mite@mite-Veriton-Series:~/Desktop/ POP $ cc prog6.c
mite@mite-Veriton-Series:~/Desktop/ POP $ ./a.out
Enter the order of Matrix A
22
Enter the order of Matrix B
22
Enter the elements of Matrix A
123 4
Matrix A is displayed as
1 2
3 4
Enter the elements of Matrix B
5678
Matrix B is displayed as
5 6
7 8
***** MATRIX MULTIPLICATION *****
The resultant Matrix is
19 22
43 50
RUN 2 :-
mite@mite-Veriton-Series:~/Desktop/ POP $ ./a.out
Enter the order of Matrix A
26
Enter the order of Matrix B
33
Matrix Multiplication Not Possible
Dept. of CS&E, MITE, Moodbidri Page 21
Principles of Programming using C BPOPS103/203
7. Develop a Program to compute Sin(x) using Taylor series approximation.
Compare your result with the built- in Library function. Print both the results
with appropriate messages.
Algorithm:-
Step 1 : Start
Step 2 : Read degree
Step 3 : initialize sum←0, pi←3.142
Step 4 : x←(pi*degree)/180
Step 5: num←x, den← 1, i←2
Step 6 : REPEAT Step 6
term←num/den
sum←sum+ term
num← -num*x*x
den←den*i*(i+1)
i←i+2
UNTIL (fabs(term)>0.000001)
Step 7 : Print “value of sin(degree) = sum without using library function ”
Step 8 : Print “value of sin(degree) = sin(x) using library function ”
Step 9 : Stop
Dept. of CS&E, MITE, Moodbidri Page 22
Principles of Programming using C BPOPS103/203
Program:-
#include<stdio.h>
#include<math.h>
#define pi 3.142
void main()
{
int degree,i;
float x,num,den,term,sum=0;
printf("Enter the value of degrees\n");
scanf("%d",°ree);
x=(degree*pi)/180;
num=x;
den=1;
i=2;
do
{
term=num/den;
sum=sum+term;
num=-num*x*x;
den=den*i*(i+1);
i=i+2;
}while(fabs(term)>0.000001);
printf("sin(%d)= %f without using library function\n",degree,sum);
printf("sin(%d)= %f using library function\n",degree,sin(x));
}
Dept. of CS&E, MITE, Moodbidri Page 23
Principles of Programming using C BPOPS103/203
Output:-
RUN 1 :-
mite@mite-Veriton-Series:~/Desktop/ POP $ cc prog7.c -lm
mite@mite-Veriton-Series:~/Desktop/ POP $ ./a.out
Enter the value of degrees
45
sin(45)= 0.707179 without using library function
sin(45)= 0.707179 using library function
RUN 2 :-
mite@mite-Veriton-Series:~/Desktop/ POP $ ./a.out
Enter the value of degrees
54
sin(54)= 0.809089 without using library function
sin(54)= 0.809089 using library function
mite@mite-Veriton-Series:~/Desktop/ POP $
Dept. of CS&E, MITE, Moodbidri Page 24
Principles of Programming using C BPOPS103/203
8. Write functions to implement string operations such as compare,
concatenate, string length. Convince the parameter passing techniques.
Algorithm:-
Step 1 : Start
Step 2 : REPEAT Step 2
Print “String Operations”
1. String Compare 2. String Concatenate 3. String Length 4. Exit
Step 3 : Read choice
Step 4 : switch (Choice)
case 1: Read str1,str2
compare(str1,str2)
break
case 2 : Read str1,str2
concatenate(str1,str2)
break
case 3 : Read str1
stringlength(str1)
break
default : Print “Invalid Choice”
UNTIL(!choice ==4)
Step 5 : Stop
function compare(str1,str2)
len1←strlen(str1)
len2←strlen(str2)
if(len1!=len2) then
Print “Strings are Different”
end if
for i←0 to str1[i]!= ‘\0’ do
if(str1[i]!=str2[i]) then
flag=0
end if
end for
if(flag==0) then
Print “Strings are Different”
else
Print “Strings are Same”
end if
Dept. of CS&E, MITE, Moodbidri Page 25
Principles of Programming using C BPOPS103/203
function concatenate(str1,str2)
for i←0 to str1[i]!= ‘\0’ do
str3[i] ←str1[i]
k←k+1
end for
for i←0 to str2[i]!= ‘\0’ do
str3[k] ←str2[i]
k←k+1
end for
str3[k] ← ‘\0’
Print “The Concatenated String is = str3”
function stringlength(str1)
for i←0 to str1[i]!= ‘\0’ do
count←count+1
end for
Print “The length of the String = count”
Dept. of CS&E, MITE, Moodbidri Page 26
Principles of Programming using C BPOPS103/203
Program:-
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void compare(char str1[50],char str2[50]);
void concatenate(char str1[50],char str2[50]);
void stringlength(char str1[50]);
void main()
{
char str1[50],str2[50];
int choice;
do
{
printf("\n STRING OPERATIONS\n");
printf("1:String Compare\t 2: String Concatenate\n");
printf("3: String Length \t 4: Exit\n");
printf("Enter your Choice :");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter two Strings\n");
scanf("%s%s",str1,str2);
compare(str1,str2);
break;
case 2: printf("Enter two Strings\n");
scanf("%s%s",str1,str2);
concatenate(str1,str2);
break;
case 3: printf("Enter a String\n");
scanf("%s",str1);
stringlength(str1);
break;
default: printf("\nYou have Either Exited or have entered an Invalid
choice\n");
}
}while(!(choice==4));
}
Dept. of CS&E, MITE, Moodbidri Page 27
Principles of Programming using C BPOPS103/203
void compare(char str1[50],char str2[50])
{
int len1,len2,i,flag=1;
len1=strlen(str1);
len2=strlen(str2);
if(len1!=len2)
{
printf("\n Strings are different\n");
}
else
{
for(i=0;str1[i]!='\0';i++)
{
if(str1[i]!=str2[i])
{
flag=0;
}
}
if(flag==0)
printf("\n Strings are different\n");
else
printf("\n Both the strings are same\n");
}
}
void concatenate(char str1[50],char str2[50])
{
char str3[100];
int i,k=0;
for(i=0;str1[i]!='\0';i++)
{
str3[i]=str1[i];
k=k+1;
}
for(i=0;str2[i]!='\0';i++)
{
str3[k]=str2[i];
k=k+1;
}
Dept. of CS&E, MITE, Moodbidri Page 28
Principles of Programming using C BPOPS103/203
str3[k]='\0';
printf("The Concatenated String =%s",str3);
}
void stringlength(char str1[50])
{
int count=0,i;
for(i=0;str1[i]!='\0';i++)
{
count=count+1;
}
printf("The Length of the string = %d\n",count);
}
Dept. of CS&E, MITE, Moodbidri Page 29
Principles of Programming using C BPOPS103/203
Output:-
RUN 1 :-
mite@mite-Veriton-Series:~/Desktop/ POP $ cc prog8.c
mite@mite-Veriton-Series:~/Desktop/ POP $ ./a.out
STRING OPERATIONS
1:String Compare 2: String Concatenate
3: String Length 4: Exit
Enter your Choice :1
Enter two Strings
mite mite
Both the strings are same
STRING OPERATIONS
1:String Compare 2: String Concatenate
3: String Length 4: Exit
Enter your Choice :2
Enter two Strings
Anushka
Kholi
The Concatenated String =AnushkaKholi
STRING OPERATIONS
1:String Compare 2: String Concatenate
3: String Length 4: Exit
Enter your Choice :3
Enter a String
Rockstar
The Length of the string = 8
STRING OPERATIONS
1:String Compare 2: String Concatenate
3: String Length 4: Exit
Enter your Choice :4
You have Either Exited or have entered an Invalid choice
Dept. of CS&E, MITE, Moodbidri Page 30
Principles of Programming using C BPOPS103/203
9. Develop a program to sort the given set of N numbers using Bubble
sort.
Algorithm:-
Step 1: Start
Step 2 : Read n
Step 3: for i←0 to n do
Read a[i]
end for
Step 4: for i← 1 to n do
for j← 0 to n-i do
if (a[j]>a[j+1]) then
temp←a[j]
a[j] ←a[j+1]
a[j+1] ←temp
end if
end for
end for
Step 5 : Print “Sorted Array”
for i←0 to n do
Print “a[i]”
end for
Step 6 : Stop
Dept. of CS&E, MITE, Moodbidri Page 31
Principles of Programming using C BPOPS103/203
Program:-
#include<stdio.h>
void main()
{
int a[50],n,i,j,temp;
printf("Enter the number of elements\n");
scanf("%d",&n);
printf("Enter %d elements\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("The entered elements are\n");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
printf("\n***** SORTING ******\n");
for(i=1;i<n;i++)
{
for(j=0;j<n-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("The sorted elements are\n");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
}
Dept. of CS&E, MITE, Moodbidri Page 32
Principles of Programming using C BPOPS103/203
Output:-
RUN 1 :-
mite@mite-Veriton-Series:~/Desktop/ POP $ cc prog9.c
mite@mite-Veriton-Series:~/Desktop/ POP $ ./a.out
Enter the number of elements
10
Enter 10 elements
10 -5 2 -25 8 -15 4 -10 6 0
The entered elements are
10 -5 2 -25 8 -15 4 -10 6 0
***** SORTING ******
The sorted elements are
-25 -15 -10 -5 0 2 4 6 8 10
Dept. of CS&E, MITE, Moodbidri Page 33
Principles of Programming using C BPOPS103/203
10. Implement structures to read, write, compute average- marks and the
students scoring above and below the average marks for a class of N students.
Algorithm:-
Step 1 : Start
Step 2 : Define a Structure Student with members rollno,name,marks,grade
Define a structure variable s
Step 3 : Read n
Step 4: for i←0 to n do
Read s[i].rollno
Read s[i].name
Read s[i].marks
Read s[i].grade
sum←sum+s[i].marks
end for
Step 5 : avg←sum/n
Step 6 : Display the Student details
for i←0 to n do
Print “s[i].rollno,s[i].name,s[i].marks,s[i].grade”
end for
Step 7 : for i←0 to n do
if(s[i].marks>avg)then
Print “Details of student who scored more than average
marks”
else
Print “Details of student who scored below than average
marks”
end if
end for
Step 8 : Stop
Dept. of CS&E, MITE, Moodbidri Page 34
Principles of Programming using C BPOPS103/203
Program:-
#include<stdio.h>
#include<string.h>
struct student
{
int rollno;
char name[20];
int marks;
char grade[1];
};
void main()
{
struct student s[20];
char sname[20];
int i,n;
float sum=0,avg=0;
printf("Enter the number of students\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter student %d details\n",i+1);
printf("\nEnter Roll Number :");
scanf("%d",&s[i].rollno);
printf("Enter Name :");
scanf("%s",s[i].name);
printf("Enter Marks :");
scanf("%d",&s[i].marks);
printf("Enter Grade :");
scanf("%s",s[i].grade);
sum+=s[i].marks;
}
avg=sum/n;
printf("\nAverage Marks= %f\n",avg);
printf("\n Student Details are \n");
printf("ROLL_NO.\t NAME\t MARKS\t GRADE\n");
for(i=0;i<n;i++)
{
printf("%d\t\t%s\t%d\t%s\n",s[i].rollno,s[i].name,s[i].marks,s[i].grade);
}
printf("\nDetails of students who scored above Average Marks\n");
printf("ROLL_NO.\t NAME\t MARKS\t GRADE\n");
for(i=0;i<n;i++)
{
Dept. of CS&E, MITE, Moodbidri Page 35
Principles of Programming using C BPOPS103/203
if(s[i].marks>=avg)
printf("%d\t\t%s\t%d\t%s\n",s[i].rollno,s[i].name,s[i].marks,s[i].grade);
}
printf("\nDetails of students who scored below Average Marks\n");
printf("ROLL_NO.\t NAME\t MARKS\t GRADE\n");
for(i=0;i<n;i++)
{
if(s[i].marks<avg)
printf("%d\t\t%s\t%d\t%s\n",s[i].rollno,s[i].name,s[i].marks,s[i].grade);
}
}
Dept. of CS&E, MITE, Moodbidri Page 36
Principles of Programming using C BPOPS103/203
Output:-
RUN 1 :-
mite@mite-Veriton-Series:~/Desktop/ POP $ cc prog10.c
mite@mite-Veriton-Series:~/Desktop/ POP$ ./a.out Enter
the number of students
4
Enter student 1 details
Enter Roll Number :7
Enter Name :jack
Enter Marks :75
Enter Grade :A
Enter student 2 details
Enter Roll Number :9
Enter Name :oogy
Enter Marks :88
Enter Grade :S
Enter student 3 details
Enter Roll Number :21
Enter Name :kris
Enter Marks :100
Enter Grade :S
Enter student 4 details
Enter Roll Number :25
Enter Name :olivia
Enter Marks :55
Enter Grade :C
Average Marks= 79.500000
Dept. of CS&E, MITE, Moodbidri Page 37
Principles of Programming using C BPOPS103/203
Student Details are
ROLL_NO. NAME MARKS GRADE
7 jack 75 A
9 oogy 88 S
21 kris 100 S
25 olivia 55 C
Details of students who scored above Average Marks
ROLL_NO. NAME MARKS GRADE
9 oogy 88 S
21 kris 100 S
Details of students who scored below Average Marks
ROLL_NO. NAME MARKS GRADE
7 jack 75 A
25 olivia 55 C
Dept. of CS&E, MITE, Moodbidri Page 38
Principles of Programming using C BPOPS103/203
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.
Algorithm:-
Step 1: Start
Step 2 : Read n, *ptr
Step 3 : for i← 0 to n do
Read a[i]
end for
Step 4 : ptr←a
Step 5 : for i←0 to n do
sum←sum+ *ptr
ptr←ptr+1;
end for
Step 6 : mean←sum/n
Step 7 : for i←0 to n do
sumvar←sumvar+pow( (*ptr-mean),2)
ptr←ptr+1
end for
Step 8 : var←sumvar/n
Step 9 : sd←sqrt(num)
Step 10 : Print “sum,mean,sd”
Step 11 : Stop
Dept. of CS&E, MITE, Moodbidri Page 39
Principles of Programming using C BPOPS103/203
Program:-
#include<stdio.h>
#include<math.h>
void main()
{
float a[50],sum=0,sumvar=0,mean,var,sd;
float *ptr;
int n,i;
printf("Enter the number of elements\n");
scanf("%d",&n);
printf("Enter %d array elements\n",n);
for(i=0;i<n;i++)
{
scanf("%f",&a[i]);
}
ptr=a;
for(i=0;i<n;i++)
{
sum=sum+*ptr;
ptr++;
}
mean=sum/n;
ptr=a;
for(i=0;i<n;i++)
{
sumvar=sumvar+(pow((*ptr-mean),2));
ptr++;
}
var=sumvar/n;
sd=sqrt(var);
printf("Sum = %f\n",sum);
printf("Mean = %f\n",mean);
printf("Standard Deviation = %f\n",sd);
}
Dept. of CS&E, MITE, Moodbidri Page 40
Principles of Programming using C BPOPS103/203
Output:-
RUN 1 :-
mite@mite-Veriton-Series:~/Desktop/ POP $ cc prog11.c -lm
mite@mite-Veriton-Series:~/Desktop/ POP $ ./a.out
Enter the number of elements
5
Enter 5 array elements
2.1 4.2 6.3 8.4 10.5
Sum = 31.500000
Mean = 6.300000
Standard Deviation = 2.969849
Dept. of CS&E, MITE, Moodbidri Page 41
Principles of Programming using C BPOPS103/203
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>
int main() {
FILE *input_file, *target_file;
char input_file_name[100], target_file_name[100];
char ch;
// read the input file name
printf("Enter the input file name: ");
scanf("%s", input_file_name);// give only file name with extension
// open the input file
input_file = fopen(input_file_name, "r");// Write entire path of file
if (input_file == NULL) {
printf("Error: could not open input file %s\n", input_file_name);
return 1;
}
// read the target file name
printf("Enter the target file name: ");
scanf("%s", target_file_name);
// open the target file
target_file = fopen(target_file_name, "w");
if (target_file == NULL) {
printf("Error: could not open target file %s\n", target_file_name);
return 1;
}
// copy the contents of the input file to the target file
while ((ch = fgetc(input_file)) != EOF) {
fputc(ch, target_file);
}
// close the files
fclose(input_file);
fclose(target_file);
printf("File copied successfully.\n");
return 0;
}
Dept. of CS&E, MITE, Moodbidri Page 42
Principles of Programming using C BPOPS103/203
Output:-
RUN 1 :-
mite@mite-Veriton-Series:~/Desktop/ POP $ cc prog12.c
mite@mite-Veriton-Series:~/Desktop/ POP$ ./a.out
Enter the input file name:
a.txt
Enter the target file name:
b.txt
File copied successfully
Dept. of CS&E, MITE, Moodbidri Page 43
Principles of Programming using C BPOPS103/203
Dept. of CS&E, MITE, Moodbidri Page 44