0% found this document useful (0 votes)
68 views2 pages

Programming Exercises Chapter 4

The document provides 5 programming exercises involving loops, with the first asking to print the ASCII character table from ! to ~ in groups of 10 per line, the second approximating pi using a series, the third prompting the user to enter a decimal number and outputting the hexadecimal value, the fourth reading numbers, finding the largest, and counting its occurrences, and the fifth prompting the user for a year and first day and displaying the first day of each month.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views2 pages

Programming Exercises Chapter 4

The document provides 5 programming exercises involving loops, with the first asking to print the ASCII character table from ! to ~ in groups of 10 per line, the second approximating pi using a series, the third prompting the user to enter a decimal number and outputting the hexadecimal value, the fourth reading numbers, finding the largest, and counting its occurrences, and the fifth prompting the user for a year and first day and displaying the first day of each month.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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:

You might also like