This is one of the first programs in the AP Computer Science A course at school. That course mainly focused on the basics of Java programming, following the Codeio exercises as means to practice.
The magic five is an exercise in which a user inputs a number greater than 0, and through a series of operations the final output will always be five, regardless of the initial input.
In this example the course illustrated the use of different basic operations in Java, as well as the syntax of variable incrementation.
//Name:
//Date:
//Description:
import java.util.Scanner;
public class MagicFive
{
public static void main(String[] args)
{
// code to get input for the user is provided here.
// use of the Scanner class will be explained on Day 7
Scanner kb = new Scanner(System.in);
System.out.print("Enter a number: ");
int original = kb.nextInt();
int answer = original;
System.out.println("Adding the next highest number.");
answer += original + 1;
System.out.println(answer);
System.out.println("Adding 9 to the number.");
answer += 9;
System.out.println(answer);
System.out.println("Dividing the number by 2.");
answer /= 2;
System.out.println(answer);
System.out.println("Subtracting the original number.");
answer -= original;
System.out.println(answer);
// add your code here
}
}