POP Manual PGM 1-12
POP Manual PGM 1-12
Program 1
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
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
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");
}
}
Program 2
determinant = b*b-4*a*c
If a is equal to 0
Print “Root1”
Root2 = -b/(2*a)
Step 9: Stop
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");
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.
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
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);
}
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
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");
}
}
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
Highn-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
highmid-1
else
lowmid+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
#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”);
}
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 i0 to m-1
for j0 to n-1
read a[i][j]
end for
end for
Step 6: [input elements of matrix B]
for i0 to p-1
for j0 to q-1
read b[i][j]
end for
end for
Step 7: [find product of 2 matrices]
for i0 to m-1
for j0 to q-1
c[i][j]0;
for k0 to n-1
c[i][j]c[i][j]+a[i][k]*b[k][j]
#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
{
Program 7
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
Step 1: Start
Step 2: [Input the number of
elements]read n
Step 3: [Input unsorted elements in
array]for i0 to n-1
read a[i]
Step 4: [Output the original
elements]for i0 to n-1
print a[i]
Step 5: [Sort the elements in ascending
order] for i 0 to n-1
for j0 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
#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]);
}
Step 1: Start
initialize i to 0
increment i by 1
initialize i to 0
break;
increment i by 1
return str1[i]-str2[i];
initialize i to 0 and j to 0
increment i by 1
str1[i++]=str2[j++];
Step 7: stop
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);
}
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
Read n
Read name,m1,m2,m3
else
below avg[k]=i;
k++;
Step 6: Stop
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 i0 to n-1
read a[i]
end for
Step 4: [Assign the array elements to pointer ‘ptr’]
ptra
Step 5: [Calculate sum]
for i0 to n-1
sumsum+ptr[i]
end for
Step 6: [Calculate mean]
meansum/n
Step 7: [Calculate sumstd]
for i0 to n-1
sumstdsumstd + pow((pow(ptr[i]-mean),2)
end for
Step 8: [Calculate standard deviation std]
stdsqrt(sumstd/n)
Step 9: [Output sum,mean and standard deviation]
print sum,mean,std
Step 10: Stop
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
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;
}