how are you all
I want to write a program where when i enter the number
For example 10
must give the numbers of Singles
1, 3, 5, 7 and 9

please its important

4
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by harrierdh

how are you all
I want to write a program where when i enter the number
For example 10
must give the numbers of Singles
1, 3, 5, 7 and 9

please its important

What do you have so far? We're not going to help you if you don't show effort.

I'm guessing you mean you only want odd numbers? If that's the case use the modulas function built into javascript. Modulas is the remainder after a division. If you divide numbers by 2 you'll get this.

``````<script language="JavaScript">
answer = 7 % 2;
// odd numbers will always a modulas (answer = 5)
answer = 6 % 2
// even numbers will return 0 in answer (answer = 0)
</script>``````

this is my code:

``````<html>
<head>
<title> Odd and sum numbers > </title>
</head>
<body>
<script language="javaScript">;
var number;
number = window.prompt('Please enter any number','please enter your number here');
number = parseFloat(number);
var sum = 0;

document.write('The odd number are: ');

while ( number > 0){
if (number%2 == 1) {
document.write(number + ' ')
sum = sum + number;
number-- ;
}
else {
number--;
}
}

document.write('<br/>Their sum is ' + sum + '<br>');
document.write('thank you');
</script>
</body>
</html>``````

is there another way to slove it?

thank you

Edited by girl.java: n/a

I tried out the program you wrote yourself, and it returns the uneven numbers, what is there to solve?

If you would like the program to be re-useable without refreshing, calculates the even and uneven numbers and their sums you could try the following:

``````<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Even, uneven and sum of those numbers</title>
</head>
<body>
<script type="text/javascript">
function Numb() {

// Resetting numbers

document.getElementById("even").innerHTML = "";
document.getElementById("uneven").innerHTML = "";

// Retrieving the number

var number = parseInt(document.getElementById("number").value);

// Setting the sums of the even and uneven numbers 0

var unevensum = 0;
var evensum = 0;

// Loop lasts untill the number has reached 0

while ( number > 0){

// If the number is uneven:

if (number%2 == 1) {

// Updating the uneven number list

document.getElementById("uneven").innerHTML += number + ' ';

// Updating the unevensum with the number

unevensum = unevensum + number;

// Decreasing the number

number-- ;

// If the number is even

} else {

// Updating the even numbers list

document.getElementById("even").innerHTML += number + ' ';

// Updating the evensum with the number

evensum = evensum + number;

// Decreasing the number

number-- ;

}
}

// Updating the evensum and unevensum in the document (so their values are visible)

document.getElementById("evensum").innerHTML = evensum;
document.getElementById("unevensum").innerHTML = unevensum;

}
</script>
<form action="#" method="post">
<h3>Even, uneven and sum of those numbers</h3>
Please enter a number here: <input type="text" maxlength="5" id="number" size="4" />
<input type="button" value="Calculate the numbers!" onclick="Numb()" /><br /><br />
<span style="text-decoration:underline; font-weight:bold;">Results:</span><br /><br />
<span style="font-weight:bold;">Even numbers:</span> <span id="even"></span><br /><br />
<span style="font-weight:bold;">Sum of even numbers:</span> <span id="evensum"></span><br /><br />
<span style="font-weight:bold;">Uneven numbers:</span> <span id="uneven"></span><br /><br />
<span style="font-weight:bold;">Sum of uneven numbers:</span> <span id="unevensum"></span><br /><br />
<br />
</form>
</body>
</html>``````

~G

Edited by Graphix: n/a

I tried it to0 and it works. I did change this line though, because modulas won't always return 1.

if (number%2 == 1) {

changed to

if (number%2 != 0) {

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.