Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /home/purvgwtw/public_html/tutorials/common.php on line 7
Find Ordinal Number
Sponsered Links
Categories
Sponsered Links

Find Ordinal Number

Through the given code, we are finding the ordinal number.

public class OrdinalNumber {
    public static String findOrdinalNo(int value) {
        int h = value % 100;
        int t = value % 10;
        if (h - t == 10) {
            return "th";
        }
        switch (t) {
        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 <= 5; i++) {
            String st = number.findOrdinalNo(i);
            System.out.println(i + " = " + i + st);
        }
    }
}

Sponsered Links
Latest Updates
 
All Content of this site is for learning only. We do not warrant the correctness of its content. The risk from using it lies entirely with the user. While using this site, you agree to have read and accepted our terms of use and privacy policy.
Copyright © 2014 PURVACOMPUTERS.COM All Right Reserved