Thread: Program for entering the Ordinal of a Number

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    3

    Program for entering the Ordinal of a Number

    Hi Guys,

    I'm learning abit of C by my self through a variety of books and online tutorials. This site and forum has been brilliant, i have been looking through the forums and tutorials and have received lots of help but this is my 1st time posting.

    I am trying to write a a program that enters the ordinal at the end of a number that the user inputs. - 1st 2nd 3rd 4th

    I have made some progress but its the terrible teens that have left me muddled as 11th is not the same as 21st or 31st etc... and the same goes for 111th is not the same as 121st.

    I thought i had it last night but of course i had forgotten about the larger numbers >100 and thats where my problem is. I had set a condition for 4 - 20 but cant keep doing the same for 104 - 120, 204 - 220 etc...

    My code is below and if anybody has any pointers or advice, that would be great! No looking for the answer but a push in the right direction :-)

    Code:
    #include <stdio.h>
    main (void)
    
    {
    	int num;
    
    	printf("Enter a Number: ");
    	scanf_s("%d", &num);
    
    if ((num % 10 == 1) && (num != 11))
    {
    	printf("%dst\n", num);
    }
    
    if ((num % 10 == 2) && (num != 12))
    {
    	printf("%dnd\n", num);
    }
    
    if ((num % 10 == 3) && (num !=13))
    {
    	printf ("%drd\n", num);
    }
    
    if ((num >= 4) && (num <=20))
    {
    	printf ("%dth\n", num);
    }
    
    	system ("pause");
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    OK, here's two ideas:


    #1)

    Make an array for suffixes. Initalize it with values that match the right suffix, to the right index to the array value, for any number. (You don't say how high you're going with these ordinals).

    Then you can print up the right suffix by using the array index:

    Say you want to print 12th. You'd use:

    Code:
    printf("%d%s", ordinal,suffix[ordinal]);
    Because suffix[12] = "th"

    #2)
    Make a separate function for this, and again using a single suffix[] array entry (3 char's probably), you'd have your logic to get the right suffix, in a single switch() statement:
    Code:
    //when the suffix ordinal is declared:
    suffix[2] = '\0'];   //set EOS char
    
    //in the getSuffix() function:
    
    void getSuffix(int ordinal) {
    
       switch (ordinal) {
          case < 2:  suffix[0] = 's'; suffix[1] = 't'; break;        //st
          case < 4;  suffix[0] = 'r'; suffix[1] = 'd'; break        //rd
          case < 20:    //th and the rest of your logic
    
       } 
    }
    
    
    //then:
    getSuffix(ordinal)
    
    printf("%d%s", ordinal, suffix);
    You'll need all your logic, but this is an easy way to shrink it down to something more manageable, concise, and clear.

    Whatever string you use, be sure it has the end of string char immediately after the last char. Otherwise, it's just a couple of char's, and not a string.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c program that accepts and executes commands?
    By Cimposter in forum C Programming
    Replies: 3
    Last Post: 09-30-2009, 02:58 PM
  2. Replies: 2
    Last Post: 09-16-2009, 06:00 AM
  3. Program that requests input of an integer number
    By theejuice in forum C Programming
    Replies: 6
    Last Post: 04-30-2008, 02:18 AM
  4. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  5. Fibonacci number program problem(assembly)
    By ok_good in forum Tech Board
    Replies: 7
    Last Post: 04-07-2006, 07:27 PM