469,307 Members | 2,487 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,307 developers. It's quick & easy.

Positive Integer

Can anybody help me with writing this program?
I do not know what function to use?

1. Prompt the user for a positive integer.
2. Print the numbers from zero to that number in the web page, each number on a separate line.

Thanks
Oct 25 '07 #1
3 2556
acoder
16,027 Expert Mod 8TB
Can you show the code that you have so far.

For the first question, you would normally use the prompt() method.

Use a for loop for the second question: for loop.
Oct 26 '07 #2
Thanks:
Here is my code. I want to enter a positive number and print out from zero to that number.
[HTML]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<HTML>
<HEAD><SCRIPT language="JavaScript" type="text/javascript">
var i= prompt('Please enter a positive integer:', ' ');
</script>
<TITLE>CSL1</TITLE>
<META NAME="GENERATOR" CONTENT="MAX's HTML Beauty++ 2004">
</HEAD>

<BODY><h3>I should be able to enter 10000 and it should
print 1,2,3,....10000<P>


1.Prompt the user for a positive integer.<P>
2.Print the numbers from <U>zero to that number</U> in the web page, each number on a separate line.<P>
3.You do not have to do data validation. You can assume the user will actually give you a positive integer.<P>
You do not have to worry about over writing the page with your results.<P></h3>[/html]
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. for (i = 0; i <1000; i++)
  3. {
  4. document.write(i)
  5. document.write("<br />")
  6. }
  7. </script><P>
  8.  
  9.  
  10.  
  11. <SCRIPT language="JavaScript" type="text/javascript"> 
  12. > //var x= prompt('Please enter a positive integer:', ' '); 
  13. > </script> 
  14. > <TITLE>CSL</TITLE> 
  15. >
  16. > <SCRIPT language="JavaScript" type="text/javascript"> 
  17. > /*if (x ==0)  
  18. > {    
  19. > document.write(0+ ' <BR> ');  
  20. >   
  21. > } 
  22. > else if (x ==2)  
  23. > {    
  24. > document.write(0+ ' <BR> ' + 2 + '<BR> ');  
  25. >   
  26. > } 
  27. > else if (x ==4)  
  28. > {    
  29. > document.write(0+ ' <BR> ' + 2 + '<BR> ' + 4  + '<BR>');  
  30. >   
  31. > } 
  32. > else if (x ==6)  
  33. > {    
  34. > document.write(0+ ' <BR> ' + 2 + '<BR> ' + 4  + '<BR>' + 6 +
  35. > '<BR>');  
  36. >   
  37. > } 
  38. > else if (x ==8)  
  39. > {    
  40. > document.write(0+ ' <BR> ' + 2 + '<BR> ' + 4  + '<BR>' + 6 +
  41. > '<BR>' + 8 + '<BR>');  
  42. >   
  43. > } 
  44. > else if (x ==10)  
  45. > {    
  46. > document.write(0+ ' <BR> ' + 2 + '<BR> ' + 4  + '<BR>' + 6 +
  47. > '<BR>' + 8 + '<BR>' + 10 + '<BR>');  
  48. >   
  49. > } 
Expand|Select|Wrap|Line Numbers
  1. </script> 
  2. > </BODY> 
  3. > </HTML>
  4.  
  5.  
  6.  
  7. </BODY>
  8. </HTML>
Oct 30 '07 #3
acoder
16,027 Expert Mod 8TB
Use a unique variable name for the prompt, e.g. num.

Then in your for loop, replace the 1000 with this num variable so that it loops to the positive integer and not just 1000.
Oct 31 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

7 posts views Thread by NotGiven | last post: by
11 posts views Thread by gouqizi.lvcha | last post: by
7 posts views Thread by kepano | last post: by
63 posts views Thread by deepak | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.