PDF Go Lab Manua Pop
PDF Go Lab Manua Pop
Student Name:
USN:
Section: Batch:
PREFACE
Institute Mission
Department Vision
To become a Center of Excellence in the computer science and information technology discipline with a
strong research and teaching environment that produces highly qualified and motivated graduates.
Department Mission
PO2 Problem analysis: Identify, formulate, review research literature, and analyze complex
engineering problems reaching substantiated conclusions using first principles of mathematics,
natural sciences, and engineering sciences
PO3 Design/development of solutions: Design solutions for complex engineering problems and
design system components or processes that meet the specified needs with appropriate
consideration for the public health and safety, and the cultural, societal, and environmental
considerations
PO4 Conduct investigations of complex problems: Use research-based knowledge and research
methods including design of experiments, analysis and interpretation of data, and synthesis of
the information to provide valid conclusions.
PO5 Modern tool usage: Create, select, and apply appropriate techniques, resources, and modern
engineering and IT tools including prediction and modeling to complex engineering activities
with an understanding of the limitations.
PO6 The engineer and society: Apply reasoning informed by the contextual knowledge to assess
societal, health, safety, legal and cultural issues and the consequent responsibilities relevant to
the professional engineering practice
PO7 Environment and sustainability: Understand the impact of the professional engineering
solutions in societal and environmental contexts, and demonstrate the knowledge of, and need
for sustainable development
PO8 Ethics: Apply ethical principles and commit to professional ethics and responsibilities and
norms of the engineering practice
PO9 Individual and team work: Function effectively as an individual, and as a member or leader in
diverse teams, and in multidisciplinary settings.
PO10 Communication: Communicate effectively on complex engineering activities with the
engineering community and with society at large, such as, being able to comprehend and write
effective reports and design documentation, make effective presentations, and give and receive
clear instructions.
PO11 Project management and finance: Demonstrate knowledge and understanding of the
engineering and management principles and apply these to one’s own work, as a member and
leader in a team, to manage projects and in multidisciplinary environments.
PO12 Life-long learning: Recognize the need for, and have the preparation and ability to engage in
independent and life-long learning in the broadest context of technological change.
Demonstrate understanding of the principles and working of the hardware and software aspects
of computer systems.
Use professional engineering practices, strategies and tactics for the development, operation and
maintenance of software.
Principles of Programming using C
Course Code: BPOPS103
Programming Experiments
1. Simulation of a Simple Calculator.
2. 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: 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.
4. Write a C Program to display the following by reading the number of rows as input,
1
121
12321
1234321
---------------------------
nth row
5. Implement Binary Search on Integers.
6. Implement Matrix multiplication and validate the rules of multiplication.
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.
8. Sort the given set of N numbers using Bubble sort.
9. Write functions to implement string operations such as compare, concatenate, and find string length.
Use the parameter passing techniques.
10. 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.
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 a C program to copy a text file to another, read both the input file name and target file name.
1. Simulation of a Simple Calculator.
#include<stdio.h>
intmain()
{
int num1,num2;
float result=0;
char op;
printf("Enter first number:");
scanf("%d", &num1);
printf("Enter second number:");
scanf("%d", &num2);
printf("Choose operation to perform(+,-,*,/,%) ");
scanf(" %c", &op);
switch(op)
{
case'+':result=num1+num2;
break;
case'-':result=num1-num2;
break;
case'*':result=num1*num2;
break;
case'/':result=(float)num1/(float)num2;
break;
case'%':result=num1%num2;
break;
default:printf("Invalid operation\n");
}
printf("result:%f\n",result);
return 0;
}
Output:
2. Compute the roots of a quadratic equation by accepting the coefficients. Print
appropriate messages.
#include<stdio.h>
#include<math.h>
intmain()
{
float a,b,c,d,rpart,ipart,root1,root2;
printf("Enter three co-efficients\n");
scanf("%f %f %f", &a, &b, &c);
if(a==0 && b==0)
{
printf("Invalid inputs");
}
else if(a==0)
{
printf("Linear equation\n");
root1=-c/b;
printf("Root=%f\n",root1);
}
else
{
d=(b*b)-(4*a*c);
if(d==0)
{
printf("The roots real and equal\n");
root1=root2=-b/(2*a);
printf("the roots are root1=%f and root2=%f", root1, root2);
}
else if(d>0)
{
printf("The roots are real and distinct\n");
root1=(-b+sqrt(d))/(2*a);
root2=(-b-sqrt(d))/(2*a);
printf("The roots are root1=%f and root2=%f", root1, root2);
}
else
{
printf("The roots are imaginary\n");
rpart=-b/(2*a);
ipart=sqrt(fabs(d)/(2*a));
printf("The root1=%f+i%f\n", rpart,ipart);
printf("The root2=%f-i%f\n", rpart,ipart);
}
}
return 0;
}
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>
intmain()
{
char name[10];
float units,charge;
printf("Enter name and unit consumed\n");
scanf("%s %f", &name, &units);
if(units<=200)
c=(units*0.8)+100;
else if(units>200&& units <=300)
charge=(units-200)*0.9+160+100;
else if(units>300)
charge=(units-300)*1+90+160+100;
if(charge>=400)
charge=charge+charge*0.15;
printf("name=%s\n",name);
printf("charge=%f\n",charge);
return 0;
}
Output:
4. Write a C Program to display the following by reading the number of rows as input,
1 21
12321
1 234 3 2 1
---------------------------
nth row.
#include <stdio.h>
int main()
scanf("%d", &iNum);
iSp_cnt = iNum - 1;
iNum_cnt = 1;
for(i=0;i<iNum;i++)
iVal = 1;
for(j=0;j<iSp_cnt;j++)
printf(" ");
for(k=0;k<iNum_cnt;k++)
{
if(k <= iNum_cnt/2)
iVal++;
else
iVal--;
printf("\n");
iSp_cnt--;
iNum_cnt += 2;
return 0;
Output:
5. Implement Binary Search on Integers.
#include<stdio.h>
intmain()
{
int i,n,a[10],low,high,mid,key,found=0;
printf("enter size of array\n");
scanf("%d", &n);
printf("enter elements of array in ascending order\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter element to search");
scanf("%d",&key);
low=0;
high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if(key==a[mid])
{
found=1;
break;
}
else if(key>a[mid])
{
low=mid+1;
}
else
{
high=mid-1;
}
}
if(found==1)
printf("item found in position:%d",mid+1);
else
printf("item not found");
}
Output:
6. Implement Matrix multiplication and validate the rules of multiplication.
#include<stdio.h>
intmain()
{
int m,n,p,q,i,j,k,a[10][10],b[10][10],c[10][10];
printf("Enter the size of matrix A\n");
scanf("%d %d", &m, &n);
printf("Enter the size of matrix B\n");
scanf("%d %d", &p, &q);
if(m!=q)
printf("Matrix multiplication is 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("Enter 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];
}
printf("A matrix is\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}printf("\n");
}
printf("B matrix is\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",b[i][j]);
}printf("\n");
}
printf("The resultant matrix C is\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",c[i][j]);
}printf("\n");
}
}
return 0;
}
Output:
7. Computesin(x)/cos(x)usingTaylorseriesapproximation.Compareyour result
withthebuiltinlibraryfunction.Printboththeresultswithappropriateinferences.
A. For cosx:
#include<stdio.h>
#include<math.h>
intmain()
{
int n,i;
float degree,x,num,den,value,term;
printf("Enter number of terms\n");
scanf("%d", &n);
printf("Enter the angle\n");
scanf("%f", °ree);
x=degree*3.1416/180;
num=1;
den=1;
term=num/den;
value=0;
for(i=1;i<=n;i++)
{
value=value+term;
num=-num*x*x;
den=den*(2*i)*(2*i-1);
term=num/den;
}
printf("Calculated value of cos(%f) is %f\n",degree,value);
printf("value of cos(%f) using built in function is %f",degree,cos(x));
return 0;
}
Output:
B. For sinx:
#include<stdio.h>
#include<math.h>
intmain()
{
int n,i;
float degree,x,num,den,value,term;
printf("Enter number of terms\n");
scanf("%d", &n);
printf("Enter the angle\n");
scanf("%f", °ree);
x=degree*3.1416/180;
num=x;
den=1;
term=num/den;
value=0;
for(i=1;i<=n;i++)
{
value=value+term;
num=-num*x*x;
den=den*(2*i)*(2*i+1);
term=num/den;
}
printf("Calculated value of sin(%f) is %f\n",degree,value);
printf("value of sin(%f) using built in function is %f",degree,sin(x));
return 0;
}
Output:
8. SortthegivensetofNnumbers usingBubblesort.
#include<stdio.h>
intmain()
{
int a[10],n,i,j,temp;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter the elements of array\n");
for(i=0;i<n;i++)
scanf("%d", &a[i]);
printf("the input array is\n");
for(i=0;i<n;i++)
{
printf("%d\t", a[i]);
}
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("\nThe sorted array is\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
return 0;
}
Output:
9.Writefunctions
toimplementstringoperationssuchascompare,concatenate,stringlength.Convincethe
parameterpassingtechniques.
#include<stdio.h>
int compare_string(char [], char []);
void concatenate(char [], char []);
int string_length(char []);
int main()
{
char s1[1000], s2[1000];
int length1,length2;
printf("Input a string1\n");
gets(s1);
printf("Input a string2\n");
gets(s2);
length1=string_length(s1);
length2=string_length(s2);
printf("Length of s1=%d\n",length1);
printf("Length of s2=%d\n",length2);
if(compare_string(s1,s2)==0)
printf("Equal strings\n");
else
printf("Unequal strings\n");
concatenate(s1, s2);
printf("string obtained on concatenation:\"%s\\n",s1);
return 0;
}
int string_length(char s1[])
{
int c=0;
while (s1[c]!='\0')
c++;
return c;
}
int compare_string(char s1[], char s2[])
{
int c=0;
while(s1[c]==s2[c])
{
if(s1[c]=='\0'|| s2[c]=='\0')
break;
c++;
}
if(s1[c]=='\0' && s2[c]=='\0')
return 0;
else
return 1;
}
void concatenate(char s1[], char s2[])
{
int c,d;
c=0;
while(s1[c]!='\0')
{
c++;
}
d=0;
while(s2[d]!='\0')
{
s1[c]=s2[d];
d++;
c++;
}
s1[c]='\0';
}
Output:
10.
Implementstructuretoread,writeandcomputeaveragemarksandthestudentsscoringaboveand
belowtheaveragemarksforaclassofNstudents.
#include<stdio.h>
struct student
{
char usn[50];
char name[50];
int marks;
}
s[10];
void main()
{
int i,n,countav=0,countbv=0;
float sum=0,avg;
printf("Enter number of students\n");
scanf("%d",&n);
printf("Enter students information\n");
for(i=0;i<n;i++)
{
printf("Enter USN\n");
scanf("%s",&s[i].usn);
printf("Enter name\n");
scanf("%s",&s[i].name);
printf("Enter marks\n");
scanf("%d",&s[i].marks);
printf("\n");
}
for(i=0;i<n;i++)
{
printf("USN:%s\n",s[i].usn);
printf("Name:%s\n",s[i].name);
printf("Marks:%d\n",s[i].marks);
}
for(i=0;i<n;i++)
{
sum=sum+s[i].marks;
}
printf("sum:%f\n",sum);
avg=sum/n;
printf("\nAverage marks:%f",avg);
for(i=0;i<n;i++)
{
if(s[i].marks>=avg)
countav++;
else
countbv++;
}
printf("\nTotal number of students above average:%d",countav);
printf("\nTotal number of students below average:%d",countbv);
return 0;
}
Output:
11.
Developaprogramusingpointerstocomputethesum,meanandstandarddeviationofallelement
sstoredinanarrayofNrealnumbers.
#include<stdio.h>
#include<math.h>
intmain()
{
float a[10],*ptr,mean,std,sum=0,sumstd=0;
int i,n;
printf("Enter the number of elements\n");
scanf("%d", &n);
printf("Enter array elements\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++)
{
sumstd=sumstd+pow((*ptr-mean),2);
ptr++;
}
std=sqrt(sumstd/n);
printf("sum=%f\t Mean=%f\t standard deviation=%f",sum,mean,std);
return 0;
}
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<stdlib.h>
int main(void)
{
FILE *fp1,*fp2;
int ch;
char fname1[100], fname2[100];
printf("\nEnter File name to be copied\n");
scanf("%s",fname1);
fp1 = fopen(fname1,"r");
if(fp1 == NULL)
{
printf("\nInput File %s doesn't exist\n", fname1);
exit(0);
}
printf("\nEnter target File name\n");
scanf("%s",fname2);
fp2 = fopen(fname2,"w");
while((ch=fgetc(fp1)) != EOF)
{
fputc(ch,fp2);
}
printf("\nFile %s successfully created\n",fname2);
fclose(fp1);
fclose(fp2);
return 0;
}
Output: