Beruflich Dokumente
Kultur Dokumente
Exercises in
Fundamentals of
Programming
Submitted by
Perlas, Shanaia F.
Salvador, Christian D.
(SBIT-1B)
Submitted to
Mrs. Lorna Estabillo
October 8, 2018
1|Page
Midterm Period
Exercise #1
1.3 Create a program that will print your experiences in your first day of class. 10 lines.
2|Page
Exercise #2
2.1 Create a program that will declare the following variable and print with the respective
value.
2.2 Declare 2 integer type variables it’s up to you what values they have. The program
will display the value of 2 variables with the sum and product of the 2 variable.
2.3 Declare 2 float variables it’s up to you what values they have. The program will
display the value of 2 variables with difference and quotient of the 2 variable.
public class Perlas_Salvador23{
public static void main(String[] args) {
float s=50.5f;
float c=10.24f;
System.out.println (“1st num= ” + s);
System.out.println (“2nd num= ” + c);
System.out.println (“Quotient= ” + (s/c) );
System.out.println (“Difference= ” + (s*c) );
}
}
Exercise #3
3.1
public class Perlas_Salvador22{
public static void main(String[] args) {
String var1 = “\” CHRISTIAN” ;
String var2 = “SALVADOR \””;
System.out.println ((var1 + var2 ) + “ is my full name ”);
}
}
3|Page
3.2 Create a program that will allow you to input an integer and print it after “You input: “
import java.util,Scanner;
public class Perlas_Salvador32{
public static void main(String[] args) {
Scanner input_new = new Scanner (System.in);
int num1;
System.out.println(“Input number: ”);
num1 = input_new.nextInt ();
System.out.println(“You Input: ” + num1);
}
}
3.3 Create a program that will accept String input. The program will then display “You
input: “ together with the inputted Strings.
import java.util,Scanner;
public class Perlas_Salvador33{
public static void main(String[] args) {
Scanner input_new = new Scanner (System.in);
String a;
System.out.println(“Input Word: ”);
a = input_new.next ();
System.out.println(“\” You Input: ” + a + (“\””) );
}
}
3.4 Create a program that will accept 2 numbers. The program will then display the 30%
of the sum of 2 numbers.
Import java.util,Scanner;
public class Perlas_Salvador32{
public static void main(String[] args) {
Scanner input_new = new Scanner (System.in);
int c;
int s;
System.out.println(“Enter Two Numbers: ”);
c = input_new.nextInt ();
s = input_new.nextInt ();
System.out.println(“30% of the sum of two numbers: ” + ((c + s)* .30));
}
}
4|Page
Exercise #4
4.1 Create a program that will enter an amount and will determine the number of bills and
coins (50’s, 20’s, 10’s, 5’s, 1’s)
import java.util.Scanner;
public class Perlas_Salvador42{
static Scanner in = new Scanner(System.in);
public static void main (String[]args){
}
}
4.2 Create a program that will prompt the user to enter student name, grades and quiz,
class standing and exam. The program will compute the total grades base on the criteria.
Quiz 40% + Class Standing 10% + Exam 50%. The program will also display
whether the grade is “Passed” or “Failed”
import java.util.Scanner;
public class Perlas_Salvador42{
String name;
String surname;
int quiz, cs, exam, grade;
double quiz1 = .40;
double cs1 = .10;
double exam1 = .50;
int q, c, e;
System.out.print("Enter first name: ");
name = input.next();
System.out.print("Enter last name: ");
surname = input.next();
System.out.print("Quiz: ");
quiz = input.nextInt();
System.out.print("Class Standing: ");
cs = input.nextInt();
System.out.print("Exam: ");
exam = input.nextInt();
System.out.print(name + " " + surname );
System.out.print("‘s grade is");
5|Page
Exercise #5
5.1 Create a program that will compute the area of the following:
Codes:
Circle = Pi Radius ² ‘c’ or ‘C’
Rectangle = Length * Width ‘r’ or ‘R’
Triangle = ½bh ‘t’ or ‘T’
Square= Side ² ‘s’ or ‘S’
import java.util.Scanner;
public class Perlas_Salvador51{
char code;
int radius, length, width, base, height, side;
switch(code) {
case 'c':
System.out.print("\tEnter radius: ");
radius=in.nextInt();
System.out.print("\tThe area of circle is: " + (3.14 * radius*radius));
break;
case 'C':
System.out.print("\tEnter radius: ");
radius=in.nextInt();
System.out.print("\tThe area of circle is: " + (3.14 * radius*radius));
break;
case 'r':
System.out.print("\tEnter length: ");
length=in.nextInt();
System.out.print("\tEnter width: ");
width=in.nextInt();
System.out.print("\tThe area of Rectangle is: " + (length*width));
break;
case 'R':
System.out.print("\tEnter length: ");
length=in.nextInt();
System.out.print("\tEnter width: ");
width=in.nextInt();
System.out.print("\tThe area of Rectangle is: " + (length*width));
break;
case 't':
System.out.print("\tEnter base: ");
base=in.nextInt();
System.out.print("\tEnter height: ");
height=in.nextInt();
System.out.print("\tThe area of Triangle is: " + (base*height)/2);
break;
case 'T':
System.out.print("\tEnter base: ");
base=in.nextInt();
6|Page
System.out.print("\tEnter height: ");
height=in.nextInt();
System.out.print("\tThe area of Triangle is: " + (base*height)/2);
break;
case 's':
System.out.print("\tEnter side: ");
side=in.nextInt();
System.out.print("\tThe area of Square is: " + (side*side));
break;
case 'S':
System.out.print("\tEnter side: ");
side=in.nextInt();
System.out.print("\tThe area of Square is: " + (side*side));
break;
default:
System.out.print("Invalid Entry");
break;
}
}
}
5.2 Create a program that will read a salesman name and sales. Compute for the salary.
Salary=Sales + Commission. (Nested if)
import java.util.Scanner;
public class Perlas_Salvador52{
String name;
int sales;
double com, salary;
Final Period
Final Exercise #1
1.1 A program that will read your name and age. Print the name and age 6 times if age is
less than 25, otherwise print only the name. (if else and do while)
import java.util.Scanner;
public class Perlas_Salvador_FE11{
do{
System.out.print("Enter name: ");
name=in.next();
if (age< 25)
for(int x=1; x<=6; ++x)
System.out.println( + x + " " + name + ", " + age);
else {
System.out.println(name + ", " + age);
}
System.out.println("Press key 1 to enter another record: ");
key=in.nextInt();
}
while (key ==1);
System.out.print("Program Terminated! ");
}
}
1.2 A Program that will print numbers from 1-10 using for loop, numbers from 20-1 using
while loop and numbers from 3-60 divisible by 3 using do while.
8|Page
import java.util.Scanner;
public class Perlas_SalvadorFE12{
String key;
System.out.println("\nPress any key to continue…");
key=shai.next();
{
int b = 20;
while (b >= 1) {
System.out.println("Number: " + b);
b--;
}
{
int c=1;
do {
System.out.println("Number: " + (c*3));
c++;
}
while (c<=20);
}
System.out.println("\nEnd of Program\n: " );
}
}
Final Exercise #2
import java.util.Scanner;
public class Perlas_SalvadorFE2 {
9|Page
return result;
}
public static void PrintInfo(int newAge){
System.out.println("Fundamentals of Programming");
}
public static void main (String[]args) {
int age;
int newAge;
char key;
try {
do{
}
while ((key == 'y') || (key == 'y'));
System.out.println("Program Complete");
catch(Exception e){
System.out.println("Wrong input!!!" );
}
}
}
Final Exercise #3
3.1 Declare an array named months and print its elements. It has twelve elements.
10 | P a g e
System.out.print("Num["+ i + "]: " + months[i] + "\n");
}
System.out.print("\nPROJECT TERMINATED!!");
}
}
3.2 Create a program that will allow you to enter 3 tuition fees and print those three
together.
import java.util.Scanner;
public class Perlas_Salvador {
}
}
3.3 Create four arrays and will print the lowest value and specify its index number.
import java.util.Scanner;
public class Perlas_SalvadorFE33 {
System.out.println(" ");
for (int i=0; i<num.length; ++i)
11 | P a g e
{
System.out.print("Number [" +i + "]: ");
num[0]= chris.nextInt();
}
int low = num[0];
for (int i=low; i<num.length; i--)
{
if (num[i] < low) low = num[i];
}
System.out.println("The lowest number is: " + low + " at index[" + "]");
}
}
12 | P a g e