Thread: Can you count to ten?

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688

    Can you count to ten?

    Little fun game. Write a program segment that prints the
    numbers from 1 to 10, You can use any language you wish, and
    any legal syntax of that chosen language to do so. Let's see how many
    ways there are to do so in as many languages as possible.

    I'll Start

    Language Ada:

    Code:
    with Ada.Text_IO;
    use Ada.Text_IO;
    
    for I in 1 .. 10 loop
      Put_Line (I);
    end loop;
    Double Helix STL

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Bash
    Code:
    $ for ((i=1;i<=10;i++)); do echo $i ; done
    Awk
    Code:
    $ awk 'BEGIN { for ( i = 1 ; i <= 10 ; i++ ) { print i } }'
    Perl
    Code:
    $ perl -e 'print join("\n",(1..10)) . "\n";'
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    C#:
    Code:
            var ten = "1,2,3,4,5,6,7,8,9,10".Replace(",", "\n");
            Console.WriteLine(ten);

  4. #4
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    Python:

    Code:
    for i in range(1,11):
       print i

  5. #5
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Fortran, of course

    Code:
    print *, (/ (i, i = 1, 10) /)

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by gemera View Post
    Python:

    Code:
    for i in range(1,11):
       print i
    Or even more succinctly:

    Code:
    print range(1, 11)
    There was no stated requirement that each number be on its own line.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  7. #7
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    C:
    Code:
    int j = 0;
    while (true) {
        for (int i = 5; i < 35; i += 3) {
            char* s = malloc(i);
            sprintf(s, "%d", ((i - 5) / 3) + 1);
            printf("%s\n", s);
            j = (i / 2) * 6;
        }
        if (j > 10) break;
    }
    Last edited by Yarin; 10-04-2016 at 11:18 AM.

  8. #8
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    @Elkvis If list notation is acceptable it would even be possible to go raw with:

    Code:
    range(1,11)
    Now, that's snappy code.

  9. #9
    Guest
    Guest
    LOL @ Yarin

    Rust:
    Code:
    for i in 0..11 {
        print!("{} ", i);
    }

  10. #10
    Registered User
    Join Date
    Oct 2016
    Location
    Wales, UK
    Posts
    12
    BASIC


    FOR..NEXT loop

    Code:

    Code:
    FOR I = 1 TO 10
    PRINT I
    NEXT I END
    REPEAT..UNTIL loop

    Code:
    I=0
    REPEAT
    PRINT I I=I+1
    UNTIL I>=11 END

  11. #11
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Java:

    Code:
    public class Program {
        public static void main(string[] args) {
            for (int i =  1; i <= 10; ++i)
                System.out.print(" " + i);
        }
    }
    C - absolutely horrible way to do it:

    Code:
    int a = 1, b = 2, c = 3, d = 4,  e = 5, f = 6, g = 7,  h = 8, i = 9, j = 10;
    int *ptr1 = &a; 
    int *ptr2 = &b;
    int *ptr3 = &c;
    int *ptr4 = &d;
    int *ptr5 = &e;
    int *ptr6 = &f;
    int *ptr7 = &g;
    int *ptr8 = &h;
    int *ptr9 = &i;
    int *ptr10 = &j;
    
    printf(" %d", *ptr1);
    printf(" %d", *ptr2);
    printf(" %d", *ptr3);
    printf(" %d", *ptr4);
    printf(" %d", *ptr5);
    printf(" %d", *ptr6);
    printf(" %d", *ptr7);
    printf(" %d", *ptr8);
    printf(" %d", *ptr9);
    printf(" %d\n", *ptr10);
    Double Helix STL

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Code:
        char cs[] = "TWENTYFOUR";
        char *ptr = cs;
        while (*ptr++) {
            printf("%d\n", (int) (ptr - cs));
        }
    Last edited by whiteflags; 10-05-2016 at 04:38 AM.

  13. #13
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    You know, I thought I'd be clever and try to do this in Haskell. Then I got frustrated and just quit lol XD

  14. #14
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Quote Originally Posted by MutantJohn View Post
    You know, I thought I'd be clever and try to do this in Haskell. Then I got frustrated and just quit lol XD
    Haskell:
    Code:
    main = print([1..10])

  15. #15
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Quote Originally Posted by gemera View Post
    @elkvis if list notation is acceptable it would even be possible to go raw with:

    Code:
    range(1,11)
    now, that's snappy code.
    matlab:

    Code:
    1:10

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Uppercase/Lowercase/Word COunt/Char count
    By Charak in forum C Programming
    Replies: 7
    Last Post: 02-23-2011, 08:16 AM
  2. Even I Can Count Better Than That...
    By pianorain in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 12-02-2005, 07:17 PM
  3. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM
  4. Replies: 2
    Last Post: 05-05-2002, 01:38 PM

Tags for this Thread