/*
* Autor: Juan Antonio Ripoll
* Date: 30/10/13
*
* Create a JAVA program to ask the user for a number "x" and display 10*x.
* It must repeat until the user enters 0 (using "do-while").
* */
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int number;
do
{
System.out.print("Enter a number: ");
number = in.nextInt();
System.out.println(number*10);
}
while (number != 0);
}
}
Wednesday, October 30, 2013
2.07 - Repeat until 0 do while
2.06 - Repeat until 0
/*
* Autor: Juan Antonio Ripoll
* Date: 30/10/13
*
* Create a JAVA program to ask the user for a number "x"
* and display 10*x. It must repeat until the user enters 0 (using "while").
* */
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = in.nextInt();
while (number != 0)
{
System.out.println(number*10);
System.out.print("Enter a number: ");
number = in.nextInt();
}
}
}
2.05 - Greatest of three numbers
/*
* Autor: Juan Antonio Ripoll
* Date: 30/10/13
*
* Write a JAVA program to get three numbers
* from the user and display the greatest one.
* */
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the first number: ");
int number1 = in.nextInt();
System.out.print("Enter the second number: ");
int number2 = in.nextInt();
System.out.print("Enter the third number: ");
int number3 = in.nextInt();
if (number1 > number2)
if (number1 > number3)
System.out.println("The greatest: " + number1);
if (number2 > number1)
if (number2 > number3)
System.out.println("The greatest: " + number2);
if (number3 > number1)
if (number3 > number2)
System.out.println("The greatest: " + number3);
}
}
2.04 - Divide if not zero using else
/*
* Autor: Juan Antonio Ripoll
* Date: 30/10/13
*
* Write a JAVA program to ask the user for two numbers, and show
* their division if the second number is not zero; otherwise,
* it will display "I cannot divide
* */
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the first number: ");
int number1 = in.nextInt();
System.out.print("Enter the second number: ");
int number2 = in.nextInt();
if (number2!=0)
System.out.print(number1 + " / " + number2 + " = "
+ number1/number2);
else
System.out.print("I cannot divide.");
}
}
2.03 - Divide if not zero
/*
* Autor: Juan Antonio Ripoll
* Date: 30/10/13
*
* Write a JAVA program to ask the user for two numbers, and show
* their division if the second number is not zero; otherwise,
* it will display "I cannot divide
* */
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the first number: ");
int number1 = in.nextInt();
System.out.print("Enter the second number: ");
int number2 = in.nextInt();
if (number2!=0)
System.out.print(number1 + " / " + number2 + " = "
+ number1/number2);
else
System.out.print("I cannot divide.");
}
}
2.02 - Multiply if not zero
/*
* Autor: Juan Antonio Ripoll
* Date: 30/10/13
*
* Write a JAVA program to ask the user for a number; if it is not zero,
* then it will ask for a second number and display their product;
* otherwise, it will display "0".
* */
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the first number: ");
int number1 = in.nextInt();
if (number1!=0)
{
System.out.print("Enter the second number: ");
int number2 = in.nextInt();
System.out.print(number1 + " X " + number2 + " = "
+ number1*number2);
}
else
System.out.print(number1);
}
}
2.01 - Positive and negative
/*
* Autor: Juan Antonio Ripoll
* Date: 30/10/13
*
* Write a JAVA program to get a number from the and
* answer whether it is positive or negative.
* */
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = in.nextInt();
if (number>0)
System.out.println(number + " is positive.");
if (number<0)
System.out.println(number + " is negative.");
}
}
1.14 - Conversion
/*
* Autor: Juan Antonio Ripoll
* Date: 30/10/13
*
* Create a JAVA program to convert from celsius degrees to Kelvin
* and Fahrenheit: it will ask the user for the amount of celsius degrees
* and using the following conversion tables:
kelvin = celsius + 273
fahrenheit = celsius x 18 / 10 + 32
* */
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the amount of celsius: ");
int celsius = in.nextInt();
System.out.println("Kelvin = " + celsius + 273);
System.out.println("Fahrenheit = " + celsius * 18 / 10 + 32);
}
}
1.13 - Rectangle
/*
* Autor: Juan Antonio Ripoll
* Date: 30/10/13
*
* Write a JAVA program to ask the user for a number and
* then display a rectangle 3 columns wide and 5 rows tall
* using that digit. For example:
Enter a digit: 3
333
3 3
3 3
3 3
333
* */
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a digit: ");
int digit = in.nextInt();
for (int row=0;row<5;row++)
{
for (int column=0;column<3;column++)
{
if (row == 0 || row == 4)
System.out.print(digit);
else
{
if (column == 1)
System.out.print(" ");
else
System.out.print(digit);
}
}
System.out.println();
}
}
}
1.12 - Formats
/*
* Autor: Juan Antonio Ripoll
* Date: 30/10/13
*
* Write a JAVA program to ask the user for a number and display
* four times in a row, separated with blank spaces,
* and then four times in the next row, with no separation.
* */
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a digit: ");
int digit = in.nextInt();
System.out.println(digit + " " + digit + " " + digit + " " + digit);
System.out.println(digit + "" + digit + "" + digit + "" + digit);
}
}
1.11 - Age
/*
* Autor: Juan Antonio Ripoll
* Date: 30/10/13
*
* Write a JAVA program to ask the user for three numbers (a, b, c)
* and display the result of (a+b)·c and the result of a·c + b·c
* */
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a number a: ");
int number1 = in.nextInt();
System.out.print("Enter a number b: ");
int number2 = in.nextInt();
System.out.print("Enter a number c: ");
int number3 = in.nextInt();
System.out.println("(a+b)·c = " + (number1 + number2) * number3);
System.out.println("a·c + b·c = " + ( (number1 * number3) + (number2*number3) ));
}
}
1.10 - Equivalent operations
/*
* Autor: Juan Antonio Ripoll
* Date: 30/10/13
*
* Write a JAVA program to ask the user for his age
* (20, for instance) and answer something as "You look younger than 20"
* (instead of 20, you should display the age that has been entered).
* */
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a age: ");
int age = in.nextInt();
System.out.println("You look younger than " + age + ".");
}
}
1.09 - Average
/*
* Autor: Juan Antonio Ripoll
* Date: 30/10/13
*
* Write a JAVA program to calculate and display the average
* of four numbers entered by the user.
* */
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a number1: ");
int number1 = in.nextInt();
System.out.print("Enter a number2: ");
int number2 = in.nextInt();
System.out.print("Enter a number3: ");
int number3 = in.nextInt();
System.out.print("Enter a number4: ");
int number4 = in.nextInt();
System.out.println("Average: " +
(number1 + number2 + number3 + number4) / 4);
}
}
1.08 - Multiplication table
/*
* Autor: Juan Antonio Ripoll
* Date: 30/10/13
*
* Write a JAVA program to ask the user for a number and
* display its multiplication table, like this:
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
...
5 x 10 = 50
* */
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
int number1 = in.nextInt();
for (int i=0; i<10;i++){
System.out.println(number1 + " x " + (i+1) + " = " +
(number1 * (i+1)));
}
}
}
1.07 - Several operations
/*
* Autor: Juan Antonio Ripoll
* Date: 30/10/13
*
* Write a JAVA program to print on screen the result of adding,
* subtracting, multiplying and dividing two numbers typed by the user.
* The remainder of the division must be displayed, too.
It might look like this:
Enter a number: 12
Enter another number: 3
12 + 3 = 15
12 - 3 = 9
12 x 3 = 36
12 / 3 = 4
12 mod 3 = 0
* */
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
int number1 = in.nextInt();
System.out.print("Enter another number: ");
int number2 = in.nextInt();
System.out.println(number1 + " + " + number2 + " = " +
(number1 + number2));
System.out.println(number1 + " - " + number2 + " = " +
(number1 - number2));
System.out.println(number1 + " x " + number2 + " = " +
(number1 * number2));
System.out.println(number1 + " / " + number2 + " = " +
(number1 / number2));
System.out.println(number1 + " mod " + number2 + " = " +
(number1 % number2));
}
}
1.06 - Use of comments
/*
* Autor: Juan Antonio Ripoll
* Date: 30/10/13
*
* Write a JAVA program to ask the user for three numbers
* and display their multiplication.
* The first line must be a comment with your name and surname.
* It MUST look as follows:
Enter the first number to multiply
12
Enter the second number to multiply
23
Enter the third number to multiply
2
* */
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter the first number to multiply");
int number1 = in.nextInt();
System.out.println("Enter the second number to multiply");
int number2 = in.nextInt();
System.out.println("Enter the third number to multiply");
int number3 = in.nextInt();
System.out.println(number1 + " x " + number2 + " x " + number3 + " = " +
number1 * number2 * number3);
}
}
1.05 - Multiple operations and precedence
/*
* Autor: Juan Antonio Ripoll
* Date: 30/10/13
*
* Write a JAVA program to print the result of multiplying two numbers which
* will entered by the user.
* */
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter 1st number: ");
int number1 = in.nextInt();
System.out.print("Enter 2st number: ");
int number2 = in.nextInt();
System.out.println(number1 + " x " + number2 + " = " + number1 * number2);
}
}
1.04 - Multiple operations and precedence
/*
* Autor: Juan Antonio Ripoll
* Date: 30/10/13
*
* Create a JAVA program to print the result of the following operations:
• -1 + 3 * 5
• (24+5) % 7
• 15 + -4*6 / 11
• 2 + 10 / 6 * 1 - 7 % 2
* */
public class Main {
public static void main(String[] args) {
System.out.println(-1+3*5);
System.out.println((24+5) % 7);
System.out.println(15+-4*6/11);
System.out.println(2+10/6*1-7%2);
}
}
1.03 - Division of two numbers
/*
* Autor: Juan Antonio Ripoll
* Date: 30/10/13
*
* Write a JAVA program to print the result of
* dividing 24 into 5 on screen.
* */
public class Main {
public static void main(String[] args) {
System.out.println(24/5);
}
}
1.02 - Sum two numbers
/*
/*
* Autor: Juan Antonio Ripoll
* Date: 30/10/13
*
* Write a JAVA program to print the result of
* adding 12 and 13 on screen.
* */
public class Main {
public static void main(String[] args) {
System.out.println(12+13);
}
}
1.01 - First program in JAVA
/*
* Autor: Juan Antonio Ripoll
* Date: 30/10/13
*
* Create a program (in JAVA) to print Hello on screen
* and then print your name (in a separate line).
* */
public class Main {
public static void main(String[] args) {
System.out.println("Hello\nJuan!");
}
}
Subscribe to:
Comments (Atom)