BERNARDO CompilationCPF PDF
BERNARDO CompilationCPF PDF
IN
COMPUTER PROGRAMMING
Exercise 1 ……………………………………………………………………… 18
Exercise 2 ……………………………………………………………………… 20
Exercise 3 ……………………………………………………………………… 21
Exercise 4 ……………………………………………………………………… 23
Exercise 5 ……………………………………………………………………… 25
Exercise 6 ……………………………………………………………………… 26
Exercise 7 ……………………………………………………………………… 27
Exercise 8 ……………………………………………………………………… 28
Input ……………………………………………………………………… 30
Output ……………………………………………………………………… 33
Flowchart ……………………………………………………………………… 34
Explanation ……………………………………………………………………… 37
1.MACHINE EXERCISE 1
#include<iostream.h>
#include<conio.h>
#include<math.h>
int main()
{
clrscr();
float x, y, z;
cout<<"Enter the length of first side (X): ";
cin>>x;
cout<<"Enter the length if the second side (Y): ";
cin>>y;
z=sqrt((x*x)+(y*y));
cout<<"The length of the triangle's hypotenuse is "<<z;
getch();
return 0;
}
OUTPUT
2.MACHINE EXERCISE 1.2
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int x, y;
cout<<"Please enter first number: ";
cin>>x;
cout<<"Please enter second number: ";
cin>>y;
cout<<"Sum: "<<(x+y)<<"\n";
cout<<"Difference: "<<(x-y)<<"\n";
cout<<"Product: "<<(x*y)<<"\n";
cout<<"Quotient: "<<(x/y);
getch();
return 0;
}
OUTPUT:
3.MECHINE EXERCISE 1.3
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int x, y;
cout<<"Enter Fahrenheit value: ";
cin>>x;
y=(((x-32)*5)/9);
cout<<"For a Fahrenheit temperature of "<<x;
cout<<" degrees, the equivalent Celsius temperature is "<<y<<" degrees";
getch();
return 0;
}
OUTPUT:
4.MACHINE EXERCISE 2
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int a;
cout<<"Please enter this week's gross sales: ";
cin>>a;
if(a>=0)
{
cout<<"Your commsion this week is ₱"<<(a*0.09);
cout<<"\nYour total earnings this week is ₱"<<((a*0.09)+200);
}
Else
{
cout<<"Program must terminate, no output";
}
getch();
return 0;
}
OUTPUTS:
5.MACHINE EXERCISE 2.2
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int a, b, c ,d;
cout<<"Please enter your Prelim Grade: ";
cin>>a;
cout<<"Please enter your Midterm Grade: ";
cin>>b;
cout<<"Please enter your Final Grade: ";
cin>>c;
d=((a*0.3)+(b*0.3)+(c*0.4));
cout<<"Your semestral grade is "<<d<<"\n";
if(d==100)
{
cout<<"Equivalent letter is A";
}
else if(d<=99&&d>=90)
{
cout<<"Equivalent letter is B";
}
else if(d<=89&&d>=80)
{
cout<<"Equivalent letter is C";
}
else if(d<=79&&d>=75)
{
cout<<"Equivalent letter is D";
}
else if(d<75)
{
cout<<"Equivalent letter is F";
}
getch();
return 0;
}
OUTPUTS:
6.MACHINE EXERCISE 3
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
char a;
int x, y;
cout<<"Please enter the first number: ";
cin>>x;
cout<<"Please enter the second number: ";
cin>>y;
cout<<"Choose an operation:\n";
cout<<" a=addition\n s=subtraction\n m=multiplication\n d=division\n";
cout<<"Please enter your desired operation: ";
cin>>a;
switch(a)
{
case 'a':
cout<<”The sum of the two numbers you entered is “<<(x+y);
break;
case 's':
cout<<”The difference of the two numbers you entered is “<<(x-y);
break;
case 'm':
cout<<”The product of the two numbers you entered is “<<(x*y);
break;
case 'd':
cout<<”The quotient of the two numbers you entered is “<<(x/y);
break;
default:
cout<<”No match for chosen operation";
}
getch();
return 0;
}
OUTPUTS:
7.MACHINE EXERCISE 3.2
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int a, b, c, d;
cout<<"Please enter your Prelim grade: ";
cin>>a;
cout<<"Please enter your Midterm grade: ";
cin>>b;
cout<<"Please enter your Final grade: ";
cin>>c;
d=((a*0.3)+(b*0.3)+(c*0.4));
cout<<"Your semestral grade is "<<d<<"\n";
switch(d){
case (100):
cout<<"Your equivalent letter is A";
break;
case (99):
case (98):
case (97):
case (96):
case (95):
case (94):
case (93):
case (92):
case (91):
case (90):
cout<<"Your equivalent letter is B";
break;
case (89):
case (88):
case (87):
case (86):
case (85):
case (84):
case (83):
case (82):
case (81):
case (80):
cout<<"Your equivalent letter is C";
break;
case (79):
case (78):
case (77):
case (76):
case (75):
cout<<"Your equivalent letter is D";
break;
default:
cout<<"Your equivalent letter is F";
}
getch();
return 0;
}
OUTPUTS:
8.MACHINE EXERCISE 4
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int x=1, n=5;
cout<<"(N)\t(N2)\t(N3)\t(N4)\n";
while(x<=n)
{
cout<<x<<"\t"<<x*x<<"\t"<<x*x*x<<"\t"<<x*x*x*x<<endl;x++;
}
getch();
return 0;
}
OUTPUT:
9.MACHINE EXERCISE 4.2
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
float a,b,c=1;
cout<<"Please enter 10 numbers: "<<endl;
while (c<=10)
{
cin>>a;
b=b+a;c++;
}
cout<<"The average of the 10 numbers you have entered is "<<(b/10);
getch();
return 0;
}
OUTPUT:
10.MACHINE EXERCISE 5
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int a,b=1,c=1;
cout<<"Please enter any non-negative integer: ";
cin>>a;
do
{
b=b*c;c++;
}while (c<=a);
cout<<a<<" factorial is "<<b;
getch ();
return 0;
}
OUTPUT:
11.MACHINE EXERCISE 5.2
#include<iostream.h>
#include<conio.h>
#include<math.h>
int main()
{
clrscr();
float a=0,pi=3.14;
cout<<"Angle\t sin x\t cos x\t tan x\n";
do
{
cout<<a<<"\t"<<sin((a*pi)/180)<<"\t"<<cos((a*pi)/180)<<"\t"<<tan((a*pi)/180)<<endl;a++;
}
while (a<=20);
getch ();
return 0;
}
OUTPUT:
12. MACHINE EXERCISE 6
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int x;
float c,f;
for(x=1;x<=6;x++)
{
cout<<"Enter a fahrenheit temperature value: ";
cin>>f; c=(f-32)*5/9;
cout<<c<< " degrees celcius" <<endl;
}
getch();
return(0);
}
OUTPUT:
13. MACHINE EXERCISE 6.2
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int x;
cout<<"X\t"<<"1\t"<<"2\t"<<"3\t"<<"4\t" <<"5\t"<<"6\t"<<"7\t"<<"8\t"<<"9\t";
for(x=1;x<=10;x++)
{
cout<<x<<'\t'<<x<<'\t' <<x*2<<'\t'<<x*3<<'\t'<<x*4 <<"\t"<<x*5<<"\t"<<x*6<<"\t"<<x*7
<<"\t"<<x*8<<'\t'<<x*9<<endl;
}
getch();
return(0);
}
OUTPUT:
Laboratory Exercise 1
COMMUNICATION THROUGH CONSOLE
Direction: Demonstrate the corresponding output for each program in the boxes below.
Give your analysis and observation for each output.
7.
It used the command (int a, b;) that
determines the value of the variable a and b
are integer. It also used (--) to subtract the
integer by 1.
Laboratory Exercise 2
2D GRAPHICS
Direction: Demonstrate the corresponding output for each program in the boxes
below. Give your analysis and observation for each output.
Direction: Demonstrate the corresponding output for each program in the boxes
below. Give your analysis and observation for each output.
Direction: Demonstrate the corresponding output for each program in the boxes
below. Give your analysis and observation for each output.
Direction: Demonstrate the corresponding output for each program in the boxes below. Give
your analysis and observation for each output.
Direction: Demonstrate the corresponding output for each program in the boxes below. Give
your analysis and observation for each output.
Direction: Demonstrate the corresponding output for each program in the boxes below. Give
your analysis and observation for each output.
Direction: Demonstrate the corresponding output for each program in the boxes below. Give
your analysis and observation for each output.
int main()
{
clrscr();
float a,e,f=0,g=1,y;
char order;
textbackground(GREEN);
cout << "=====================" << endl;
cprintf (" Ayabells Menu ");
cout << "\n=====================" << endl;
cout << endl;
cout << "Student Meal:" << endl;
cout << "(A) Siomai and Rice for Php 25.00" << endl;
cout << "(B) Sisig and Rice for Php 35.00" << endl;
cout<< "(C) Chicken skin and Rice for Php 35.00"<<endl;
cout<< "(D) Fishballs 8 pieces for Php 5.00"<<endl;
cout << "How many order?" << endl;
cin >> y;
if(y<1000&&y>0){
while (g<=y) {
cout << "What do you want to order? " << endl;
cin >> order;
switch (order) {
case 'a':
case 'A':
cout << "You ordered siomai and rice."<<endl;
a=25;
break;
case 'b':
case 'B':
cout << "You ordered sisig and rice."<<endl;
a=35;
break;
case 'c':
case 'C':
cout<<"You ordered chicken skin and rice."<<endl;
a=35;
break;
case 'd':
case 'D':
cout<<"You ordered fishballs."<<endl;
a=5;
break;
default:
cout << "Invalid entry please try again. ";
break;
}
f= a+f;
cout << "Your total bill is " <<f<< endl;
g++;
}
}
else{
cout<<"Invalid Input\n";}
cout << "Thank You for coming!" <<endl;
getch();
return 0;
}
OUTPUT:
FLOWCHART:
NO
YES
NO
YES
NO
YES
EXPLANATION OF THE PROGRAM:
This program shows the student meal from the Ayabells menu which contains food like
siomai and rice, sisig and rice, chicken skin and rice, and fishballs. It is designed to take the
quantities of the order in the given menu and to compute for the total bill for every purchase. I
used iostream.h and conio.h that are both preprocessor statement that has header file that
consoles input and output stream. I used char a data type that is like the int command, it is an
integral type that means the underlying value is stored as an integer. I used float it's a
fundamental data type built into the compiler that's used to define numeric values with floating
decimal points. For me to get the green background color I used the textbackground and also
used the cprintf it will print out the given text that I made and will not be included in the color
of its background. I inputted while statements a looping statement this allows to repeatedly run
the same block of code until the condition is met and if statement which allows the user to
control if a program enters a section of code or not based whether a given condition is true or
false. Putting the switch allows a variable to be tested for equality against a list of values, for
each value is called a case which the variable being switched on is checked for each case and
break a looping control statement this is use so the I can leave a loop even if the condition for its
end is not fulfilled. I also inputted (f=a+f) a command that I used to get the total bill of the order.
And used else if the condition is false it will proceed as an invalid input that will end the
program instantly. You will know the transaction is completed when the output of the “Thank
You for coming!” appeared because that is the last part of the program .