[go: up one dir, main page]

0% found this document useful (0 votes)
199 views43 pages

POP Lab Manual For Enginnering 1st Semester

Uploaded by

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

POP Lab Manual For Enginnering 1st Semester

Uploaded by

iamalamblamb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

Ex.

No:1
Simple Calculator
Aim:
Develop Simulation of Simple Calculator using Arithmetic Operators.
Algorithm:
• Begin.

• Print Enter Your Choice.

• Enter Your Choice.

• Enter Two Operands For Operation.

• User Will Enter +,-,*,/ .

• Switch(Operator)

• Do The Operation.

• Print The Result.

• Exit.

Flowchart:
Code:
#include <stdio.h>
void main()
{
char operator;
float num1, num2, result;

printf("Simulation of a Simple Calculator\n");


printf("*********************************\n");
printf("Enter two numbers \n");
scanf("%f %f", &num1, &num2);
fflush(stdin);
printf("Enter the operator [+,-,*,/] \n");
scanf("%s", &operator);
switch(operator)
{
case '+': result = num1 + num2; // add two numbers
break;
case '-': result = num1 - num2; // subtract two numbers
break;
case '*': result = num1 * num2; // multiply two numbers
break;
case '/': result = num1 / num2; // divide two numbers
break;
default :printf("Error in operation");
break;
}
printf("\n %5.2f %c %5.2f = %5.2f\n", num1, operator, num2, result);
}

Output:

Result:
Thus, the Implementation Simple Calculator was done and Executed Successfully
Ex. No:2
Quadratic Equation
Aim:
Develop a C program to implement Quadratic Equation.

Algorithm:
1. Start
2. Read the coefficients of the equation, a, b and c from the user. 3.Calculate discriminant = (b * b) – (4 * a * c) 4.If discriminant > 0:
4.1: Calculate root1 = ( -b + sqrt(discriminant)) / (2 * a)
4.2: Calculate root2 = ( -b - sqrt(discriminant)) / (2 * a)
4.3: Display “Roots are real and different”
4.4: Display root1 and root2
5. Else if discriminant = 0:
5.1: Calculate root1 = -b / (2 *a)
5.2: root2 = root1
5.3: Display “Root are real and equal”
5.4: Display root1 and root2
6. Else:
6.1: Calculate real = -b / (2 * a)
6.2: Calculate imaginary = sqrt(-discriminant) / (2 * a)
6.3: Display “Roots are imaginary”
6.4: Display real, “±”, imaginary, “i”
7. Stop
Flowchart:

Code:
#include <stdio.h>
#include <stdlib.h> #include <math.h> void main()
{ float a, b, c, root1, root2;
float realp, imagp, disc;
printf("Enter the values of a, b and c \n");
scanf("%f %f %f", &a, &b, &c);
/* If a = 0, it is not a quadratic equation */ if (a == 0 || b == 0 || c == 0)
{
printf("Error: Roots cannot be determined \n"); exit(1);
}
else
{
disc = b * b - 4.0 * a * c;
if (disc < 0)
{
printf("Imaginary Roots\n");
realp = -b / (2.0 * a) ;
imagp = sqrt(abs(disc)) / (2.0 * a); printf("Root1 = %f +i %f\n", realp, imagp);
printf("Root2 = %f -i %f\n", realp, imagp);
}
else if (disc == 0)
{
printf("Roots are real and equal\n");
root1 = -b / (2.0 * a);
root2 = root1;
printf("Root1 = %f\n", root1); printf("Root2 = %f\n", root2);
}
else if (disc >0 )
{
printf("Roots are real and distinct \n"); root1 =(-b + sqrt(disc)) / (2.0 * a);
root2 =(-b - sqrt(disc)) / (2.0 * a); printf("Root1 = %f \n", root1); printf("Root2 =
%f \n", root2);
}}}
Result:
Thus, the Implementation Quadratic Equation was done and executed successfully.
EX. No:3
Electricity bill

Aim:
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 the customer number
• Step-3: Read the customer’s name
• Step-4: Read the unit consumed
• Step-5: If the unit is less than 200 than charge 0.80 per unit
• Step-6: If the unit is greater than 200 and less than 300, than charge 0.90 per unit
• Step-7: If the unit is greater than 400 than charge 1.00 per unit
• Step-8: Calculate the amount by unit* charge
• Step-9: If in case the amount is greater then 400, then an additional surcharge of 15% of the total amount is charge total-amount =
amount + surcharge.
• Step-10: Addition of Rs.100 as a minimum meter charge for all customer to the total amount
• Step-11: Print the customer number, name, unit_consumed_amount, surcharge, totalAMOUNT + 100
• Step-12: Stop
Flowchart:
Code:
Code:
#include<stdio.h>
#include<string.h>
void main()
{
int cust_no, unit_con;
float charge,surcharge=0, amt, total_amt;
char nm[25];
printf("Enter the customer IDNO :\t");
scanf("%d",&cust_no);
printf("Enter the customer Name :\t");
scanf("%s",nm);
printf("Enter the unit consumed by customer :\t");
scanf("%d",&unit_con);
if (unit_con<200 )
charge = 0.80;
else if (unit_con>=200 &&unit_con<300)
charge = 0.90;
else
charge = 1.00;
amt = unit_con*charge;
if (amt>400)
surcharge = amt*15/100.0;
total_amt = amt+surcharge;
printf("\t\t\t\nElectricity Bill\n\n");
printf("Customer IDNO:\t%d",cust_no);
printf("\nCustomer Name:\t%s",nm);
printf("\nunit Consumed:\t%d",unit_con);
printf("\nAmount Charges @Rs. %4.2f per unit :\t%0.2f",charge,amt);
printf("\nSurchage Amount:\t%.2f",surcharge);
printf("\nMinimum meter charge Rs:\t%d",100);
printf("\nNet Amount Paid By the Customer:\t%.2f",total_amt+100);
}

Result:
Thus, the Implementation Electricity Board charges was done and Executed Successfully

EX. No:4
Pattern Triangle
Aim:
Write a C Program to display the following by reading the number of rows as input,
1
121
12321
1234321
--------------------------- nth row
Code:
#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");
}
}
Result:
Thus, the Implementation Pattern Triangle was done and Executed Successfully
Ex. No:5
Binary search for numbers

Aim:
To implement Binary Search Algorithm for given array of numbers using C
Algorithm:
• Step-1: Start
• Step-2: [Read the value of N]-Read N
• Step-3: [Enter the elements in the ascending order]- For i=0 to N-1 is to be read from keyboard
• Step-4: [Enter a element for searching that could be a key element]
• Step-5: Initially set low=0 and high = N
• Step-6: [Find out the middle value]-Mid = (low + high)/2;
• Step-7: Check whether the key value is lesser than the middle value than goto step 8
o Check whether the key value is greater than the middle value than goto step 9
• Step-8: high = mid -1
• Step-9: low = mid + 1
• Step-10: if key value and mid- value are same then go to step 11 if not than go to step 12
• Step-11: Successful search
• Step-12: Not searched the key element
• Step-13: Stop

Flowchart:
Code:
#include<stdio.h>
#include<string.h>
void main()
{
char name[50][50],key[50];
int n,i,low,high,mid,found=0;
printf("How many names to Read:\n");
scanf("%d",&n);
printf("Enter the Names in ascending order:\n");
for(i=0;i<n;i++)
{
scanf("%s",name[i]);
}
printf("\nType the name to be searched:");
scanf("%s",key);

low=0;
high=n-1;
while(low<=high && !found)
{
mid=(low+high)/2;
if(strcmp(name[mid],key)==0)
found=1;
else if(strcmp(name[mid],key)<0)
low=mid+1;
else
high=mid-1;
}

if(found==1)
printf("\nSearch successful and Name found in the list at position:%d",mid+1);
else
printf("Name not found and search Unsuccessful....!");
}

Result:
Thus, the Implementation Binary Search was done and Executed Successfully

Ex. No:6
Matrix Multiplication

Aim:
Develop Matrix multiplication without affecting Multiplication property using C.
Algorithm:

1. Start.
2. Enter the value of m and n (or) order of the first matrix.
3. Enter the value of p and q (or) order of the second matrix.
4. Create a matrix of size a[m][n] and b[p][q].
5. Enter the element of matrices row wise using loops.
6. If a number of columns of first matrix is not equal to the number of rows of second matrix, print matrix multiplication is not possible and exit. If not,
proceed to next step.
7. Create a third matrix, c of size m x q to store the product.
8. Set a loop from i=0 to i=m.
9. Set an inner loop for the above loop from j=0 to j=q.
10. Initialise the value of element (i, j) of new matrix to 0.
11. Set an inner loop inside the above loop from k=0 to k=p.
12. Using the add and assign operator (+=) store the value of a[i][k] * b[k][j] in the third matrix, c[i][j].
13. Print the third matrix.
14. Stop.

Flowchart:
Code:
#include<stdio.h>
int main()
{
int r1,r2,c1,c2;
printf("Enter number of rows for First Matrix:\n");
scanf("%d",&r1);
printf("Enter number of columns for First Matrix:\n");
scanf("%d",&c1);
printf("Enter number of rows for Second Matrix:\n");
scanf("%d",&r2);
printf("Enter number of columns for Second Matrix:\n");
scanf("%d",&c2);
if(c1!=r2)
{
printf("Matrices Can't be multiplied together");
}
else
{
int m1[r1][c1],m2[r2][c2];
printf("Enter first matrix elements \n");
for(int i=0;i<r1;i++)
{
for(int j=0;j<c1;j++)
{
scanf("%d",&m1[i][j]);
}
}
printf("Enter Second matrix elements\n");
for(int i=0;i<r2;i++)
{
for(int j=0;j<c2;j++)
{
scanf("%d",&m2[i][j]);
}
}
int mul[r1][c2];
for(int i=0;i<r1;i++)
{
for(int j=0;j<c2;j++)
{
mul[i][j]=0;

// Multiplying i’th row with j’th column


for(int k=0;k<c1;k++)
{
mul[i][j]+=m1[i][k]*m2[k][j];
}
}
}
printf("Multiplied matrix\n");
for(int i=0;i<r1;i++)
{
for(int j=0;j<c2;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
}
return 0;
}
Result:
Thus, the implementation of matrix multiplication was done successfully.
EX. No:7

Taylor Series sin(x)/cos(x)

Aim:
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 the value of x in degree]- Read x
Step-3: [Initialization and Radians Conversion]
Temp = x
x=x*(3.142/180.0)
Term = x
sinx = term
n=1
Step-4: [Computer sin value]
Repeat while (term>FLT_EPSILON)
Fact = 2*n*(2*n+1)
Term = term * x * x /fact Sinx
sinx +term = n + 1n
Step-5: [Output]- Print sin(x) without using library function of Print sin(x) and with using library function
Step-6: Stop
Flowchart:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
main()
{
float x, t, sum;
int d;
int i, n=20;
/* Read the Input x value in degrees */
printf ("Input X Value (in degrees) :");
scanf ("%f", &x);
d= x;
/*converting x to radians */
x=x*3.1412/180;
t=1;
sum=1;
for (i=1; i<n+1; i++)
{
t=t*pow ((double) (-1), (double) (2*i-1))*x*x/ (2*i*(2*i-1));
sum=sum+t;
}
/* Print the Results */
for (i =0; i<35; i++)
printf ("_"); printf ("\n\n");
printf ("COS (%d) =%7.3f\n\n", (int) d, sum);
for (i =0; i<35; i++)
printf ("_"); printf ("\n\n");
printf(“\nBuilt-in Sine value=%f\n”,cos(x));

return 0;
}
Ex.No:8
Bubble Sort.
Aim:
Develop a program to sort the number in Ascending order using Bubble Sort.
Algorithm:
1. Start
2. Read the array of given items from the user.

3. Take the first element (index = 0), compare the current element with the next element.

4. If the current element is greater than the next element, swap them.

5. Else,
If the current element is less than the next element, then move to the next element.

6. Repeat Step 3 to Step 5 until all elements are sorted.

7. Stop

Flowchart:
Code:
#include <stdio.h>

int main ()
{
int array [100], n, c, d, swap;

printf("Enter number of elements\n");


scanf("%d", &n);

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

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


scanf("%d", &array[c]);

for (c = 0 ; c < n - 1; c++)


{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1]) /* For decreasing order use '<' instead of '>' */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}

printf("Sorted list in ascending order:\n");

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


printf("%d\n", array[c]);

return 0;
}

Result:
Thus, the Implementation of Bubble Sort was done and Executed Successfully

Ex. No:9

String Operations
Aim:
Write functions to implement string operations such as compare, concatenate, string length. Convince
the parameter passing techniques.
Algorithm:
• Step-1: Start
• Step-2: Press 1 – Compare, 2 – Concatenate, 3-length of string, if press 1 than goto step 3, if press
step 2 then goto step 4, if press 3 then goto step 5
•Step-3: Read the string1 and string2 and cojmparsion function by using strcmp built in unction is
used. If res = 0 then print both strings are equal, otherwise print strints are not equal and than goto
the step 4
• Step-4: Read String1 and sring2 and find concatenation of two strings using string handling function
strcat() and display sring and return back to main fuctions.
• Step-5: Read string1 and call function to find the length of string by calling function length ( *string)
• Step-6: if digit=1 then gtoto step 2 else goto step 7
• Step-7: Stop
Flow Chart
Code:
#include<stdio.h>

#include<string.h>

void compare(char [ ],char [ ]);

void concat(char [ ],char [ ]);

void length(char *[ ]);

void main( )

int n,digit;

char str1[10],str2[10];

do

printf("press 1-compare 2-concatenate 3-length of string");

printf("\n enter your choice=");

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

case 1:printf("enter first string=");

scanf("%s",str1);

printf("enter second string=");

scanf("%s",str2);

compare(str1,str2);

break;

case 2: printf("enter first string=");

scanf("%s",str1);

printf("enter second string=");

scanf("%s",str2);

concat(str1,str2);

break;

case 3:printf("enter string=");

scanf("%s",str1);
length(&str1);

break;

default: printf("wrong choice");

break;

printf("\n Do you want to continue(1/0)? ");

scanf("%d", &digit);

}while(digit==1);

void compare(char str1[ ],char str2[ ])

int i;

i=strcmp(str1,str2);

if(i==0)

printf("strings are equal\n ");

else
printf("string are not equal\n");

void concat(char str1[ ],char str2[ ])

strcat(str1,str2);

printf("concatenate string=%s",str1);

void length(char *str1[ ])

int len;

len=strlen(str1);

printf("the length of string=%d",len);

Result:
Thus the string operation with various string function was written sand executed successfully.
EX.NO 10
Structures
Aim:
Using Structure to read, write and compute the total marks below and above from class of N Students
Algorithm:
Step-1: Start
Step-2: Read number of students
Step-3: For every student, read the student id, name , marks for all the subjects
Step-4: Calculate the avarage marks and store it in the avg field
Step-5: Print the results
Step-6: Initialise loop
Step-7: Read teh average of every student
Step-8: Check for if avg>35.00
Step-9: If yes than print the result else goto next interation
Step-10: Initialise the loop
Step-11: Read average of every student
Step-12: Check if avg<35.00
Step-13: If yes than print result else goto next iteration
Step-14: Stop
Flowchart:
Code:
#include<stdio.h>
#include<conio.h>

struct student{
int marks;
}st[10];
void main()
{
int i,n;
float total=0,avgmarks;
//clrscr();
printf("\nEnter the number of students in class(<=10):");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter student %d marks :",i+1);
scanf("%d",&st[i].marks);
}
for(i=0;i<n;i++)
{
total = total + st[i].marks;
}
avgmarks=total/n;
printf("\nAverage marks = %.2f",avgmarks);
for(i=0;i<n;i++)
{
if(st[i].marks>=avgmarks)
{
printf("\n student %d marks = %d above average",i+1,st[i].marks);
}
else
{
printf("\n student %d marks = %d below average",i+1,st[i].marks);
}
}
getch();
}

Result:
Thus, the student details using structure was implemented and executed successfully.
EX.NO 11

Pointers
Aim:
using pointers to compute sum, mean and standard deviation in c
Algorithm:
Step 1: [Start]

Step 2: [Input the elements]


Read n, a[i]

Step 3: [Initialize] sum=0,sumstd=0

Step 4: [Compute sum, mean, standard deviation]


Ptr=a
For i = 0 thru n in steps of 1
Sum=sum+*ptr
Ptr++
Mean=sum/n
Ptr=a
For i 0 thru n in steps of 1
Sumstd=sumstd+pow((*ptr-mean),2)
Ptr++
std=sqrt(sumstd/n)

Step 5: [Output]
Print sum, mean, standard deviation
Flowchart:

Code:
#include<stdio.h>
#include<math.h>

void main ()
{
float a[20], sum1 = 0, sum2 = 0, mean, var, dev;
int i, n;
printf ("Enter no of elements:");
scanf ("%d", &n);
printf ("Enter array elements:");
for (i = 0; i < n; i++)
{
scanf ("%f", a + i);
sum1 = sum1 + * (a + i);
}
mean = sum1 / n;
for (i = 0; i < n; i++)
{
sum2 = sum2 + pow ((*(a + i) - mean), 2);
}
var = sum2 / n;
dev = sqrt (var);
printf ("Sum :%f\n", sum1);
printf ("Mean :%f\n", mean);
printf ("Variance :%f\n", var);
printf ("Deviation :%f\n", dev);

}
Output:

Result:
Thus the computation of sum, mean and standard deviation using pointer was written and implemented successfully.
Aim:
Program which copies one file to another

Algorithm:
Step 1: Start
Step 2: read command line arguments
Step 3: check if no of arguments =3 or not. If not print invalid no of arguments
Step 4: open source file in read mode
Step 5: if NULL pointer, then print source file can not be open
Step 6: open destination file in write mode
Step 7: if NULL pointer, then print destination file can not be open
Step 8 : read a character from source file and write to destination file until EOF
Step 9: Close source file and destination file
Step 10: Stop

FlowChart:

You might also like