on 10-09-2006 4:42 PM
I've been working on this new program and I can not figure out how to make my program recognize which numbers are odd and which are even. Here is what I have so far:
import java.util.*;
public class Lab5 {
public static void main(String[] args) {
Scanner kbd = new Scanner(System.in);
int number;
System.out.print("Enter a number (0 to quit): ");
number = kbd.nextInt();
while (number != 0) {
if (number % 2 == 0) {
System.out.println(number + " is even.");
} else {
System.out.println(number + " is odd.");
}
System.out.print("Enter a number (0 to quit): ");
number = kbd.nextInt();
}
System.out.println(number + " numbers were even" );
System.out.println(number + " numbers were odd" );
}
}
Hi Jorden,
i think you need to print a counter for even and odd number, if so try this way:
import java.util.*;
public class Lab5 {
public static void main(String[] args) {
Scanner kbd = new Scanner(System.in);
int number;
int even_counter = 0;
int odd_counter = 0;
System.out.print("Enter a number (0 to quit): ");
number = kbd.nextInt();
while (number != 0) {
if (number % 2 == 0) {
System.out.println(number + " is even.");
even_counter++;
} else {
System.out.println(number + " is odd.");
odd_counter++;
}
System.out.print("Enter a number (0 to quit): ");
number = kbd.nextInt();
}
System.out.println(even_counter + " numbers were even" );
System.out.println(odd_counter + " numbers were odd" );
}
}
Kind Regards,
Sergio
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
hi
good
use similar logic in java you ll get the output
int main()
{
int input, even=0, odd=0;
do
{
cout << "Please enter a positive integer (negative integer to stop):"<< ' ';
cin>> input;
if ( (even%2 == 0) && (input >= 0) )
even++;
else if (input >= 0)
odd++;
}
while (input>=0);
cout << "You have entered" << ' '<< odd << ' ' <<"odd numbers." << endl;
cout << "And" << ' ' << even << ' ' <<"even numbers." << endl;
system("PAUSE");
return 0;
}
thanks
mrutyun^
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
P.S. Award point if it helped..
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Again this is not a forum to get answers for school work! Please refrain from answering these posts as they do nothing but undermine the value of these forums. Lately there has been a dramatic increase of completely useless posts that add nothing to overall value of this community.
To the person who posted this question. First of all, if you need help go see your professor/teacher. This isn't the place to ask such questions. SDN is a forum where SAP professionals ask for assistance, guidance and tips from fellow developers all over the world in order to resolve serious issues. Your posts not only undermine the whole purpose of this community, but also make it hard for us to filter through useless posts and find relevant ones so as to be able to help those who actually need it.
Currently work is being done by the SDN team to introduce spam reporting functionality. Once this is in place (and I hope soon) perhaps we'll finally be able to clear out irrelevant posts.
regards,
Dion
User | Count |
---|---|
85 | |
10 | |
10 | |
10 | |
7 | |
6 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.