Tuesday, November 24, 2015

Ordinal Number Generator in Java

This program will accept an input numerical value from the user and then it will generate the corresponding ordinal number based on the given number by the user using Java as our programming language.

If you  have some questions please send me an email at jake.r.pomperada@gmail.com and jakerpomperada@yahoo.com

My mobile number here in the Philippines is 09173084360.




Program Listing

import java.util.Scanner;


class ordinal3 {

public static String ordinal(int c) {

    int r100 = (c % 100);

    int r10 = (c % 10);

    if(r10 == 1 && r100 != 11) {
        return c + "st";
    } else if(r10 == 2 && r100 != 12) {
        return c + "nd";
    } else if(r10 == 3 && r100 != 13) {
        return c + "rd";
    } else {
        return c + "th";
    }
}


public static void main(String[] args) {

int values=0;

Scanner in = new Scanner(System.in);
System.out.print("\n\n"); 
System.out.print("\t Ordinal Number Generator");
System.out.print("\n\n");
System.out.print("Enter an integer :=> ");


values = in.nextInt();
System.out.print("\n\n");
  for (int a=1; a<=values; a++)
   {
    
      System.out.print(" " +ordinal(a)+ " ");
    }
  
   System.out.print("\n\n");
   System.out.print("End of Program ");
   System.out.print("\n\n");
    }
 }

No comments:

Post a Comment