Loop to print odd numbers not printing as many as it should
I'm writing a script that prints out a user-provided amount of odd numbers starting from a user-provided number.
So for an example if you would enter that you want to print out 5 numbers starting from the number 3, it would output 3, 5, 7, 9, and 11.
I'm currently trying to use the following code:
echo "Enter how many numbers you want to print"
read n
echo "Enter the first number"
read a
for ((a; a < n; a++)); do
((b = a % 2))
if [ $b -ne 0 ]; then
echo "$a"
fi
done
However, with n=5; a=3
, the output is not the expected 3
5
7
9
11
but is instead only 3
.
bash
add a comment |
I'm writing a script that prints out a user-provided amount of odd numbers starting from a user-provided number.
So for an example if you would enter that you want to print out 5 numbers starting from the number 3, it would output 3, 5, 7, 9, and 11.
I'm currently trying to use the following code:
echo "Enter how many numbers you want to print"
read n
echo "Enter the first number"
read a
for ((a; a < n; a++)); do
((b = a % 2))
if [ $b -ne 0 ]; then
echo "$a"
fi
done
However, with n=5; a=3
, the output is not the expected 3
5
7
9
11
but is instead only 3
.
bash
...so, what's the part that doesn't actually work? See Minimal, Complete, and Verifiable example guidelines -- a question should have a specific problem (not the larger problem your script tries to solve, but the problem with your script), and the shortest code that lets others see that problem.
– Charles Duffy
Nov 22 at 17:31
BTW, a good place to start is logging withset -x
. See your code running at ideone.com/hgCZDP, with a log in the "stderr" section.
– Charles Duffy
Nov 22 at 17:35
2
...in the current case, what you have isn't a bash-the-language bug, but a thinking-about-your-problem bug: You're comparinga < n
, buta
doesn't start at 0, it starts at the value the user entered, so it doesn't printn
numbers; instead, it just prints odd numbers greater than or equal toa
and less thann
. For the examplesn=5
anda=3
, the only odd number that meets that criteria is3
, so that's all it prints.
– Charles Duffy
Nov 22 at 17:36
add a comment |
I'm writing a script that prints out a user-provided amount of odd numbers starting from a user-provided number.
So for an example if you would enter that you want to print out 5 numbers starting from the number 3, it would output 3, 5, 7, 9, and 11.
I'm currently trying to use the following code:
echo "Enter how many numbers you want to print"
read n
echo "Enter the first number"
read a
for ((a; a < n; a++)); do
((b = a % 2))
if [ $b -ne 0 ]; then
echo "$a"
fi
done
However, with n=5; a=3
, the output is not the expected 3
5
7
9
11
but is instead only 3
.
bash
I'm writing a script that prints out a user-provided amount of odd numbers starting from a user-provided number.
So for an example if you would enter that you want to print out 5 numbers starting from the number 3, it would output 3, 5, 7, 9, and 11.
I'm currently trying to use the following code:
echo "Enter how many numbers you want to print"
read n
echo "Enter the first number"
read a
for ((a; a < n; a++)); do
((b = a % 2))
if [ $b -ne 0 ]; then
echo "$a"
fi
done
However, with n=5; a=3
, the output is not the expected 3
5
7
9
11
but is instead only 3
.
bash
bash
edited Nov 22 at 17:41
Charles Duffy
172k25193249
172k25193249
asked Nov 22 at 17:26
Frostbytee
11
11
...so, what's the part that doesn't actually work? See Minimal, Complete, and Verifiable example guidelines -- a question should have a specific problem (not the larger problem your script tries to solve, but the problem with your script), and the shortest code that lets others see that problem.
– Charles Duffy
Nov 22 at 17:31
BTW, a good place to start is logging withset -x
. See your code running at ideone.com/hgCZDP, with a log in the "stderr" section.
– Charles Duffy
Nov 22 at 17:35
2
...in the current case, what you have isn't a bash-the-language bug, but a thinking-about-your-problem bug: You're comparinga < n
, buta
doesn't start at 0, it starts at the value the user entered, so it doesn't printn
numbers; instead, it just prints odd numbers greater than or equal toa
and less thann
. For the examplesn=5
anda=3
, the only odd number that meets that criteria is3
, so that's all it prints.
– Charles Duffy
Nov 22 at 17:36
add a comment |
...so, what's the part that doesn't actually work? See Minimal, Complete, and Verifiable example guidelines -- a question should have a specific problem (not the larger problem your script tries to solve, but the problem with your script), and the shortest code that lets others see that problem.
– Charles Duffy
Nov 22 at 17:31
BTW, a good place to start is logging withset -x
. See your code running at ideone.com/hgCZDP, with a log in the "stderr" section.
– Charles Duffy
Nov 22 at 17:35
2
...in the current case, what you have isn't a bash-the-language bug, but a thinking-about-your-problem bug: You're comparinga < n
, buta
doesn't start at 0, it starts at the value the user entered, so it doesn't printn
numbers; instead, it just prints odd numbers greater than or equal toa
and less thann
. For the examplesn=5
anda=3
, the only odd number that meets that criteria is3
, so that's all it prints.
– Charles Duffy
Nov 22 at 17:36
...so, what's the part that doesn't actually work? See Minimal, Complete, and Verifiable example guidelines -- a question should have a specific problem (not the larger problem your script tries to solve, but the problem with your script), and the shortest code that lets others see that problem.
– Charles Duffy
Nov 22 at 17:31
...so, what's the part that doesn't actually work? See Minimal, Complete, and Verifiable example guidelines -- a question should have a specific problem (not the larger problem your script tries to solve, but the problem with your script), and the shortest code that lets others see that problem.
– Charles Duffy
Nov 22 at 17:31
BTW, a good place to start is logging with
set -x
. See your code running at ideone.com/hgCZDP, with a log in the "stderr" section.– Charles Duffy
Nov 22 at 17:35
BTW, a good place to start is logging with
set -x
. See your code running at ideone.com/hgCZDP, with a log in the "stderr" section.– Charles Duffy
Nov 22 at 17:35
2
2
...in the current case, what you have isn't a bash-the-language bug, but a thinking-about-your-problem bug: You're comparing
a < n
, but a
doesn't start at 0, it starts at the value the user entered, so it doesn't print n
numbers; instead, it just prints odd numbers greater than or equal to a
and less than n
. For the examples n=5
and a=3
, the only odd number that meets that criteria is 3
, so that's all it prints.– Charles Duffy
Nov 22 at 17:36
...in the current case, what you have isn't a bash-the-language bug, but a thinking-about-your-problem bug: You're comparing
a < n
, but a
doesn't start at 0, it starts at the value the user entered, so it doesn't print n
numbers; instead, it just prints odd numbers greater than or equal to a
and less than n
. For the examples n=5
and a=3
, the only odd number that meets that criteria is 3
, so that's all it prints.– Charles Duffy
Nov 22 at 17:36
add a comment |
1 Answer
1
active
oldest
votes
This is a logic error, rather than a problem using bash. If you want to print n
numbers, the easiest way to make sure that happens is to iterate from 0
to n
, as follows:
#!/usr/bin/env bash
n=5; a=3 # of course, you can also read from the user.
if ((a % 2 == 0)); then # if our starting number is even...
(( ++a )) # add 1 to make it odd.
fi
for ((i=0; i<n; i++)); do # iterate from 0 to n...
echo "$((a + i*2))" # ...emitting 2*i+a each time.
done
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435831%2floop-to-print-odd-numbers-not-printing-as-many-as-it-should%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is a logic error, rather than a problem using bash. If you want to print n
numbers, the easiest way to make sure that happens is to iterate from 0
to n
, as follows:
#!/usr/bin/env bash
n=5; a=3 # of course, you can also read from the user.
if ((a % 2 == 0)); then # if our starting number is even...
(( ++a )) # add 1 to make it odd.
fi
for ((i=0; i<n; i++)); do # iterate from 0 to n...
echo "$((a + i*2))" # ...emitting 2*i+a each time.
done
add a comment |
This is a logic error, rather than a problem using bash. If you want to print n
numbers, the easiest way to make sure that happens is to iterate from 0
to n
, as follows:
#!/usr/bin/env bash
n=5; a=3 # of course, you can also read from the user.
if ((a % 2 == 0)); then # if our starting number is even...
(( ++a )) # add 1 to make it odd.
fi
for ((i=0; i<n; i++)); do # iterate from 0 to n...
echo "$((a + i*2))" # ...emitting 2*i+a each time.
done
add a comment |
This is a logic error, rather than a problem using bash. If you want to print n
numbers, the easiest way to make sure that happens is to iterate from 0
to n
, as follows:
#!/usr/bin/env bash
n=5; a=3 # of course, you can also read from the user.
if ((a % 2 == 0)); then # if our starting number is even...
(( ++a )) # add 1 to make it odd.
fi
for ((i=0; i<n; i++)); do # iterate from 0 to n...
echo "$((a + i*2))" # ...emitting 2*i+a each time.
done
This is a logic error, rather than a problem using bash. If you want to print n
numbers, the easiest way to make sure that happens is to iterate from 0
to n
, as follows:
#!/usr/bin/env bash
n=5; a=3 # of course, you can also read from the user.
if ((a % 2 == 0)); then # if our starting number is even...
(( ++a )) # add 1 to make it odd.
fi
for ((i=0; i<n; i++)); do # iterate from 0 to n...
echo "$((a + i*2))" # ...emitting 2*i+a each time.
done
edited Nov 22 at 18:27
community wiki
2 revs, 2 users 92%
Charles Duffy
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435831%2floop-to-print-odd-numbers-not-printing-as-many-as-it-should%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
...so, what's the part that doesn't actually work? See Minimal, Complete, and Verifiable example guidelines -- a question should have a specific problem (not the larger problem your script tries to solve, but the problem with your script), and the shortest code that lets others see that problem.
– Charles Duffy
Nov 22 at 17:31
BTW, a good place to start is logging with
set -x
. See your code running at ideone.com/hgCZDP, with a log in the "stderr" section.– Charles Duffy
Nov 22 at 17:35
2
...in the current case, what you have isn't a bash-the-language bug, but a thinking-about-your-problem bug: You're comparing
a < n
, buta
doesn't start at 0, it starts at the value the user entered, so it doesn't printn
numbers; instead, it just prints odd numbers greater than or equal toa
and less thann
. For the examplesn=5
anda=3
, the only odd number that meets that criteria is3
, so that's all it prints.– Charles Duffy
Nov 22 at 17:36