Sei sulla pagina 1di 2

Programming Exercises Chapter 4 (Loops)

1º) (Display the ASCII character table) Write a program that prints the characters in
the ASCII character table from ! to ~. Display ten characters per line.

public class Ascii {

public static void main(String[] args) {


// TODO Auto-generated method stub
final int CARACTER_POR_LINEA = 12;
final char PRIMER_CARACTER = '!';
final char ULTIMO_CARACTER = '~';

char primero =PRIMER_CARACTER;


for (int count =1;count <=((int)ULTIMO_CARACTER -
(int)PRIMER_CARACTER+1);++count,++primero){
if (count % CARACTER_POR_LINEA == 0)
System.out.println(" "+primero);
else
System.out.print(" "+primero);

}
}

2º) (Compute ) You can approximate by using the following series:


1 1 1 −1𝑖+1
𝜋 = 4 (1 − + − ⋯ )
3 5 7 2𝑖 − 1
Write a program that displays the value for i = 10000, 20000, ..., and 100000.

import java.util.Scanner;
public class ComputePi {

public static void main(String[] args) {


// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
for (int i = 10000;i<=100000;i+=10000){
double pi = 0;
for (int j = 1; j<=i;j++){
pi+=Math.pow(-1, j+1)/(2*j-1);
}
pi *= 4;
System.out.println("El valor de PI para i = "+i+" es= "+pi);
}

3º) (Decimal to hex) Write a program that prompts the user to enter a decimal integer
and displays its corresponding hexadecimal value. Don’t use Java’s Integer.toHexString(int) in this
program.

4º) (Occurrence of max numbers) Write a program that reads integers, finds the largest of them,
and counts its occurrences. Assume that the input ends with number 0. Suppose that you
entered 3 5 2 5 5 5 0; the program finds that the largest is 5 and the occurrence count for 5 is 4.

5º) (Display the first days of each month) Write a program that prompts the user to enter the
year and first day of the year, and displays the first day of each month in the year on the
console. For example, if the user entered the year 2013, and 2 for
Tuesday, January 1, 2013, your program should display the following output:

Potrebbero piacerti anche