×

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

Related Topics

Java Math atan() Method

The atan() method of Math class computes the trigonometric Arc Tangent (inverse of tangent ) of an angle. The value returned is between -pi/2 to pi/2. Syntax: public static double atan(double a) Parameters: The...

2 minutes read.

Java Error Stack Trace

The stack trace in Java is an array of stacks.The stack trace reveals the console's location of an exception or error by gathering data from all program methods. The JVM...

3 minutes read.

How to Iterate a HashMap in Java?

Hash-map is a class of Java collections framework which has the functionality to implement the Map interface of Java. It stores the data in key-value pairs and can be accessed...

5 minutes read.

Java Strings

String class is the most used class in Java programming language. The string is the sequence of characters, which is treated as objects in Java. Creating String objects We create String objects...

5 minutes read.

Grepcode Java util date

What is java.util.Date Class? The date and time in Java are provided through the java.util.Date class. If you imported java.util, it could be helpful. Use the Java.util.Date class to implement this class...

4 minutes read.

Find Unique Elements in Array Java

An array in Java is a grouping of objects that share the same type of data. We are free to enter identical or repeating elements into an array. Therefore, we...

6 minutes read.

Java file Writer

File Writer: It is used to write all the data from text files. It inherits the properties from the Output Stream class. It is meant for writing characters. It is meant...

4 minutes read.

How to check valid date in Java?

Every time we get data for any application, we must first ensure that it is accurate before continuing with any further processing. We might have to confirm the following while dealing...

4 minutes read.

Hybrid Inheritance in Java

The most crucial OOPs concept in Java is inheritance, which enables the transfer of a class's properties to another class. It describes the Is-A relationship generally. We can create a...

3 minutes read.

Java BufferedWriter

BufferWriter Class: It is used to write the data more efficiently. This class is present in the java.io package, it inherits the data from the Writer class. Writer class is...

4 minutes read.

Java Read File to String

There are different ways to deal with forming and examining a text record. This is normal while dealing with various applications. There are different ways to deal with looking at a...

6 minutes read.

Java Math max() Method

The max() method of Math class returns the greater of two arguments. The arguments can be of double, float, int or long data type. Syntax: public static double max(double a, double b)public...

2 minutes read.

Memory Leak in Java

Java offers memory management right out of the box. When we use the new keyword to create an object, the JVM initialises for that object immediately. The trash collector automatically...

3 minutes read.

Computing Digit Sum of all Numbers from 1 to n in Java

In this tutorial, we will discuss various methods to compute the sum of digits of all numbers beginning from 1 to n through a Java program. We will achieve it through...

7 minutes read.

Java import packages

To know about the importing the packages of Java, we need to understand about how to packages work. Packages The package in Java is a collection of Classes and Interfaces. The packages...

3 minutes read.

Why main method is static in Java

The method serves as the entry point for Java programmes or simply the point from which the programme begins to run. As a result, it is one of the most...

4 minutes read.

Contextual keywords in Java

Contextual keywords were earlier known as restricted identifiers and restricted keywords. Context keywords are chosen based on their expected placement in the syntactic grammar. These are the keywords in the code...

3 minutes read.

MessageDigest in Java

The compression of mathematical numbers is accomplished using hash functions. In almost every data security application, hash functions are employed. The hashing, often called has values, returns a value called...

3 minutes read.

Pig Latin Program in Java

Pig Latin Program in Java Pig Latin is a method for translating words of the English language into a different language. It is an encrypted word that is generated by using the following steps....

4 minutes read.

Null Pointer Exception in Java

It is a runtime error exception. The null value is allocated to the object reference in this exception. We will explicitly throw this null pointer exception when the program wants...

3 minutes read.