0% found this document useful (0 votes)
10 views21 pages

POP Lab Programs

Pop

Uploaded by

blackistheshade
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)
10 views21 pages

POP Lab Programs

Pop

Uploaded by

blackistheshade
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
You are on page 1/ 21

Program-1

#include<stdio.h>

void main()

int a,b ;

char op;

printf("\nEnter an expression \n");

scanf("%d%c%d",&a,&op,&b);

switch(op)

case '+':

printf("\nans=%d\n",a+b);

break;

case '-':

printf("\nans=%d\n",a-b);

break;

case '*':

printf("\nans=%d\n",a*b);

break;

case '/':

if(b!=0)

{printf("\nans=%d\n",a/b);

break;

else
printf("\nDivision by zero error ....\n ");

break;

default:

printf("\nInvalid operator ...\n ");

Program-2

//Develop a program to compute the roots of a quadratic equation by

//accepting the coefficients. Print appropriate messages.

#include<stdio.h>

#include<math.h>

void main()

float a,b,c,d,r1,r2;

printf("Enter three coefficients a,b and c of quadratic equation\n");

scanf("%f%f%f",&a,&b,&c);

if(a!=0)

printf("\n Given co efficients form quadratic equation");

d=(b*b)-(4*a*c);

if(d>0)

printf("\n Roots are real and distinct");

r1=(-b+sqrt(d))/(2*a);
r2=(-b-sqrt(d))/(2*a);

printf("\n Root1=%f \t Root2=%f\n",r1,r2);

else if(d==0)

printf("\n Roots are real and equal");

r1=-b/(2*a);

r2=r1;

printf("\n Root1=%f \t Root2=%f\n",r1,r2);

else

printf("\n Roots are imaginary\n");

r1=-b/(2*a);

r2=sqrt(fabs(d))/(2*a);

printf("\n Root1=%f +i %f \t Root2=%f -i %f\n",r1,r2,r1,r2);

else

printf("\n Given coefficients do not form quadratic equation\n");

Program-3

#include<stdio.h>

void main()
{

int units;

float amt,totalamt,surcharge;

char name[25];

printf("Enter the name of user: ");

scanf("%s",name);

printf("\n Enter the total units consumed: ");

scanf("%d",&units);

if(units<=200)

amt=units*0.80;

else if(units<=300)

amt=160+((units-200)*0.90);

else

amt=250+((units-300)*1.00);

totalamt=amt+100;

if(totalamt>400)

surcharge=totalamt*0.15;

totalamt=totalamt+surcharge;
printf("\n Total amount after adding surcharge");

printf("\n User:%s \n Electricity Bill:Rs.%.2f\n",name,totalamt);

else

printf("\n User: %s \n Electricity Bill:Rs.%.2f\n",name,totalamt);

Program-4

//Program to display number pyramid by taking number of rows as its input

#include<stdio.h>

void main()

int i,j,n,m=0;

printf("Enter number of rows: ");

scanf("%d",&n);

m=40;

for(i=0;i<n+1;i++)

for(int k=m;k>=1;k--)

printf(" ");

for(j=1;j<=i;j++)
{

printf(" %d ",j);

for(j=i-1;j>=1;j--)

printf(" %d ",j);

m=m-1;

printf("\n");

Program-5

//Binary Search Program using single dimension array

#include<stdio.h>

#include<stdlib.h>

int main()

int a[10],i,n,low,high,mid,key;

printf("Enter the value of n: ");

scanf("%d",&n);

printf("\nEnter %d values in ascending order\n",n);

for(i=0;i<n;i++)

{
scanf("%d",&a[i]);

printf("Enter the number to be searched: ");

scanf("%d",&key);

printf("Array Elements are");

for(i=0;i<n;i++)

printf("\n%d",a[i]);

low=0;

high=n-1;

while (low<=high)

mid=(low+high)/2;

if(a[mid]==key)

printf("\nSearch is Successful, Number found ..!\n");

exit(0);

else if (key<a[mid])

high=mid-1;

else if (key>a[mid])

low=mid+1;

printf("\n Search unsuccessful, Number not found.... \n ");


return(0);

Program-6

#include<stdio.h>

void main()

int a[10][10],b[10][10],c[10][10];

int m,n,p,q,i,j,k;

printf("Enter the order of matrix A: ");

scanf("%d%d",&m,&n);

printf("Enter the order of matrix B: ");

scanf("%d%d",&p,&q);

if (n!=p)

printf("\n Matrix multiplication is not possible!");

else

printf("\n Matrix multiplication is possible!");

printf("\n 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("\n The elements of matrix A \n");


for(i=0;i<m;i++)

printf("\n");

for(j=0;j<n;j++)

printf("\t%d",a[i][j]);

printf("\n Enter the elements of matrix B\n");

for(i=0;i<p;i++)

for(j=0;j<q;j++)

scanf("%d",&b[i][j]);

printf("\n The elements of matrix B \n");

for(i=0;i<p;i++)

printf("\n");

for(j=0;j<q;j++)

printf("\t%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("\n The elements of matrix C \n");

for(i=0;i<m;i++)

printf("\n");

for(j=0;j<q;j++)

printf("\t%d",c[i][j]);

printf("\n");

Program-7

/Program to compute Sin(x) using Taylor series

#include<stdio.h>

#include<math.h>
void main()

float x,sum=0;

int n,i,j,fact;

printf("\n Enter the value of n and x: ");

scanf("%d%f",&n,&x);

for(i=0;i<n;i++)

fact=1;

j=2*i+1;

while(j>0)

fact=fact*j;

j=j-1;

sum=sum+(float)( pow(x,2*i+1)*pow(-1,i)) / (float)fact;

printf("\n Result=%f",sum);

printf("\n Using built in function sin(%f)=%f",x,sin(x));

printf("\n Comparision (difference) betwn machine result and built in function=%f\n",sum-sin(x));

Program-8

//Sorting the numbers using bubble sort

#include<stdio.h>

void main()
{

int a[25];

int n,i,j,temp;

printf("\n Enter the value of n ");

scanf("%d",&n);

printf("\n Enter %d integer values\n",n);

for(i=0;i<n;i++)

scanf("%d",&a[i]);

for(i=0;i<n;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("\n Sorted Integer Values \n");

for(i=0;i<n;i++)

{
printf("\n %d\n",a[i]);

Program-9

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

int string_length(char str1[])

int c=0,i;

while(*(str1+c)!='\0')

c++;

return c;

int string_compare(char str1[],char str2[])

int i=0;

while(str1[i]!='\0' || str2[i]!='\0')

if(str1[i]!=str2[i])

return(str1[i]-str2[i]);
}

i++;

return(str1[i]-str2[i]);

void string_cat(char *s1,char *s2)

int i,j;

for(i=0;s1[i]!='\0';i++);

for(j=0;s2[j]!='\0';i++,j++)

s1[i]=s2[j];

s1[i]='\0';

return;

void main()

char string1[20],string2[20];

int res,choice;

while(1)

{
printf("\nMenu of choices \n 1.Find length \n 2.Compare \n 3.Concatenate\n 4.Exit");

printf("\nEnter your choice: ");

scanf("%d",&choice);

switch(choice)

case 1:

printf("\nEnter the string: ");

scanf("%s",string1);

printf("\nLength of the string is: %d",string_length(string1));

break;

case 2: {

printf("\nEnter the strings to be compared: ");

printf("\nEnter string1\n");

scanf("%s",string1);

printf("\nEnter string2\n");

scanf("%s",string2);

res=string_compare(string1,string2);

if(res==0)

printf("\nGiven strings are equal…\n");

else

printf("\nGiven strings are not equal…\n");

break;

}
case 3: {

printf("\nEnter string1: ");

scanf("%s",string1);

printf("\nEnter string2: ");

scanf("%s",string2);

string_cat(string1,string2);

printf("\nConcatenated string is: %s",string1);

break;

case 4: {exit(0);}

default:{

printf("\nWrong choice …\n");

break;}

Program-10

//Implement structures to read, write and compute average marks and the students scoring above
and below the average marks for a class of N students.

#include<stdio.h>

struct student

int rollno,m1,m2,m3;
char name[25];

float sum,percent;

};

void main()

struct student s[20];

int i,n;

float avg;

printf("\n Enter the number of students: ");

scanf("%d",&n);

for(i=0;i<n;i++)

printf("\n Enter the roll no \n");

scanf("%d",&s[i].rollno);

printf("\n Enter the name \n");

scanf("%s",s[i].name);

printf("\n Enter m1 marks out of 100 \n");

scanf("%d",&s[i].m1);

printf("\n Enter m2 marks out of 100 \n");

scanf("%d",&s[i].m2);

printf("\n Enter m3 marks out of 100 \n");

scanf("%d",&s[i].m3);
s[i].sum=s[i].m1+s[i].m2+s[i].m3;

s[i].percent=(s[i].sum/300.00)*100.00;

printf("\n Student Details are as follows");

printf("\n Roll No \t Name \t Total Marks \t Percentage \n");

for(i=0;i<n;i++)

printf(" %d \t\t %s \t %.2f \t %.2f \n",s[i].rollno,s[i].name,s[i].sum,s[i].percent);

avg=0.0;

for(i=0;i<n;i++)

avg=avg+s[i].percent;

avg=avg/n;

printf("\n Class Average=%.2f",avg);

for(i=0;i<n;i++)

if (s[i].percent<avg)

printf("\n Student Below Average Marks=%s",s[i].name);

else if (s[i].percent>avg)

printf("\n Student Above Average Marks=%s",s[i].name);

}
}

Program-11

#include<stdio.h>

#include<stdlib.h>

#include<math.h>

void main()

int n,i;

float *fp;

float sum,sumsqr,mean,deviation,stddeviation,variance;

printf("\n Enter the value of n ");

scanf("%d",&n);

fp=(float *)malloc(n*sizeof(float));

printf("\n Enter %d elements\n",n);

for(i=0;i<n;i++)

scanf("%f",(fp+i));

for(i=0;i<n;i++)

sum=sum+ *(fp+i);

mean=sum/n;

for(i=0;i<n;i++)
{

deviation=*(fp+i)-mean;

sumsqr=sumsqr+(deviation * deviation);

variance=sumsqr/n;

stddeviation=sqrt(variance);

printf("\n Sum is %.4f",sum);

printf("\n Mean is %.4f",mean);

printf("\n Variance is %.4f",variance);

printf("\n Standard deviation is %.4f\n",stddeviation);

free(fp);

Program-12

#include<stdio.h>

#include<stdlib.h>

void main()

FILE *fp1,*fp2;

char file_name[50],c;

printf("\nEnter the name of the file to open for reading: ");

scanf("%s",file_name);

fp1=fopen(file_name,"r");

if(fp1==NULL)

{
printf("\nCannot open the file: %s",file_name);

exit(0);

printf("\nEnter the name of the file to open for writing: ");

scanf("%s",file_name);

fp2=fopen(file_name,"w");

c=fgetc(fp1);

while(!feof(fp1))

fputc(c,fp2);

c=fgetc(fp1);

printf("\nContents copied to: %s\n",file_name);

fclose(fp1);

fclose(fp2);

You might also like