How to print ordinal numbers in Java – This specific article deals with the code for representing a user input number in its ordinal form using Java language.
Sample output along with suitable examples will be provided for a proper understanding of the java program for ordinal numbers converter.
- The problem here is to represent a user-defined number in its ordinal form (Example: 42 as 42nd).
- The input here is a number provided by the user, to change the number to ordinal java.
- The output here is the ordinal form of the user-defined input number.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.Scanner; class EndLetters { static String[] s = new String[] {"th","st","nd","rd","th"}; public static void main(String arg[]) { Scanner sc=new Scanner(System.in); System.out.println("Enter the number"); int n = sc.nextInt(); System.out.println("EndLettersofNumbers("+n+")--->"+"\""+n+"-"+ELON(n)+"\""); } static String ELON(int n) { int k=n%10; if(k>=4) return s[4]; else return s[k]; } } |
Output – 1:
Solution :
- In the class EndLetters, a static string array referenced by variable s is initialized as a field variable.
- Since this array is declared as static, it does not belong to any object or instance of the class but belongs to the class itself.
- It is not a part of any method in the class and hence can be accessed by all the methods.
- The string values representing the end letters of an ordinal number (i.e.”st” | “nd” | “rd” | “th” date java) are stored in the array.
- In the main method, a new Scanner class object is initialized and a reference variable sc is set to represent the object. This class is used to take user input in Java.
- Scanner class is part of the java.util package and hence the import statement, in the beginning, is stated to import the functionality of the specified class.
- The next line prints a statement to the console instructing the user to enter a number.
- The .nextInt() method is called by the Scanner object sc. This method reads an int value input by a user and stores it to the int variable n.
- Within the print statement in the last line of the main method, the method ELON is called and n is passed in as the argument for ordinal numbers converter.
12345static String ELON(int n) {int k=n%10;if(k>=4) return s[4];else return s[k];} - In this method, the units place value of the input number is calculated and stored in variable k (n%10 finds the remainder when n is divided by 10) to determine the appropriate suffix for the number.
- A conditional statement returns the appropriate suffix by checking the value of k and returning the suffix string value by obtaining it from the array s.
- This returned string suffix is printed out along with the number to represent the ordinal form of the user input number by the print statement in the main method.
Output – 2:
1 2 3 |
Enter the number 455 EndLettersofNumbers(455)--->"455-th" |
- Since the unit’s place number, in this case, is 5, the if condition in the ELON method is satisfied and the element at position 4 in the array (which is ‘th’) is returned. 455th is the correct representation of 455 in ordinal form.
Output – 3:
- Since the unit’s place number, in this case, is 2, the else condition in the ELON method is satisfied and the element at position 2 in the array (which is ‘nd’) is returned. 122nd is the correct representation of 122 in ordinal form.
1 2 3 |
Enter the number 122 EndLettersofNumbers(122)--->"122-nd" |