0% found this document useful (0 votes)
20 views31 pages

POP Manual PGM 1-12

Uploaded by

archanav5703
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)
20 views31 pages

POP Manual PGM 1-12

Uploaded by

archanav5703
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/ 31

Principles of Programming using C (22POP13)

Program 1

Simulation of a Simple Calculator.


Algorithm: To implement Commercial calculator.

Step 1: Start
Step 2: [Enter first number]
read num1
Step 3: [Enter Second number]
read num2
Step 4: [Enter Choice]
read choice
Step 5: [To perform addition]
if choice is equal to plus
add num1 and num2
print result
Step 6: [To perform subtraction]
if choice is equal to minus
subtract num2 from num1
print result
Step 7: [To perform multiplication]
if choice is equal to multiplication
multiply num1 and num2
print result
Step 8: [To perform division]
if choice is equal to division
divide num1 by num2
print result
Step 9: [To perform Modulus]
if choice is equal to modulus
divide num1 by num2
print result (remainder)
Step 10: Stop

DEPT.OF CSE,BIT 2022-2023 1


Principles of Programming using C (22POP13)

Source Code:

#include<stdio.h>
void main()
{
float sum, sub, mul, div, a, b;
int choice,mod;
printf("Enter the values of a&b\n");
scanf("%f%f", &a,&b);
printf("Enter 1 for Addition,2 for Sutraction,3 for Multiplication, 4 for
Division,5 for Mod\n");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("addition\n");
sum=a+b;
printf("%f ",sum); break;

case 2: printf("subtraction\n");
sub=a-b;
printf("%f",sub);
break;

case 3: printf("Multiplication\n");
mul=a*b;
printf("%f",mul);
break;

case 4: printf("Division\n");
if(b==0)
printf("Divide by zero error! please enter non-zero number");
else

DEPT.OF CSE,BIT 2022-2023 2


Principles of Programming using C (22POP13)
{
div=a/b;
printf("%f",div);
}
break;

case 5: printf("Modulus\n");
if(a<b)
mod=a;
else
mod=(int)a%(int)b;
printf("%d",mod);
break;
default: printf("Wrong choice, enter correct choice");
}
}

DEPT.OF CSE,BIT 2022-2023 3


Principles of Programming using C (22POP13)

Program 2

Compute the roots of a quadratic equation by accepting the coefficients.


Print appropriate messages.

Algorithm: To compute all possible roots of the quadratic equation


Step 1: Start
Step 2: [Input the values of a, b, c] read a, b, c

Step 3: [Calculate the determinant]

determinant = b*b-4*a*c

Step 4: [Check for validity]

If a is equal to 0 and b is equal to 0print “Invalid Inputs”

Step 5: [Check for different roots]

If a is equal to 0

print “Linear equation”Root1=-c/b

Print “Root1”

Step 6: [Check for real and equal roots]

If determinant is equal to 0 print “Roots are real and equal”Root1= -b/(2*a)

Root2 = -b/(2*a)

Print “Root1 & Root2”

Step 7: [Check for real and distinct roots]

If determinant is greater than 0


Then print “Roots are real and distinct”
Root1= (-b+ (sqrt (fabs (determinant))))/(2*a)

Root2= (-b-(sqrt (fabs (determinant))))/(2*a)


Print “root1 and root2”
End if
Step 8: [Check for imaginary roots]

print “Roots are imaginary”Real=-b/ (2*a)

Imaginary=sqrt (fabs (determinant))/ (2*a)print “Root1 and R oot2”

Step 9: Stop

DEPT.OF CSE,BIT 2022-2023 4


Principles of Programming using C (22POP13)

Source Code
#include<stdio.h>
#include<math.h>
void main()
{
float a, b, c, disc, re, img, x1, x2;
printf("Enter thevalues for a,b,c\n");
scanf("%f%f%f", &a, &b, &c);
if((a==0)&&(b==0))
{
printf(“Roots cannot be determined\n”);
}
else if(a==0)
{
printf(“Linear Equation\n”);
x1=-c/b;
printf(“Root1=%.3f\n”,x1);
}
else
{
disc=b*b-(4*a*c);
if(disc==0)
{
x1=x2=-b/(2*a);
printf("Roots are equal\n");
printf("x1=%f\n x2=%f",x1,x2);
}
else if(disc>0)
{
x1=(-b+sqrt(disc))/(2*a);
x2=(-b-sqrt(disc))/(2*a);
printf("Roots are real and distinct\n");

DEPT.OF CSE,BIT 2022-2023 5


Principles of Programming using C (22POP13)
printf("x1=%f,x2=%f",x1,x2);
}
else
{
re=-b/(2*a);
img = sqrt(fabs(disc))/(2*a);
printf("Roots are imaginary\n");
printf("x1 =%f+%fi\n",re,img);
printf("x2=%f-%fi\n",re,img);
}
}
}

DEPT.OF CSE,BIT 2022-2023 6


Principles of Programming using C (22POP13)

Program 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 the users are charged a minimum of Rs. 100 as meter charge. If the total
amount is more than Rs.400, then an additional sub charge 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: To generate the electricity bill

Step 1: Start
Step 2: [Input the name of the user and number of units consumed]
read name, units
Step 3: [The minimum charge of Rs. 100 is initialized]
Charge←100
Step 4: [Based on units consumed, calculate the charges]
if units<=200 then
charge←charge+units*0.80
else if units<=300 then
charge←charge+200*0.8+((units-200)*0.9)
else
charge←charge+200*0.8+100*0.9+((units-300)*1)
if charge>400 then
charge←charge+charge*0.15
end if
end if
Start 5: [Display the charge]
print charge
Start 6: Stop

DEPT.OF CSE,BIT 2022-2023 7


Principles of Programming using C (22POP13)

Source code
#include<stdio.h>
void main()
{
char name[20];
float units, charge=100;
printf("Enter the name of the user and number of units consumed\n");
scanf("%s%f", name, &units);
if(units<=200)
charge+=units*0.80;
else if(units<=300)
charge+=200*0.8+((units-200)*0.9);
else
{
charge+=200*0.8+100*0.9+((units-300)*1);

if(charge>400)
charge+=charge*0.15;
}
printf(“Charge=%f\n”,charge);
}

DEPT.OF CSE,BIT 2022-2023 8


Principles of Programming using C (22POP13)

Program 4:
Write a C program to display the following by reading the numbers of rows as input,
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
-----------------------------------------
nth row
Algorithm: To display the pattern for n number of rows

Step 1: Start
Step 2: [Input the number of rows to display]
Read n
Step 3: for i  0 to n
for j  1 to n-1
print “ “
for j  1 to i
print ”j”
for j  i-1 to 1
print ”j”
Start 4: Stop

DEPT.OF CSE,BIT 2022-2023 9


Principles of Programming using C (22POP13)

Source 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");
}
}

DEPT.OF CSE,BIT 2022-2023 10


Principles of Programming using C (22POP13)

Program 5:
Implement Binary Search on Integers / Names.
Algorithm: To search a key element using Binary search
Step 1: Start
Step 2: [Input the number of elements]
read n
Step 3: [ Input the array items]
for i  0 to n-1
read a[i]
end for
Step 4: [Input the key]
read key
Step 5: [Initialize low to zero and high to last element]
Low  0
Highn-1
Step 6: [Calculate mid element and compare with key]
while(low<=high && !found)
mid (low+high)/2
if a[mid] key then
goto step 7
else if a[mid]>key then
highmid-1
else
lowmid+1
end if
end while
Step 7: [Print a message for successful or not successful]
if found ==1 then
print “search successful, print in mid+1 location”
else
print “search unsuccessful key not present in the list”
endif
Step 8: Stop

DEPT.OF CSE,BIT 2022-2023 11


Principles of Programming using C (22POP13)
Source Code:

#include<stdio.h>
#include<string.h>
void main()
{
int a[10], key, n, i, low, high, mid, found=0;
printf(“Enter the number of elements to read, n=”);
scanf(“%d”,&n);
printf(“Enter the elements in ascending order\n”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
printf(“Enter the key to search\n”);
scanf(“%d”,&key);
low=0;
high=n-1;
while(low<=high&&!found)
{
mid=(low+high)/2;
if(a[mid]==key)
found=1;
else if(a[mid]<key)
low=mid+1;
else
high=mid-1;
}
if(found==1)
printf(“Key found in position:%d”,mid+1);
else
printf(“Key not found”);
}

DEPT.OF CSE,BIT 2022-2023 12


Principles of Programming using C (22POP13)

Program 6
Implement Matrix multiplication and validate the rules of multiplication.
Algorithm: To compute product of two matrices

Step 1: Start
Step 2: [input order of matrix A]
read m, n
Step 3: [input order of matrix B]
read p,q
Step 4: [check for compatibility]
if(n!=p)
print “multiplication not possible”
go to step 11
end if
Step 5: [input elements of matrix A]
for i0 to m-1
for j0 to n-1
read a[i][j]
end for
end for
Step 6: [input elements of matrix B]
for i0 to p-1
for j0 to q-1
read b[i][j]
end for
end for
Step 7: [find product of 2 matrices]
for i0 to m-1
for j0 to q-1
c[i][j]0;
for k0 to n-1
c[i][j]c[i][j]+a[i][k]*b[k][j]

DEPT.OF CSE,BIT 2022-2023 13


Principles of Programming using C (22POP13)
end for
end for
end for
Step 8: [print matrix A]
for i0 to m-1
for j0 to n-1
print a[i][j]
end for
end for
Step 9: [print matrix B]
for i0 to p-1
for j0 to q-1
print b[i][j]
end for
end for
Step 10: [print resultant matrix C]
for i0 to m-1
for j0 to q-1
print c[i][j]
end for
end for
Step 11: Stop

DEPT.OF CSE,BIT 2022-2023 14


Principles of Programming using C (22POP13)
Source code:

#include<stdio.h>
void main()
{
int a[10][10],b[10][10],c[10][10],i,j,k,m,n,p,q;
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 values for matrix A\n”);


for(i=0;i<m;i++)
{
for(j=0;j<n;i++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the values for 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];
}
}
}

DEPT.OF CSE,BIT 2022-2023 15


Principles of Programming using C (22POP13)
printf(“the elements of matrix A\n”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("The elements of matrix B\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
printf("The resultant matrix C\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
}

DEPT.OF CSE,BIT 2022-2023 16


Principles of Programming using C (22POP13)

Program 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.
Algorithm: To compute sin(x) using Taylor series

Step 1: Start
Step 2: [Input value of degree]
Read degree
Step 3: [Convert degree to radians]
X=(degree *PI)/180
Step 4: [Initialise]
term =x
sum=term
Step 5: [Calcuate each term and add]
term=-term*x*x/((i-1)*i);
sum=sum+term;
Step 6: [Print the output]
Print the sine value for the given degree
Step 7: Stop

DEPT.OF CSE,BIT 2022-2023 17


Principles of Programming using C (22POP13)
Source code:
#include<stdio.h>
#include<math.h>
void main()
{
int degree,i=2;
double x,nume,denom,sum=0,term;
printf("Enter the value of degree\n");
scanf("%d",&degree);
x=(degree*3.142)/180;
nume=x; denom=1;do
{
term=nume/denom;
nume=- nume*x*x;
denom=denom*i*(i+1);
sum=sum+term;
i=i+2;
}
while(fabs(term)>0.00001);
printf("without using library functions in(%d)=%lf",degree,sum);
printf("\nUsing the library functions in(%d)=%lf",degree,sin(x));
}

DEPT.OF CSE,BIT 2022-2023 18


Principles of Programming using C (22POP13)
Program 8
Sort the given set of N numbers using Bubble sort.
Algorithm: To sort N integer numbers using Bubble sort

Step 1: Start
Step 2: [Input the number of
elements]read n
Step 3: [Input unsorted elements in
array]for i0 to n-1
read a[i]
Step 4: [Output the original
elements]for i0 to n-1
print a[i]
Step 5: [Sort the elements in ascending
order] for i 0 to n-1
for j0 to n-i
if a[j] > a[j+1] then
temp a[j]
a[j]  a[j+1]
a[j+1] temp
end if
endfor
end for

Step 6: [Output the sorted elements]


for i0 to n-1
print a[i]
Step 7: Stop

DEPT.OF CSE,BIT 2022-2023 19


Principles of Programming using C (22POP13)
Source code:

#include<stdio.h>
void main()
{
int a[10],n,i,j,temp;
printf("Enter the number of elements\n");
scanf("%d",&n);
printf("Enter the values\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\t",a[i]);
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("\nThe sorted elements are\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
}

DEPT.OF CSE,BIT 2022-2023 20


Principles of Programming using C (22POP13)
Program 9
Write functions to implement string operations such as compare, concatenate,
string length. Convince the parameter passing parameters.

Algorithm: To implement the string operations

Step 1: Start

Step 2: [Store two strings in Str1 and Str2]

Str1=String1 and Str2=String2

Step 3: [To find String Length]

initialize i to 0

while(str[i] not equal to 0)

increment i by 1

Step 4: [To Compare two Strings]

initialize i to 0

while (str1[i] is equal to str2[i])

if (str1[i] is equal to null)

break;

increment i by 1

return str1[i]-str2[i];

Step 5: [To Concatenate two Strings]

initialize i to 0 and j to 0

while (str1[i] not equal to null)

increment i by 1

while (str2[j] not equal to null)

str1[i++]=str2[j++];

Step 6: [Output the results]

The length of the String

res1=Comparision of two Stringsres2= Concatenated String

Step 7: stop

DEPT.OF CSE,BIT 2022-2023 21


Principles of Programming using C (22POP13)

Source Code
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void stringcompare(char s1[ ],char s2 [])
{
int l1=0,l2=0,i,p=0;
l1=strlen(s1);
l2=strlen(s2);
if(l1!=l2)
{
p=1;
}
else
{
for(i=0;s1[i]!='\0';i++)
{
if(s1[i]!=s2[i])
p=1;
}
}
if(p==1)
printf("strings are different\n");
else
printf("Strings are same\n");
}
void stringlength(char s[])
{
int i;
for(i=0;s[i]!='\0';i++)
{
}
printf("Length of the string is %d\n",i);
}

DEPT.OF CSE,BIT 2022-2023 22


Principles of Programming using C (22POP13)
void stringconcatinate(char s1[ ],char s2[ ])
{
int i,j;
for(i=0;s1[i]!='\0';i++)
{
}
for(j=0;s2[j]!='\0';j++)
{
s1[i]=s2[j];
i++;
}
s1[i]='\0';
printf("Concatinated string is : \n");
printf("%s",s1);
}
void main()
{
char s1[50],s2[50];
int choice;
printf("Enter a string\n");
scanf("%s",&s1);
printf("Enter another string\n");
scanf("%s",&s2);
printf("Enter1 for comparing,2 forlength,3 for concatination\n");
scanf("%d",&choice);
switch(choice)
{
case 1 : printf("Sting comapared\n");
stringcompare(s1,s2);
break;
case 2 : printf("Strings length\n");
printf("For first string\n");

DEPT.OF CSE,BIT 2022-2023 23


Principles of Programming using C (22POP13)
stringlength(s1);
printf("For second string\n");
stringlength(s2);break;
case 3 : printf("String concatination\n");
stringconcatinate(s1,s2);
break;
default:printf("Invalid input\n");
}
}

DEPT.OF CSE,BIT 2022-2023 24


Principles of Programming using C (22POP13)
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.

Algorithm: To read, write and compute average- marks and the students scoring above and
below the average marks for a class of N students using structures

Step 1: Start

Step 2: [input number of students]

Read n

Step 3: [input details of students ie.name and marks]

Read name,m1,m2,m3

Step 4: [ Calculate total and average]


Fori=0 to n
s[i].total=(s[i].m1+s[i].m2+s[i].m3);
T=T+s[i].total;
AVG=T/N;
Step 5: [Find students above and below average]
for i=0 to n
Above avg[j]=i;
j++;

else
below avg[k]=i;

k++;
Step 6: Stop

DEPT.OF CSE,BIT 2022-2023 25


Principles of Programming using C (22POP13)
Source Code
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student
{
char usn[10],name[20];int marks;
};
void main()
{
struct student s[100];
int n,i;
float sum,avg;
printf("Enter the number of students\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter the student %d details\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);
sum=sum+s[i].marks;
}
avg=sum/n;
printf("Class Average is%f\n",avg);
printf("Details of students scoring above class average\n");
printf("USN\tName\tMarks\n");
for(i=1;i<=n;i++)
{
if(s[i].marks>avg)
printf("%s\t%s\t%d\n",s[i].usn,s[i].name,s[i].marks);
}

DEPT.OF CSE,BIT 2022-2023 26


Principles of Programming using C (22POP13)
printf("Details of students scoring below class average\n");
printf("USN\t Name\t Marks\n");
for(i=1;i<=n;i++)
{
if(s[i].marks<avg)
printf("%s\t%s\t%d\n",s[i].usn,s[i].name,s[i].marks);
}
}

DEPT.OF CSE,BIT 2022-2023 27


Principles of Programming using C (22POP13)
Program 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: To find sum, mean and standard deviation for ‘n’ number of elements

Step 1: Start
Step 2: [Input the no. of elements]
Read n
Step 3: [Input the ‘n’ number of elements]
for i0 to n-1
read a[i]
end for
Step 4: [Assign the array elements to pointer ‘ptr’]
ptra
Step 5: [Calculate sum]
for i0 to n-1
sumsum+ptr[i]
end for
Step 6: [Calculate mean]
meansum/n
Step 7: [Calculate sumstd]
for i0 to n-1
sumstdsumstd + pow((pow(ptr[i]-mean),2)
end for
Step 8: [Calculate standard deviation std]
stdsqrt(sumstd/n)
Step 9: [Output sum,mean and standard deviation]
print sum,mean,std
Step 10: Stop

DEPT.OF CSE,BIT 2022-2023 28


Principles of Programming using C (22POP13)
Source Code
#include<stdio.h>
#include<math.h>
void main()
{
float a[10],*ptr, mean, std,sum=0,sumstd=0;
int i,n;
printf("enter the no of elements\n");
scanf("%d",&n);
printf("enter the array elements\n");
for(i=0;i<n;i++)
{
scanf("%f",&a[i]);
}
ptr=a;
for(i=0;i<n;i++)
{
sum=sum+ptr[i];
}
mean=sum/n;
for(i=0;i<n;i++)
{
sumstd=sumstd+pow((ptr[i]-mean),2);
}
std=sqrt(sumstd/n);
printf("sum=%.3f\n",sum);
printf("mean=%.3f\n",mean);
printf("standard deviation=%.3f\n",std);
}

DEPT.OF CSE,BIT 2022-2023 29


Principles of Programming using C (22POP13)

Program 12
Write a C program to copy a text file to another, read both the file name and
target file name.
Algorithm: To copy a text from one file to another

Step 1: Start
Step 2: enter the file to open for read
Read file1
Step 3: fopen file1 for reading
Step 4: if fptr1==null
Print “cannot open the file”
Step 5: enter the file to open for writing
Read file2
Step 6: fopen file2 for writing
Step 7: if fptr2==null
Print “cannot open the file”
Step 8: while fptr1 != EOF
fputc(c to ptr2)
c = fgetc(ptr1)
Step 9: print “content copied”
Step 10: fclose fptr1;
fclose fptr2;
Step 11: Stop

DEPT.OF CSE,BIT 2022-2023 30


Principles of Programming using C (22POP13)

Source Code
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fptr1, *fptr2;
char filename[100], c;
printf("Enter the filename to open for reading \n");
scanf("%s", filename);
fptr1 = fopen(filename, "r");
if (fptr1 == NULL)
{
printf("Cannot open file %s \n", filename);
exit(0);
}
printf("Enter the filename to open for writing \n");
scanf("%s", filename);
fptr2 = fopen(filename, "w");
if (fptr2 == NULL)
{
printf("Cannot open file %s \n", filename);
exit(0);
}
c = fgetc(fptr1);
while (c!= EOF)
{
fputc(c, fptr2);
c = fgetc(fptr1);
}
printf("\nContents copied to %s", filename);
fclose(fptr1);
fclose(fptr2);
return 0;
}

DEPT.OF CSE,BIT 2022-2023 31

You might also like