0

If i want the output like:

1st Armstrong number = 0
2nd Armstrong number = 1
3rd Armstrong number = 153
.............................................
.............................................
20th Armstrong  number = ....

here my question is : if i have to print many armstrong numbers(1st to 20th) then is it the proper way to write printf one by one ? then i need to much time & code will be so long,how i minimize it?

please help....

This is my code which is able to find first 6 Armstrong Number..

  int main(){
   int a, b, c, num, i=0,j=0;
   printf("Printing all the armstrong numbers between 1 - 999");
   while(i<=999)
   {
    a= i/100;
    a= a*a*a;
    num= i%100;
    b= num/10;
    b= b*b*b;
    c= num%10;
    c=c*c*c;
    if(i==a+b+c)
      {
          j++;
          if(j==1) printf("\n1st");
          else if(j==2) printf("\n2nd");
          else if(j==3) printf("\n3rd");
          else if(j==4) printf("\n4th");
          else if(j==5) printf("\n5th");
          else if(j==6) printf("\n6th");

       printf(" Armstrong number= %d",i);
      }
    i++;
   } // end of while
return 0;
} // end of main
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
  • http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/chap04/arms.html – lurker Apr 17 '14 at 21:48
  • 2
    So what you want is basically a way to transform integers into ordinal numbers? I.E. 1 becomes "1st", 2 becomes "2nd", etc. – Rose Kunkel Apr 17 '14 at 21:50
  • There is actually a system behind it. It's not as if there are 20 possibilities for the first 20 ordinals. – Jongware Apr 17 '14 at 21:52
  • http://codegolf.stackexchange.com/questions/4707/outputting-ordinal-numbers-1st-2nd-3rd – Bill Lynch Apr 17 '14 at 21:54
  • This answer is a C# answer you could port to C. http://stackoverflow.com/questions/20156/is-there-an-easy-way-to-create-ordinals-in-c – Adam Apr 17 '14 at 21:54

6 Answers6

1

It's simple :

if(i==a+b+c)
  {
    j++;

    int key = j % 10;

    if(j == 11)
      key = 11;

    switch(key){ 

      case 1:
          printf("\n%dst Armstrong number= %d",j,i);
          break;
      case 2:
          printf("\n%dnd Armstrong number= %d",j,i);
          break;
      case 3:
          printf("\n%drd Armstrong number= %d",j,i);
          break; 
      case 11:   
      default:
          printf("\n%dth Armstrong number= %d",j,i);

  }
}
Ashraful Haque
  • 549
  • 2
  • 10
1

It appears that the rule for ordinal numbers is as follows:

x % 10 == 1: *st
x % 10 == 2: *nd
x % 10 == 3: *rd
Otherwise:   *th

Let's write this up in code:

const char * format = 
    (x % 10 == 1) ? "%dst armstrong number: %d\n" :
    (x % 10 == 2) ? "%dnd armstrong number: %d\n" :
    (x % 10 == 3) ? "%drd armstrong number: %d\n" :
                    "%dth armstrong number: %d\n" ;

printf(format, j, i);
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
1

Here's my solution. Try to keep your problems divided rather than trying to solve them all in one function. I hope this helps.

#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>

#define CUBE(n) (n*n*n)

char* getCountSuffix(uint16_t n) {
    n %= 100; // We don't care about the hundreds place
    if(n >= 10 && n <= 20) { // 10-19 always use "th" ("tenth", "eleventh", "twelveth", etc.)
        return "th";
    }
    n %= 10;
    switch(n) {
        case 1:
            return "st";
            break;
        case 2:
            return "nd"; // edit: was "nt"
            break;
        case 3:
            return "rd";
            break;
        default:
            return "th";
    }
}

bool isArmstrong(uint16_t n) {
    uint16_t hundreds = n / 100;
    uint16_t tens = (n % 100)/10;
    uint16_t ones = n % 10;

    return (CUBE(hundreds) + CUBE(tens) + CUBE(ones)) == n;
}

int main() {
    size_t i, count;

    for(i = 0, count = 1; i < 1000; i++) {
        if(isArmstrong(i)) {
            printf("%u%s. %u\r\n", count, getCountSuffix(count), i);
            count++;
        }
    }

    return 0;
}
SimpleJ
  • 13,812
  • 13
  • 53
  • 93
0

You find a pattern and make use of that pattern. Find a program that converts Roman Numerals to numbers, see how they are extracting a pattern and achieving it.

PS: You are mixing up the presentation and implementation. Implementation should compute the armstrong number and should pass it to another method for display, which can keep track of it and display it whatever way required. Displaying is not the problem you want to solve here and I would propose dont spend too much time for this 1st, 2nd, 3rd cases.

Arun Thirupathi
  • 351
  • 2
  • 11
0

Use printf's formatting capabilities. Replace all those else if printfs with:

printf("\n%dth", j);

The integer j will be substituted for %d. If you need to use (1st, 2nd, 3rd, 4rth) nd, th and st then have a few if statements to decide with one will be used, then printf using that one.

Straw1239
  • 589
  • 2
  • 8
0
        int sum = 0;
        int count;
        count = int.Parse(Console.ReadLine()); //Armstrong numbers from 0 to count
        Console.WriteLine();
        Console.Write(" Armstrong numbers from 0 to " + count + " are: "); 
        for (int c = 0; c <= count; c++)
        {
            sum = 0;
            for (int i = 1; i <= c; i *= 10)
            {
                if (c / i % 10 >= 1)
                {
                    int cube = (c / i % 10);
                    sum += cube * cube * cube;
                }
            }
            if (sum == c)
            {
                Console.Write(sum + " ");
            }
        }
Arthur
  • 1
  • 2