Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 5 of 6 FirstFirst ... 3456 LastLast
Results 101 to 125 of 133

Thread: 500 Ways to Print 1 to 10

  1. #101
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: 500 Ways to Print 1 to 10

    95: Java (Banner style)
    // A "banner-style" rendering of numbers 1-10
    //
    // Zaphod_b
    //
    public class z
    {
        public static void main(String [] args)
        {
            int charHeight = zero.length; // All chars have this height
     
            String [][] message = {
                one, space, two, space, three, space, four, space, five, space,
                six, space, seven, space, eight, space, nine, space,
                one, zero
            };
            for (int row = 0; row < charHeight; row++)
            {
                for (int j = 0; j < message.length; j++)
                {
                    System.out.print(message[j][row]);
                }
                System.out.println();
            }
     
     
     
        }// end of main
     
        /*
           Characters are from figlet/standard.flf font
           Part of figlet package from
               [url]ftp://ftp.figlet.org/pub/figlet/program/unix/[/url]
            Standard font by Glenn Chappell & Ian Chai 3/93 -- based on Frank's .sig
            Includes ISO Latin-1
            figlet release 2.1 -- 12 Aug 1994
            Modified for figlet 2.2 by John Cowan <cowan@ccil.org>
              to add Latin-{2,3,4,5} support (Unicode U+0100-017F).
     
            This simplified program has no kerning or smushing, but
            just prints the chars as defined.  The figlet program does it right!
                Zaphod_b
     
        */
        static String [] space = {
            " ",
            " ",
            " ",
            " ",
            " ",
            " "
        };
     
        static String [] zero = {
            "  ___  ",
            " / _ \\ ",
            "| | | |",
            "| |_| |",
            " \\___/ ",
            "       "
        };
        static String [] one = {
            " _ ",
            "/ |",
            "| |",
            "| |",
            "|_|",
            "   "
        };
     
        static String [] two = {
            " ____  ",
            "|___ \\ ",
            "  __) |",
            " / __/ ",
            "|_____|",
            "       "
        };
     
        static String [] three = {
            " _____ ",
            "|___ / ",
            "  |_ \\ ",
            " ___) |",
            "|____/ ",
            "       "
        };
     
        static String [] four = {
            " _  _   ",
            "| || |  ",
            "| || |_ ",
            "|__   _|",
            "   |_|  ",
            "        "
        };
     
        static String [] five = {
            " ____  ",
            "| ___| ",
            "|___ \\ ",
            " ___) |",
            "|____/ ",
            "       "
        };
     
        static String [] six = {
            "  __   ",
            " / /_  ",
            "| '_ \\ ",
            "| (_) |",
            " \\___/ ",
            "       "
        };
     
        static String [] seven = {
            " _____ ",
            "|___  |",
            "   / / ",
            "  / /  ",
            " /_/   ",
            "       "
        };
     
        static String [] eight = {
            "  ___  ",
            " ( _ ) ",
            " / _ \\ ",
            "| (_) |",
            " \\___/ ",
            "       "
        };
     
        static String [] nine = {
            "  ___  ",
            " / _ \\ ",
            "| (_) |",
            " \\__, |",
            "   /_/ ",
            "       "
        };
     
    } // End of class definition

    Output:
     _   ____    _____   _  _     ____     __     _____    ___     ___    _   ___  
    / | |___ \  |___ /  | || |   | ___|   / /_   |___  |  ( _ )   / _ \  / | / _ \ 
    | |   __) |   |_ \  | || |_  |___ \  | '_ \     / /   / _ \  | (_) | | || | | |
    | |  / __/   ___) | |__   _|  ___) | | (_) |   / /   | (_) |  \__, | | || |_| |
    |_| |_____| |____/     |_|   |____/   \___/   /_/     \___/     /_/  |_| \___/



    Cheers!

    Z
    Last edited by Zaphod_b; August 19th, 2012 at 12:22 AM.

  2. #102
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: 500 Ways to Print 1 to 10

    96: Java (Roman Numerals)
    //
    // Once our brains are nudged outside the box of printing
    // decimal digit representations of the numbers 1-10, well,
    // it can actually be more fun.
    //
    // For example: How about Roman Numerals?
    //
    // Zaphod_b
    //
    public class z
    {
        public static void main(String [] args)
        {
            for (int i = 1; i <= 10; i++)
            {
                System.out.printf("%s", decimalToRoman(i));
                if (i < 10)
                {
                    System.out.print(" ");
                }
                else
                {
                    System.out.println();
                }
            }
        }
     
        // Convert number 1-10 to Roman Numeral String
        //
        // This is the tail-end abstraction from a general-purpose
        // routine to convert numbers 1-9999.
        //
        static String decimalToRoman(int n)
        {
            String str = "";
            // A "real" application might throw an exception here,
            // but...
            if (n < 1 || n > 10)
            {
                System.out.printf("Error: In decimalToRoman, argument = %d, must be 1 through 10.\n", n);
                return str;
            }
     
            // For a full-blown conversion program, all pieces of the number
            // that are greater than 10 would have already been put into
            // the string, and this part would be entered with a value of
            // n from 1 through 9.
            // If this appears kind of brute-forcey, well that's the way we did
            // it back in the day...
            //
            if (n == 10)
            {
                return "X";
            }
     
            // Here's the units piece of the pie
            if (n == 9)
            {
                str += "IX";
            }
            else if (n >= 5)
            {
                str += "V";
                for (int i = 0; i < n-5; i++)
                {
                    str += "I";
                }
            }
            else if (n == 4)
            {
                str += "IV";
            }
            else
            {
                for (int i = 0; i < n; i++)
                {
                    str += "I";
                }
            }
            return str;
        }
    }

    Output:

    I II III IV V VI VII VIII IX X


    Cheers!

    Z
    Last edited by Zaphod_b; July 21st, 2012 at 01:01 PM.

  3. #103
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: 500 Ways to Print 1 to 10

    97: Java
    //Print 1 2 3 4 5 6 7 8 9 10
    //solution # 97
    //jps
    public class PrintOneToTen_097 {
     
    	public static void main(String[] args) {
    		System.out.print("1 ");
    		System.out.print("2 ");
    		System.out.print("3 ");
    		System.out.print("4 ");
    		System.out.print("5 ");
    		System.out.print("6 ");
    		System.out.print("7 ");
    		System.out.print("8 ");
    		System.out.print("9 ");
    		System.out.print("10");
    	}
    }


    Output:
    1 2 3 4 5 6 7 8 9 10

  4. #104
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: 500 Ways to Print 1 to 10

    98: Java Bi-quinary
    /*
        Write digits 1-10 in bi-quinary format similar to the
        way digits were displayed on the IBM 650 Magnetic
        Drum Calculator, as seen in the late 1950s (and
        maybe even a little later).  See the comment at
        the bottom for a little more information.
     
        This was a decimal arithmetic machine, and BCD register contents
        were displayed on the console panel in bi-quinary format.
     
        Each digit: Two lamps on the top row, one of which would be "on."
        The left one was "on" for numbers 0-4, the right one was "on" for
        numbers 5-9.
     
        Below the two lamps for each digit was a column of five lamps,
        one of which would be "on."  The bottom was "on" for number 0
        or 5, The next was "on" for number 1 or 6, etc.
     
        I will indicate a lamp that is off by a 'o' character.  A 'x'
        indicates a lamp that is illuminated.
     
        Actual display on the panel grouped the digits according to fields
        of the machine instruction codes.
     
        I'll just use a column of '|' characters to separate the numbers
        (like using commas to separate the numbers that were written as
        words a couple of posts ago).
     
        I could make a font of digits (kind of like the Banner program),
        but, instead of that, I'll just create the digit strings on
        the fly, and print them in a way similar to the method of
        the Banner program.
     
     
        Zaphod_b
     
    */
     
    import java.util.ArrayList;
     
    public class z
    {
        public static void main(String [] args)
        {
            // An ArrayList of arrays of strings. Each element of the
            // ArrayList is a character that will be printed.
            //
            ArrayList<String []> message = new ArrayList<String []>();
     
     
            // Create the message
            message.add(spacer);
            for (int i = 1; i <= 10; i++)
            {
                message.add(makeDigits(i));
                message.add(spacer);
            }
     
            // Print it out
            for (int row = 0; row < charHeight; row++)
            {
                for (int j = 0; j < message.size(); j++)
                {
                    System.out.print(message.get(j)[row]);
                }
                System.out.println();
            }
        }
     
        static int charHeight = 6;
        static String biqTopRowlt5 = "x o ";
        static String biqTopRowge5 = "o x ";
        static String biqColumnoff = " o  ";
        static String biqColumnon  = " x  ";
        // Spacer between the numbers
        static String spr = "| ";
        static String [] spacer = {spr, spr, spr, spr, spr, spr};
     
        // Space between digits of a two-digit number
        static String sp = "  ";
        static String [] space  = {sp, sp, sp, sp, sp, sp};
     
        //
        // Make String array to display a one- or two-decimal digit
        // number.
        // This only works for numbers up to 99.  I didn't bother
        // to do any checking or exception-throwing or anything like
        // that.  I mean, really!
        //
        static String [] makeDigits(int n)
        {
            if (n <= 9)
            {
                return makeDigit(n);
            }
            else
            {
                int tens = n / 10;
                int units = n % 10;
                // Get separate string arrays for the digits
                String [] tensStr  = makeDigit(tens);
                String [] unitsStr = makeDigit(units);
     
                // Now put them together with a space in between
                String [] ret = new String[charHeight];
                for (int i = 0; i < charHeight; i++)
                {
                    ret[i] = tensStr[i] + space[i] + unitsStr[i];
                }
                return ret;
            }
        }
        //
        // Make a string array for digit value 0 through 9
        // I didn't bother to do any checking, etc., etc., etc...
        //
        static String [] makeDigit(int n)
        {
            String [] digit = new String[charHeight];
            if (n < 5)
            {
                digit[0] = biqTopRowlt5;
            }
            else
            {
                digit[0] = biqTopRowge5;
            }
            int unitsCol = 5-(n%5);
            for (int i = 1; i < charHeight; i++)
            {
                if (i == unitsCol)
                {
                    digit[i] = biqColumnon;
                }
                else
                {
                    digit[i] = biqColumnoff;
                }
            }
            return digit;
        }
    }
    /*
     
       A history lesson: The IBM 650 was, I think, the first commercial computer
       ever marketed that was not advertised as bigger, faster, and with more
       computing ability than predecessors.  It only cost about $300,000
       (compared with the million-dollar-plus UNIVAC I).  The CPU, Power Unit,
       and Card Reader/Punch together only weighed about 6000 pounds,
       and the whole installation could be put into a single room, requiring
       only about 45 square feet of floor space! (Well, that's what the IBM
       marketing guys said, but you actually needed a little more.)
     
       Note that it was marketed as a "Magnetic Drum Calculator" rather than
       an "Electronic Computer."
     
       I loved hearing that magnetic drum as it spun up to 12,500 RPM when
       it was first turned on.  Started out at a low hum and went up to
       ultrasonic.  (They didn't have "bring your dog to work" days back
       then, but if they would have, it might have been interesting...)
     
       Most of all, however, I just loved those blinking bi-quinary lights!
     
    */

    Output

    | x o | x o | x o | x o | o x | o x | o x | o x | o x | x o   x o | 
    |  o  |  o  |  o  |  x  |  o  |  o  |  o  |  o  |  x  |  o     o  | 
    |  o  |  o  |  x  |  o  |  o  |  o  |  o  |  x  |  o  |  o     o  | 
    |  o  |  x  |  o  |  o  |  o  |  o  |  x  |  o  |  o  |  o     o  | 
    |  x  |  o  |  o  |  o  |  o  |  x  |  o  |  o  |  o  |  x     o  | 
    |  o  |  o  |  o  |  o  |  x  |  o  |  o  |  o  |  o  |  o     x  |


    Cheers!


    Z
    Last edited by Zaphod_b; July 21st, 2012 at 01:06 PM.

  5. #105
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: 500 Ways to Print 1 to 10

    99: Java (Exactly like #55 except for the parts that are different.)

    public class Z
    {
        public static void main(String [] args)
        {
            for (long value = 0xA987654321L; value != 0; value >>= 4)
            {
                System.out.print((value & 0xF) + " ");
            }
            System.out.println();
        }
    }

    Output

    1 2 3 4 5 6 7 8 9 10


    Cheers!

    Z
    Last edited by Zaphod_b; August 16th, 2012 at 11:36 PM.

  6. #106
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: 500 Ways to Print 1 to 10

    100: Java (To the nines.)

    /*
       To the nines...
     
       Zaphod_b
    */
    public class Z
    {
        public static void main(String [] args)
        {
            int x = 1111111109;
            printDigits(x/9, 9);
            System.out.printf("%d\n", (x/9)%(9+1) + 1);
        }
     
        static void printDigits(int x, int n)
        {
            for (int mask = (int)(Math.pow(10,n-1)+0.5); mask != 0; mask /= 10)
            {
                System.out.printf("%d ", x/mask);
                x %= mask;
            }
     
        }
    }

    Output

    1 2 3 4 5 6 7 8 9 10


    Cheers!

    Z
    Last edited by Zaphod_b; August 16th, 2012 at 11:36 PM.

  7. #107
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: 500 Ways to Print 1 to 10

    Java to Brainf***k (Credits to helloworld and JavaPF for showing me the path to hell)
    i dont know if this is a valid entry

    101: Java to Brainf***k (Generates simple BF codes) run the generated output in a BF interpreter
    public class BFMaker {
     
        public static void main(String[] args) {
     
            String[] seq = {"One[1]", "Two[2]", "Three[3]", "Four[4]", "Five[5]", "Six[6]", "Seven[7]", "Eight[8]", "Nine[9]", "Ten[10]"};
     
            StringBuilder brnFck = new StringBuilder();
            String ptrLooper = "[>++<-]>";
     
            char incrPtr = '+';
            char print = '.';
     
            for (int j = 0; j < seq.length; j++) {
     
                String str = seq[j];
     
                for (int x = 0; x < str.length(); x++) {
     
     
                    int ascii = str.charAt(x);
                    int rmdr = 0;
                    int half = 0;
     
                    if (ascii % 2 == 1) {
     
                        rmdr = 1;
                    }
     
                    half = ascii / 2;
     
                    for (int i = 1; i <= half; i++) {
     
                        brnFck.append(incrPtr);
                    }
     
                    brnFck.append(ptrLooper);
     
                    if (rmdr == 1) {
     
                        brnFck.append(incrPtr);
                    }
     
                    brnFck.append(print).append('\n').append('>');
                }
     
     
                // create a brain f***ing white spaces
                for (int s = 1; s <= (int) ' '; s++) {
     
                    brnFck.append("+");
                }
     
                brnFck.append(".");
                brnFck.append("\n").append(">");
            }
     
            brnFck.deleteCharAt(brnFck.length() - 1);
            System.out.println(brnFck);
     
        }
    }

    Output in BrainF:
    One[1] Two[2] Three[3] Four[4] Five[5] Six[6] Seven[7] Eight[8] Nine[9] Ten[10]

  8. The Following User Says Thank You to chronoz13 For This Useful Post:

    curmudgeon (November 25th, 2012)

  9. #108
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: 500 Ways to Print 1 to 10

    102: MIX (No loop)
    *
    * print1_10.mixal
    *
    * Just prints out literal message from memory.
    *
    *
    * Assemble from command line with mixasm print1_10
    * Execute  from command line with mixvm -r print1_10
    *
    *
    * Zaphod_b
    *
    TERM    EQU     19
            ORIG    3000
    START   OUT     MSG(TERM)
            HLT
    MSG     ALF     "1 2 3"
            ALF     " 4 5 "
            ALF     "6 7 8"
            ALF     " 9 10"
            END     START

    Output:

    Program loaded. Start address: 3000
    Running ...
    1 2 3 4 5 6 7 8 9 10
    ... done


    Tested with GNU mdk-1.2.6

    Cheers!

    Z
    Last edited by Zaphod_b; September 7th, 2012 at 06:52 PM.

  10. #109
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: 500 Ways to Print 1 to 10

    103: MIX (With Loop)

    *
    * print1_10_loop.mixal
    *
    * Assemble from command line with mixasm print1_10_loop
    * Execute  from command line with mixvm -r print1_10_loop
    *
    * Zaphod_b
    *
    TERM    EQU     19      The console (stdout)
    NUM     EQU     100     Storage for number
    OUTBUF  EQU     105     Print buffer
     
            ORIG    3000
    START   IOC     0(TERM)         Connect printer to console
            LD1     =-10=           Loop counter 
    2H      LDA     NUM             The loop starts here
            INCA    1
            STA     NUM
            CMPA    =10=            Number less than 10?
            JL      3F              Yes: Go to single-digit branch
            CHAR
            STX     OUTBUF(1:2)     Not less than 10; store two digits
            JMP     4F              Go print it
    3H      CHAR
            STX     OUTBUF(1:1)     The single-digit branch
    4H      OUT     OUTBUF(TERM)    Print it
            INC1    1               Increment loop counter
            J1N     2B              If not zero, back for more
            HLT
            END     START

    Output:

    Program loaded. Start address: 3000
    Running ...
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    ... done


    Tested with GNU mdk-1.2.6

    Cheers!

    Z

  11. #110
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: 500 Ways to Print 1 to 10

    104: Groovy
    //
    // Simple Groovy loop to print 1 through 10
    //
    //  Groovy is exactly like Java except for the parts that are different.
    //
    // Zaphod_b
    //
    for (int num = 1; num <= 10; num++)
        print num + " "
    println ""

    Output:

    1 2 3 4 5 6 7 8 9 10


    Tested with Groovy Version 1.8.8 JVM 1.6.0_22


    Cheers!

    Z
    Last edited by Zaphod_b; October 18th, 2012 at 02:41 PM.

  12. #111
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: 500 Ways to Print 1 to 10

    105: Groovy
    //
    // Simple Groovy program to print 1 through 10 from an array
    //
    // Zaphod_b
    //
    int [] nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    for (int num : nums)
        print num + " "
    println ""

    Output:

    1 2 3 4 5 6 7 8 9 10

    Tested with Groovy Version 1.8.8 JVM 1.6.0_22


    Cheers!

    Z

  13. #112
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: 500 Ways to Print 1 to 10

    106: Groovy

    //
    // "Real Groovy" loop to print 1 through 10
    //
    // "Real Groovy" doesn't contain anything that
    // would work as Java.
    //
    // Zaphod_b
    //
    for (num in 1..10)
        print num + " "
    println ""

    Output

    1 2 3 4 5 6 7 8 9 10

    Tested with Groovy Version 1.8.8 JVM 1.6.0_22


    Cheers!

    Z

  14. #113
    Junior Member
    Join Date
    Oct 2012
    Posts
    3
    My Mood
    Fine
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 500 Ways to Print 1 to 10

    import java.util.Enumeration;
    import java.util.Vector;
    public class whleloop {

    public static void main(String[] args) {
    Enumeration no;
    Vector number = new Vector();
    for(int i=1 ; i<=10 ; i++){
    number.add(i);
    }
    no = number.elements();
    while(no.hasMoreElements())
    System.out.println(no.nextElement());
    }
    }

  15. #114
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: 500 Ways to Print 1 to 10

    108: Same as previous Enumeration example, but with a for(){} loop

    import java.util.*;
     
    public class ForLoopEnum {
     
        public static void main(String [] args) {
     
            //Use concrete type to avoid 'unchecked operations' compiler warnings
            Vector<Integer> vecInt = new Vector<Integer>();
     
            for(int i = 0; i < 10; i++) {
                vecInt.add(i+1); // Autobox int to Integer
            }
     
            for (Enumeration enu = vecInt.elements(); enu.hasMoreElements(); /**/) {
                System.out.print(" " + enu.nextElement());
            }
            System.out.println();
        } // End main()
     
    } // End class definition

    Output:

    1 2 3 4 5 6 7 8 9 10


    Cheers!

    Z
    Last edited by Zaphod_b; October 20th, 2012 at 03:34 PM.

  16. #115
    Junior Member
    Join Date
    Oct 2012
    Posts
    6
    My Mood
    Dead
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 500 Ways to Print 1 to 10

    public class GiveMe10 {
    	enum GiveTen {one, two, three, four, five, six, seven, eight, nine, ten};
    	public static void main(String[] args) {
    		GiveTen[] num = GiveTen.values();
    		for (int i = 0; i < 10; ++i ) {
    			System.out.print(num[i].ordinal() + 1);
    		}
    	}
    }

    output:
    12345678910

  17. #116
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: 500 Ways to Print 1 to 10

    [size=large]#110 ComputerCraft[/size]
    [size=tiny]Please don't kill me[/size]

    print("1 2 3 4 5 6 7 8 9 10")

  18. #117
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: 500 Ways to Print 1 to 10

    #110 ComputerCraft
    Please don't kill me

    print("1 2 3 4 5 6 7 8 9 10")

  19. #118
    Junior Member
    Join Date
    Nov 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 500 Ways to Print 1 to 10

    Perl: print(1..10);
    Pyhton: print range(1, 11)

    --- Update ---

    Perl: print(1..10);
    Pyhton: print range(1, 11)

    Java:
    public class OneToTen
    {
    public static void main(String[] args)
    {
    int group1 = 54321;
    int n1 = group1 % 10;
    group1 = group1 / 10;
    int n2 = group1 % 10;
    group1 = group1 / 10;
    int n3 = group1 % 10;
    group1 = group1 / 10;
    int n4 = group1 % 10;
    group1 = group1 / 10;
    int n5 = group1 % 10;

    int group2 = 109876;
    int n6 = group2 % 10;
    group2 = group2 / 10;
    int n7 = group2 % 10;
    group2 = group2 / 10;
    int n8 = group2 % 10;
    group2 = group2 / 10;
    int n9 = group2 % 10;
    int n10 = group2 / 10;
    System.out.printf("%d %d %d %d %d %d %d %d %d %d", n1, n2, n3, n4, n5, n6, n7, n8, n9, n10);


    }
    }
    Output java: 1 2 3 4 5 6 7 8 9 10

  20. #119
    Member
    Join Date
    Feb 2013
    Location
    earth
    Posts
    88
    Thanks
    12
    Thanked 9 Times in 9 Posts

    Default Re: 500 Ways to Print 1 to 10

    .

  21. #120
    Member
    Join Date
    Jan 2013
    Posts
    34
    My Mood
    Busy
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default Re: 500 Ways to Print 1 to 10

    #23

    String[] a = {"i", "ii", "iii", "iiii", "xxxxx", "gggggg", "zzzzzzz", "imimimim", "NEIN_NEIN", "oooxxxooox"};
    for(String m : a){
      System.out.print(getLength(m)+" ");
    }
    int getLength(String x){
      int r = 0;
      for(char i : x.toCharArray()){
        r++;
      }
      return r;
    }

  22. #121
    Banned
    Join Date
    Jul 2013
    Posts
    49
    Thanks
    1
    Thanked 5 Times in 4 Posts

    Default Re: 500 Ways to Print 1 to 10

    haha...this is fun...hope I am not repeating.

    //    #include <stdio.h>
        #define MIN_RANGE 1
        #define MAX_RANGE 10
        int main (void)
        {
        int i;
        for (i = MIN_RANGE; i <= MAX_RANGE; i++)
        printf("%i\n", i);
        return 0;
        }


    IN C

    --- Update ---



     
    //   for x in range(10):
        print x + 1

  23. #122
    Junior Member
    Join Date
    Aug 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 500 Ways to Print 1 to 10

    #117 : Caml Light
    (probably works for OCaml, as well)

    let print_nbrs = function i -> print_int(i); print_char(` `);;
    for i = 1 to 10 do
    print_nbrs(i); done;;


    --- Update ---

    #118 (is this 118 ?)
    Same, but terminally recursive, and currificated (if I remember correctly, unless I'm mistaken).


    let rec print_nbrs = fun
    0 x -> print_int(x);print_char(` `);
    |a x -> print_nbrs (a-1) (x+1);;
     
    for i = 1 to 10 do
    print_nbrs i 0; done;;

  24. #123
    Junior Member
    Join Date
    Oct 2013
    Location
    Rio de Janeiro, RJ
    Posts
    2
    My Mood
    Inspired
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 500 Ways to Print 1 to 10

    #119, I guess. (Hopefully no one used this before)

    How about this?

    public class Counter {
    	public static void main(String args[]){
    		int counter = 1;
     
    		while (counter <=10){
    			System.out.println(counter);
    			counter++;
    		}
    	}
    }

  25. #124
    Member
    Join Date
    Sep 2013
    Posts
    70
    Thanks
    1
    Thanked 13 Times in 13 Posts

    Default Re: 500 Ways to Print 1 to 10

    If this is still going how about any of these? Hopefully one of them is new to the list.
        for (int i = 10; i < 20; i++){
          System.out.print(i % 10 + 1 + " ");
        }
        System.out.println();
        for (int i = -1; i >= -10; i--){
          System.out.print(i * (-1) + " ");
        }
     
        String number = "0123456789";
        System.out.printf("%s %s %s %s %s %s %s %s %s %s", number.charAt(1), number.charAt(2), number.charAt(3),
                          number.charAt(4), number.charAt(5), number.charAt(6), number.charAt(7),
                          number.charAt(8), number.charAt(9), number.charAt(1) + number.charAt(0) + "");

  26. #125
    Junior Member Bethany Ferrell's Avatar
    Join Date
    Dec 2013
    Posts
    8
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: 500 Ways to Print 1 to 10

    Going wayyy back to high school and DARPANET here:

    ⍞←⍳10 
    ⍝ or just 
    ⍳10 
    ⍝ APL's terseness is legendary. Some call it obscurity.

    The character that looks like a lower-case "i" without the dot is a Greek iota, which generates a vector from 1 to its argument; the upside-down U with a small circle inside is "lamppost," the APL comment character; the quad-quote is a sink that feeds program output to the terminal, and the left-pointing arrow is the assignment operator.

Page 5 of 6 FirstFirst ... 3456 LastLast

Similar Threads

  1. print code to browser
    By bookface in forum Java Theory & Questions
    Replies: 4
    Last Post: April 21st, 2010, 01:09 AM
  2. print space
    By brainTuner in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 1st, 2010, 06:09 PM
  3. How to print results to screen
    By NinjaLink in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 19th, 2010, 01:46 PM
  4. Can someone please tell me why my code doesn't print out anything?
    By deeerek in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 6th, 2010, 08:35 AM
  5. print hex decimal value
    By ran830421 in forum Java Theory & Questions
    Replies: 1
    Last Post: November 25th, 2009, 07:23 PM