write a program that will display all the odd numbers between 1 and 15. here is the code that i used

Scanner scan = new Scanner (System.in);
        int i, num=0, count_odd = 0; 


        for(i=1; i<=15; i++)
        {

          System.out.println("please enter   number ");
           num = scan.nextInt();
        }


        if(num%2!=0)
        {

           count_odd++;

        }

         System.out.println("The  number of odd numbers entered were " + count_odd);

and here is the result when i run it. would like to know if it is the wrong or right code and if it is the wrong code where do i went wrong.
here is the result.
please enter number
1
please enter number
2
please enter number
3
please enter number
4
please enter number
5
please enter number
6
please enter number
7
please enter number
8
please enter number
9
please enter number
10
please enter number
11
please enter number
12
please enter number
13
please enter number
14
please enter number
15
The number of odd numbers entered were 1
BUILD SUCCESSFUL (total time: 33 seconds)

the program is supposed to display only odd numbers only. dont where I am wrong

Recommended Answers

All 8 Replies

First of all you don't need a scaner.
You need to have for loop with algorithm to see is it odd or not inside that loop.
So you will have something like this:

for(i=1; i<=15; i++)
{
// if you get remainder of division with 2 then that is odd number
int result = (int) (i % 2);
//if result is 1 then that is odd number so you just write it down
if (result == 1) {
System.out.println(i);
}
}

Your code is the solution to a different problem (count the number of odd numbers in an arbitrary list of numbers entered b the user). Looks like you copied from the wrong source.

commented: zing +13

The problem is that you want to have your if loop inside the for, because now you execute the for loop, you end up having a value of 15, then your if loop executes with only that value, therefore 1 number was entered and it was odd as well

@jamescherrill what makes you think that I copied the source of code, for your information, I didnt copied, an example for a program was done in class and I just cant remember how it goes, dont assume thing that u dont know.

an @slavi thanks u very much for your answer. it is much appreciated.

@divinity: kidding, right?
you don't need a Scanner, you make it needlesly complex. the code you need is much easier than what you posted. if you had written the code yourself, you would have understood it, or at the very least, be able to alter it here and there, and you would have had your answer almost straight away.

@stultuske so i dont need a scanner the reason why I was using the scanner is because it is what I was taught, I have no idea that you dont need to use the scanner, I am only going by what I am taught and what the teacher is looking for. why is it complex when u used the scanner, plz enlighten me and I am a newbie here, this is my first time doing programming. it is not something that I have been doing a long time.

You don't need a scaner because he read an input from user.
So you need to make a program that writes from 1 to 15 odd numbers.
So you have a loop that goes from 1 to 15.
Then inside that loop you need to check is that code odd or not.
You can check it easy just divide it with 2 and if you get a rest then it is odd number. For example:
2/2 is 1 rest is 0
3/2 is 1 rest is 1 so it is odd number.
4/2 is 2 rest is 0
5/2 is 2 rest is 1 so it is odd number.
...
If it is odd then just write it down using System.out.println(i);
So I give you a solution of your assigment in my last post but you give me minus and you don't even know what you need to do.
My previous post has a mistake on purpose you need to define that "i" in foor loop.
You can define it befor loop like I did or in the loop just put for (int i=1; ...

So please, first read your asigment, analyze it, then try to make a solution.

commented: Excellent teaching post - explains what OP needs to do next +15

as to why you don't need a scanner: you need to print the odd numbers between 1 and 15.

for(i=1; i<=15; i++)
        {
          System.out.println("please enter   number ");
           num = scan.nextInt();
        }

so, here you spent time reading numbers you already have: the i is the number you need to use, you don't need num.
and using a scanner here, is what is causing your problem.

you read 15 numbers, overwrite each value, and only perform the next operations for the last number you read.

your teacher doesn't expect you to use a Scanner. if the assignment was to make make 15 dynamic, so that you can enter the number of the highest value, then that would/could be read using Scanner. if you don't need it dynamically, and he expects you to use Scanner, he's wrong.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.