Ordinal Number in Java
What is the Ordinal Number in Java?
An object's or person's position or rank can be expressed mathematically using an ordinal number. Depending on the criteria used to establish the positions, including size, weight, markings, etc., the ordinal number sequence will change. Ordinals is another name for the ordinal numbers.
Rankings are represented by ordinal numbers. In other words, ordinal numbers are used to represent those integers that define position or ranking. Several sentences employ ordinal numerals, including:
- China has the most people per square mile, with India coming in second.
- At the 2016 Rio Olympics, Usain Bolt placed first in the sprinting competition.
- The Marshal of an Air Force is in the first position as in Indian Air Force.
- Neil Armstrong was the first person to set foot on the moon.
- The first element in the set S = x, y, u, i, p, a, f, h, j, q, l is x.
- The first integer in the series of cosines is 1.
Instances of Ordinal Numbers in the Real World
- Ram won the class contest.
- The third girl to stand in a line was Sarita.
- The apartment's fourth story is where Mohan resides.
- In the shop's display, the table is placed in position sixteen.
Ordinal Numbers and Order
We choose the position whenever we desire to organize something in a specific order, which then results in the use of ordinal numbers.
Sorting
We frequently use the positions of elements (ordinals), whether consciously or unconsciously, to do the sorting. For instance, when using selection sort to sort a number array in ascending order, the smallest entry is chosen 1st. This is so that the smallest element can occupy the top spot in the array. The 2nd member in the array is the second smallest element. The 3rd smallest element also occupies the 3rdslot.
The largest member is placed at the top of the array as we sort in decreasing order. The next-largest component is placed in slot 2, and so on. Ordinals are relevant in sorting because sorting is a kind of order.
Ordinal Number Example
It is typical to see pupils arranged in lines or school assemblies according to height. The shortest student in the class is first in line, while the tallest student is positioned last. As a result, we are ranking students based on height. To put it another way, the students are arranged according to their height. Since every student is given an ordinal number
In a similar vein, grades can also be used as a ranking criterion. The student who receives the highest grade is placed first. Other ranks may also be chosen in the same manner.
Java Program for the Series of Ordinals
A relative position is indicated by an ordinal number, such as 1st, 2nd, 3rd, etc. To put it another way, ordinal integers series are words that describe a number's rank about a certain order, in a certain order or location. To assess this, we divided the given number by 10 and 100 to obtain the modulus.
The modulus was then subtracted twice. Then, if the value of the result is 10, the number is provided with the suffix "th." We have demonstrated that if an integer's modulus by dividing it by 10 equals 1, it will give "st," if it is 2, "nd," if it is 3, "rd," and for any other number, it will return "nd." the remainder, "th" will be returned. The first ten ordinal numbers will be shown here.
Example code
public class OrdinalNumber {
public static String ordinalNo(int value) {
int hunRemainder = value % 100;
int tenRemainder = value % 10;
if (hunRemainder - tenRemainder == 10) {
return "th";
}
switch (tenRemainder) {
case 1:return "st";
case 2:return "nd";
case 3:return "rd";
default:return "th";
}
}
public static void main(String[] args) {
OrdinalNumber number = new OrdinalNumber();
for (int i = 1; i<= 10; i++) {
String st = number.ordinalNo(i);
System.out.println(i + " = " + i + st);
}
}
}
The output of the program
java -cp /tmp/5zC3gSEOHI OrdinalNumber
1 = 1st
2 = 2nd
3 = 3rd
4 = 4th
5 = 5th
6 = 6th
7 = 7th
8 = 8th
9 = 9th
10 = 10th