You are on page 1of 7

# Saint Louis University

## PROBLEMS FOR ALGORITHM DEVELOPMENT

JAVA PROGRAMMING

(COMPROG 2526)
07:30 - 09:30 AM TTHS P508

## Submitted by: Cayabyab, Anjiel D.

Submitted To: Sir Ramel Cabanilla
25. Read a number and calculate and output its factorial.

import java.util.*;
public class Prob25_CAYABYAB {
public static void main(String[] args) {

/* while
System.out.print ("Enter a number: ");
int n = 1;
int f = 1;
while (n<=x){
f = f*n;
n++;
System.out.println ( f + "is factorial of the number. ");
}
*/
/* do while
System.out.print ("Enter a number: ");
int n = 1;
int f = 1;
do{
f = f*n;
n++;
System.out.println ( f + "is factorial of the number. ");
}while (n<=x);
*/
//for
System.out.print ("Enter a number: ");
for (int n = 1, f = 1; n<=x; n++){
f = f*n;
System.out.println ( f + " is factorial of the number. ");
}
}
}

26. Input and print a number (NUM), its reverse by using operators div and mod. If the
last digit is zero, replace it with a one(1) before reversing the number. Output also
the sum of all the digits.
Example 1:
Input: 543267
Output: 762345 (reversed value)
Sum of all digits: 27

Example 2:
Input: 14980 (add 1 to n such that n=14981.)
Output: 18941 (reversed value)
Sum of all digits: 23
30. Calculate and output the volume and area of spheres using the formula:
V = (4PiR3)/3 A = 4PiR2 where R is the radius of the sphere, from 1 to 20.

## public class Prob30_CAYABYAB {

public static void main(String[] args) {

/*while
int r = 1;
while(r<=20){
double v = 4*Math.PI*Math.pow(r,3)/3.0;
double a = 4*Math.PI*Math.pow(r,2);
System.out.printf("\n%4d \t%10.2f \t%8.2f", r,v,a);
r++;
}
*/
/* do while
int r = 1;
do{
double v = 4*Math.PI*Math.pow(r,3)/3.0;
double a = 4*Math.PI*Math.pow(r,2);
System.out.printf("\n%4d \t%10.2f \t%8.2f", r,v,a);
r++;
}while(r<=20);
*/
//for
for (int r = 1; r<=20; r++){
double v = 4*Math.PI*Math.pow(r,3)/3.0;
double a = 4*Math.PI*Math.pow(r,2);
System.out.printf("\n%4d \t%10.2f \t%8.2f", r,v,a);
}
}
}

31. Using the equation Y=2+4X-X2, compute and output values of Y for values of X from 1
to 20, and in increment of 1.

## public class Prob31_CAYABYAB {

public static void main(String[] args) {

/*while
System.out.print("x\t y\t");
int x = 1;
while (x<=20){
int y = 2 + (4*x) - (x*x);
System.out.printf("\n%2d \t%8d", x,y);
x++;
}
*/
/* do While
System.out.print("x\t y\t");
int x = 1;
do{
int y = 2 + (4*x) - (x*x);
System.out.printf("\n%2d \t%8d", x,y);
x++;
}while(x<=20);
*/
//for
System.out.print("x\t y\t");
for (int x = 1; x<=20; x++){
int y = 2 + (4*x) - (x*x);
System.out.printf("\n%2d \t%8d", x,y);
}
}
}

32. An object falling from rest in a vacuum falls 16 feet on the first second, 48 feet
on the 2nd second, 80 feet on the third second, 112 feet the 4th second and so on. Output
the distance traveled by the object after 15 seconds.

## public class Prob32_CAYABYAB {

public static void main(String[] args) {

/* while
System.out.print("Seconds\t Feet\t\n");
int x=1;
int y = 16;
while (x<=15){
System.out.printf("\n%2d \t"+ y,x);
x++;
y = y + 32;
}
*/
/*do While
System.out.print("Seconds\t Feet\t\n");
int x=1;
int y = 16;
do {
System.out.printf("\n%2d \t"+ y,x);
x++;
y = y + 32;
}while (x<=15) ;
*/
//for
System.out.print("Seconds\t Feet\t\n");
for (int x = 1, y=16; x<=15; x++){
System.out.printf("\n%2d \t"+ y,x);
y = y + 32;
}
}
}

## 34. The value of S is computed from the formula:

S = 1 + 1/2 + 1/3 + 1/4 + 1/5 + … + 1/N

Output the number of terms required and the value of S before S exceeds 3.1.
Example:

1st term: S = 1;
2nd term: S = 1 + 1/2 = 1.5;
3rd term: S = 1 + 1/2 + 1/3 = 1.8333;
4th term: S = 1 + 1/2 + 1/3 + 1/4 = 2.08333;
5th term: S = 1 + 1/2 + 1/3 + 1/4 + 1/5 = 2.2833;
...
nth term: S = ?
public class Prob34_CAYABYAB {
public static void main(String[] args) {

/* while
System.out.print("\tN\t \tS\n");
int n = 1;
double s = 0;
while (s <= 3.01){
s = s + (1.0/n);
System.out.printf("\n%4d %8.4f", n,s);
n++;
}
*/
/* do while
System.out.print("\tN\t \tS\n");
int n = 1;
double s = 0;
do {
s = s + (1.0/n);
System.out.printf("\n%4d %8.4f", n,s);
n++;
}while (s <= 3.01);
*/
//for
System.out.print("\tN\t \tS\n");
int n = 1;
for (double s = 0; s <= 3.01; n++){
s = s + (1.0/n);
System.out.printf("\n%4d \t%8.4f", n,s);
}
}
}

## public class Prob35_CAYABYAB {

public static void main(String[] args) {

/* while
int x = 1;
while(x<=150){
System.out.print(x + " ");
x= x+3;
}
*/
/* doWhile
int x = 1;
do{
System.out.print(x + " ");
x = x+3;
}while (x<=150);
*/
// for
for (int x= 1; x<=150; x= x+3){
System.out.print(x + " ");
}
}
}
36. Output the set of numbers in the series 1 2 4 7 11 16 … until 211 is reached.

## public class Prob36_CAYABYAB {

public static void main(String[] args) {

/* while
int x = 1;
int y = 1;
while (x<=211){
x = x + y++;
System.out.print(x + " ");
}
*/
/* doWhile
int x = 1;
int y = 1;
do{
x = x + y++;
System.out.print(x + " ");
}while (x<=211);
*/
for(int x=1, y=1; x<=211; x=x+y++){
System.out.print(x+ " ");
}
}
}

38. A ball is dropped from an initial height of 50 feet. If the ball bounces 2/3 of
the previous height, make a program that will calculate and print the total distance
traveled by the ball after the 10th bounce.

## public class Prob38_CAYABYAB {

public static void main(String[] args) {

/* while
double h = 50.0;
double d = 50.0;
int b = 1 ;
while (b<=10){
h = (2/3.0) * h ;
d = d + (2*h) ;
System.out.println("@b = " + b + "\tThe
distance travelled by the ball is " + d);
b++;
}
*/
/* do while
double h = 50.0;
double d = 50.0;
int b = 1 ;
do {
h = (2/3.0) * h ;
d = d + (2*h) ;
System.out.println("@b = " + b + "\tThe distance travelled by the ball is " +
d);
b++;
}
while (b<=10);
*/
//for
double h = 50.0;
double d = 50.0;
for (int b = 1 ; b<=10; b++){
h = (2/3.0) * h ;
d = d + (2*h) ;
System.out.println("@b = " + b + "\tThe distance travelled by the ball is " +
d);
}
}
}