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 3 of 6 FirstFirst 12345 ... LastLast
Results 51 to 75 of 133

Thread: 500 Ways to Print 1 to 10

  1. #51
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: 500 Ways to Print 1 to 10

    What's the fun if you arrive at the correct solution the first time

    46. Java

    public static void main(String[] args)
    {
         int[] numbers = new int[10];
         boolean foundAnswer = false;
         while(!foundAnswer)
         {
              for(int i = 0; i < numbers.length; i++)
              {
                   numbers[i] =(int)( Math.random() * 10 + 1);
              }
              sort(numbers);
              foundAnswer = true;
              for(int i = 1; i < numbers.length && foundAnswer; i++)
              {
                   if(numbers[i-1] == numbers[i])
                   {
                        foundAnswer = false;
                   }
              }
         }
         System.out.println(numbers);
    }
     
    public static void sort(int[] numbers)
    {
         for (int i = 0; i < numbers.length; i++)
         {
              int min = 0;
              for(int j = i; j < numbers.length; j++)
              {
                   if(numbers[min] > numbers[j])
                   {
                        min = j;
                   }
              }
              int temp = numbers[min];
              numbers[min] = numbers[i];
              numbers[i] = temp;
         }
    }

  2. #52
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: 500 Ways to Print 1 to 10

    47. Java

    More math...

    int value = 2;
    while(value <= 1024)
    {
         System.out.println(Math.log(value) / Math.log(2));
         value <<= 1;
    }

  3. #53
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: 500 Ways to Print 1 to 10

    48. Java
    import java.util.Calendar;
     
    public class Calendar1To10 {
     
      public static void main(String[] args) {
        Calendar start = Calendar.getInstance();
     
        Calendar now = Calendar.getInstance();
        while (now.get(Calendar.YEAR) < start.get(Calendar.YEAR) + 10) {
          try {
            Thread.sleep(1000 * 60 * 60 * 24);
            Calendar temp = Calendar.getInstance();
            if (temp.get(Calendar.YEAR) > now.get(Calendar.YEAR)) {
              now = temp;
              System.out.println(now.get(Calendar.YEAR) - start.get(Calendar.YEAR));
            }
          } catch (InterruptedException ex) {
            ex.printStackTrace();
          }
        }
      }
    }
    There still won't be 500 ways by the time this one completes executing

  4. #54
    Junior Member
    Join Date
    Nov 2010
    Posts
    3
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: 500 Ways to Print 1 to 10

    #49 Java

    public class Print1To10
    {
    	public static void main(String[] args)
    	{
    		System.out.println(1);
    		System.out.println(1<<1);
    		System.out.println(1^1<<1);
    		System.out.println(1<<1<<1);
    		System.out.println(1^1<<1<<1);
    		System.out.println(1<<1^1<<1<<1);
    		System.out.println(1^1<<1^1<<1<<1);
    		System.out.println(1<<1<<1<<1);
    		System.out.println(1^1<<1<<1<<1);
    		System.out.println(1<<1^1<<1<<1<<1);
    	}
    }

  5. The Following User Says Thank You to AdamB For This Useful Post:

    JavaPF (December 2nd, 2010)

  6. #55
    Member
    Join Date
    Nov 2010
    Location
    New Zealand
    Posts
    32
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Re: 500 Ways to Print 1 to 10

    #50. Logo


    You can copy-paste the source into the right editor screen over here.

  7. #56
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: 500 Ways to Print 1 to 10

    #51: There is always a chance that this takes forever to finish.

    import java.util.TreeSet;
    import java.util.Set;
     
    public class OneToTen {
     
    	public static void main(String... args){
    		Set<Integer> numberSet = new TreeSet<Integer>();
     
    		int times = 0;
     
    		while(numberSet.size() < 10){
     
    			times++;
     
    			Integer i = (int)(Math.random()*10 +1);
    			numberSet.add(i);
    		}
     
    		for(int i : numberSet){
    			System.out.println(i);
    		}
    		//System.out.println("Times: " + times);
    	}
    }

  8. #57
    Member
    Join Date
    Nov 2010
    Location
    New Zealand
    Posts
    32
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Re: 500 Ways to Print 1 to 10

    #52. 8086 assembler using interrupts

    org 100h
     
    ; set screen to text mode
    mov ax, 3
    int 10h
     
    ; init segment register
    mov     ax, 0b800h
    mov     ds, ax
     
    ; move characters' ASCII codes to video mem
    mov [02h], 31h
    mov [04h], 20h
    mov [06h], 32h
    mov [08h], 20h
    mov [0ah], 33h
    mov [0ch], 20h
    mov [0eh], 34h
    mov [10h], 20h
    mov [12h], 35h
    mov [14h], 20h
    mov [16h], 36h
    mov [18h], 20h
    mov [1ah], 37h
    mov [1ch], 20h
    mov [1eh], 38h
    mov [20h], 20h
    mov [22h], 39h
    mov [24h], 20h
    mov [26h], 31h
    mov [28h], 30h
     
    ; wait for keyb input
    mov ah, 0
    int 16h
    ret

  9. #58
    Junior Member
    Join Date
    Nov 2010
    Posts
    3
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: 500 Ways to Print 1 to 10

    #53 Using PHP's array_walk() and (very ugly) create_function():

    PHP Code:
    $nums = array(1,2,3,4,5,6,7,8,9,10);
    array_walk($numscreate_function('$x''echo "$x ";')); 

  10. #59
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: 500 Ways to Print 1 to 10

    #54. Arduino C:

    int counter = 1;
    void setup()
    {
        Serial.begin(38400);
    }
     
    void loop()
    {
        Serial.println(counter);
        ++counter;
        while(counter > 10)
        {}
    }

  11. #60
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: 500 Ways to Print 1 to 10

    #55
    public static void main(String[] args) {
        long mask = 0xfl;
        long value = 728121033505l;
        int shifter = 0;
        while(shifter < 40) {
            System.out.println((value & mask) >> shifter);
            mask = mask << 4;
            shifter += 4;
        }
    }
    It probably can be done with out shifter but my brain hurts.

  12. #61
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: 500 Ways to Print 1 to 10

    #56
    class OneToTen {
        int[] ones = {1,1,1,1,1,1,1,1,1,1};
     
        public static void main(String[] args) {
            new OneToTen();
        }
     
        OneToTen() {
            for(int index = 0; index < ones.length; index++) {
                System.out.println(sum(index));
            }
        }
     
        public int sum(int endValue) {
            int total = 0;
            for(int index = 0; index <= endValue; index++) {
                total += ones[index];
            }
            return total;
        }
    }

  13. #62
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: 500 Ways to Print 1 to 10

    #57
    import java.util.ArrayList;
    import java.util.Collections;
     
    class OneToTenStrings {
        ArrayList<String> words = new ArrayList<String>();
     
        public static void main(String[] args) {
            new OneToTenStrings();
        }
     
        OneToTenStrings() {
            words.add("jaberwocky");
            words.add("grazing");
            words.add("cog");
            words.add("inception");
            words.add("frozen");
            words.add("dark");
            words.add("earnt");
            words.add("a");
            words.add("halftime");
            words.add("be");
            Collections.sort(words);
            while(! words.isEmpty()) {
                System.out.println(words.remove(0).length());
            }
        }
    }

  14. #63
    Member
    Join Date
    Mar 2011
    Location
    Earth!
    Posts
    77
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: 500 Ways to Print 1 to 10

    Ok, I only read to page 4, so I hope no one posted this. Anyway, here is printing 1-10 using JavaScript from Java.
    public class Main
    {
    	public static void main(String[] args)
    	{
    		ScriptEngineManager man = new ScriptEngineManager();
    		ScriptEngine engine = man.getEngineByName( "rhino" );
    		try
    		{
    			engine.eval( "for (i = 1; i < 11; ++i){ print(i + \" \"); }" );
    		}
    		catch (ScriptException e)
    		{
    			System.err.println( "Muhahahaha, my attempt to look smart failed :P" );
    		}
    	}
    }

  15. #64
    Junior Member
    Join Date
    Feb 2011
    Posts
    7
    My Mood
    Bored
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: 500 Ways to Print 1 to 10

    Here it goes (using do....while)
    class One2Ten
              {
                     public void display()
                        {
                             int a = 1;
                                   do
                                           {
                                               System.out.println(a);a++;}
                                   while(a<11);
                        }
              }
    Last edited by Lokesh; March 7th, 2011 at 06:42 AM.

  16. #65
    Member OutputStream's Avatar
    Join Date
    Apr 2011
    Posts
    32
    My Mood
    Fine
    Thanks
    1
    Thanked 4 Times in 3 Posts

    Default Re: 500 Ways to Print 1 to 10

    #60: Beanshell
    print("1 2 3 4 5 6 7 8 9 10");

  17. #66
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default Re: 500 Ways to Print 1 to 10

    #61

    how about Batch? lol

    ECHO 12345678910

  18. #67
    Member
    Join Date
    Oct 2011
    Posts
    50
    My Mood
    Fine
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: 500 Ways to Print 1 to 10

    print(N) :- N < 11, write(N), N is N+1, print(N).
    ?-print(1).

    A PROLOG solution.
    Last edited by daniel.j2ee; December 13th, 2011 at 05:06 PM.

  19. #68
    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

    #63 Python
    i = 1
    while i <= 10:
      print(i)
    Think that's right, haven't done python in a while

  20. #69
    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

    #64 Java
     
    //package textField;
     
    import java.awt.BorderLayout;
     
    import javax.swing.JFrame;
    import javax.swing.JTextField;
     
    /**
     * Prints one through ten with a JTextField
     * @author TJStretch
     */
    public class OneToTenTextfield extends JFrame
    {
    	//Variables
    	/**
    	 * The JTextField that 1 through 10 will be printed on
    	 */
    	private JTextField txtField;
    	/**
    	 * Randomly generated serial version UID
    	 */
    	private static final long serialVersionUID = -5841505335372992754L;
     
    	//Constructors
    	public OneToTenTextfield() 
    	{
    		setLayout(new BorderLayout());
    		setSize(125, 100);
    		setOneToTen();
    		add(txtField, BorderLayout.CENTER);
    		setVisible(true);
    	}
            //Methods
    	/**
    	 * Initialize and set txtField to be ready for use.
    	 */
    	private void setOneToTen() 
    	{
    		if(txtField == null)
    			txtField = new JTextField();
    		String theStr = "";
    		for(int i = 1; i < 11; i++)
    			theStr += i+" ";
    		txtField.setText(theStr);
    		txtField.setEditable(false);
    	}
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) 
    	{
    		new OneToTenTextfield();
    	}
     
    }
    Last edited by Tjstretch; October 25th, 2011 at 03:34 PM.

  21. #70
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: 500 Ways to Print 1 to 10

    65 C#:

            delegate void count(count func, int i);
     
            public static void doCount(count b, int i)
            {
                Console.Out.WriteLine(i);
                if(i < 10)
                    b(b, i + 1);
            }
     
            static void Main(string[] args)
            {
                new count(doIt).Invoke(doIt, 1);
                Console.In.ReadLine();
            }

  22. #71
    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

    #66 (I think---are we supposed to assign the numbers or is it done by some referee?): "seq" command

    Oh, well...


    Is this allowed?

    For any system with seq utility (All UNIX systems that I have ever used had this. It is also in the GNU core utilities that are supplied with all Linux distros with which I am familiar.)

    From a command line (or, you could put it in a shell script):
    seq 1 10

    Cheers!

    Z
    Last edited by Zaphod_b; July 17th, 2012 at 02:58 PM.

  23. #72
    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

    #67: Busybox

    Similar to, but not exactly the same as the previous post (so I think it should be counted separately): For systems with busybox installed. (Many embedded Linux systems that don't have the full complement of GNU core utilities and even some desktop Linux systems have busybox.) On embedded systems, the seq command is probably a symbolic link to busybox, but it can be invoked explicitly as an argument to busybox, assuming its applet was compiled in, which it always is for my systems.)

    From a command line or you could put it in a shell script):
    busybox seq 1 10

    Cheers!

    Z
    Last edited by Zaphod_b; July 17th, 2012 at 02:58 PM.

  24. #73
    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

    #68: FORTRAN II

    From the days when FORTRAN was a powerful acronym (not the sissified proper noun Fortran) and versions were designated with Roman Numerals (like world wars), here it is in FORTRAN II. Line printers in common use and the IBM 026 key punch machines used in Computer Centers of the day did not have lower case alphabetical characters, so it looks like we were always SHOUTING.

    C FORTRAN II FROM 1967
    C
    C  FROM ZAPHOD_B IN 2012
    C
          WRITE(6,101) (I,I=1,10)
    101   FORMAT(1X,I10)
          END

    Can still be compiled with GNU g77. I just executed it on my Linux workstation. It still works the way it did way back when. Or least that's what my Earth logbook from the 20th century indicates.

    Back in the day. working on MainFrames, logical unit 6 was (almost) always the line printer, but could (usually) be changed by JCL (Job Control Language). Modern Fortran compiler installations on workstations (including GNU g77 on my Linux boxes) are, typically, set up to connect logical unit 6 to standard output (console window).

    The almost magical (mystical, at least) formidable power of the FORTRAN FORMAT I/O interpreter still gasts my flabber after all these decades.


    Cheers!

    Z
    Last edited by Zaphod_b; July 17th, 2012 at 05:44 PM.

  25. #74
    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

    #69: Fortran 77

    Fortran 77. Compile with GNU g77. (Also works with compilers for later versions, like Fortran 90, but I guess that wouldn't be allowed as a separate count)

    C  Fortran 77
    C
    C  Zaphod_b
    C
          Program PrintSequence
     
          do  i=1,10
            print *, i
          end do
          Stop
     
          End Program PrintSequence


    Cheers!

    Z
    Last edited by Zaphod_b; July 17th, 2012 at 02:53 PM.

  26. #75
    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

    #70: Fortran 90

    Compile with GNU gfortran

    !  Fortran 90 with a do loop
    !
    !  Zaphod_b
    !
            Program PrintSequence
     
            do  i=1,10
              write(*,'(1x,i0)') i
            end do
            Stop
     
            End Program PrintSequence

    Cheers!


    Z
    Last edited by Zaphod_b; July 17th, 2012 at 02:54 PM.

Page 3 of 6 FirstFirst 12345 ... 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