my problem is the following. If I input number 2, the code counts it as an odd number.
Remainder for 2 / 2 = 0 so the error doesn't make sense.
Below is the program:
import java.util.Scanner;
public class Ohjelma {
public static void main(String[] args) {
// Tänne voit kirjoittaa ohjelmakoodia. Ohjelmasi voit ajaa
// valitsemalla menusta Run->Run File tai painamalla Shift+F6
Scanner reader = new Scanner(System.in);
System.out.println("Type numbers: ");
int number = Integer.parseInt(reader.nextLine());
int sum = 0;
int many = 0;
double average = 0;
int even = 0;
int odd = 0;
while (number != -1) {
System.out.println("Type numbers: ");
sum = sum + number;
number = Integer.parseInt(reader.nextLine());
many++;
average = (double)sum / many;
if (number%2 == 0) {
even++;
} else {
odd++;
}
}
System.out.println("Thank you and see you later!");
System.out.println("The sum is " + sum);
System.out.println("How many numbers: " + many);
System.out.println("Average: " + average);
System.out.println("Even numbers: " + even);
System.out.println("Odd numbers: " + odd);
The main problem is that for the critical part of your program it largely ignores the first input, apart from adding it to the running sum. You want to recast it like this:
Scanner reader = new Scanner(System.in);
int sum = 0;
int many = 0;
double average = 0;
int even = 0;
int odd = 0;
do {
System.out.println("Type numbers: ");
number = Integer.parseInt(reader.nextLine());
if (number == -1)
break;
sum = sum + number;
many++;
average = (double)sum / many;
if (number%2 == 0) {
even++;
} else {
odd++;
}
} while (true);
This will certainly processes even and odd numbers correctly.
Your code reads the second line of input into number before it checks whether number is odd ... and -1 is odd.
Related
Here is the code but it's not optimized for all the exceptions. For example, if the user types 0 from the beginning, it will give the division by zero error and also I don't know why the sum of the numbers work.
Basically, I make num = 1 so it's different than 0 so the while loop can start, but in the end, I made sum = sum - 1; but it gave a wrong number.
Please help me.
import java.util.Scanner;
class Main {
public static void main (String[] args) {
int count = 0;
int num = 1;
double average = 0.0;
int soma = 0;
Scanner scanner = new Scanner(System.in);
System.out.println("Type a integer. 0 to exit.");
while (num != 0) {
num = scanner.nextInt();
sum = sum + num;
count++;
}
if (num == 0) {
average = sum / (count -1);
System.out.println("The sum of the numbers is: "+ soma);
System.out.println("The average of the numbers is: "+ media);
}
}
}
change while to do while:
do {
num = scanner.nextInt();
sum = sum + num;
count++;
}while (num != 0);
Change FROM:
average = sum / (count -1);
System.out.println("The sum of the numbers is: "+ soma);
System.out.println("The average of the numbers is: "+ media);
TO:
average = sum / (count);
System.out.println("The sum of the numbers is: "+ sum);
System.out.println("The average of the numbers is: "+ average);
it should work
(Count positive and negative numbers and compute the average of numbers) Write
a program that reads an unspecified number of integers, determines how many
positive and negative values have been read, and computes the total and average of
the input values (not counting zeros). Your program ends with the input 0. Display
the average as a floating-point number. Here is a sample run:
Enter an integer, the input ends if it is 0: 1 2 -1 3 0
The number of positives is 3
The number of negatives is 1
The total is 5.0
The average is 1.25
I have 2 major issues. 1) I cannot get the loop to stop. 2) even if I did, the average comes up short. Using the example above, my average is always 1.0, not 1.25 as it should be. It's like the program is reading 5 numbers total instead of the 4 numbers that equate to 5. What is seen in the code below is all I can use: Bare basics of java...
import java.util.Scanner;
public class NewClass {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int positive = 0, negative = 0, total = 0, count = 0;
float average;
System.out.println("Enter the number: ");
int number = input.nextInt();
while(number != 0) {
total += number;
count++;
if(number > 0){
positive++;
}
else if(number < 0){
negative++;
}
average = total / count;
System.out.println("The number of positives is "+ positive);
System.out.println("The number of negatives is "+ negative);
System.out.println("The total is "+ total);
System.out.println("The average is "+ average);
}
}
}
You need to read more numbers. You read one value before your loop. You could do something like
int number;
while((number = input.nextInt()) != 0) {
total += number;
count++;
if(number > 0){
positive++;
} else if(number < 0) {
negative++;
}
} // <-- end loop body.
float average = total / (float) count; // <-- not integer math.
System.out.println("The number of positives is " + positive);
System.out.println("The number of negatives is " + negative);
System.out.println("The total is " + total);
System.out.println("The average is " + average);
You should just use the do while.
import java.util.*;
public class PosAndNeg {
public static void main(String [] args){
Scanner input = new Scanner(System.in);
int positive = 0, negative = 0, total = 0, count = 0;
int number;
float average;
System.out.println("Enter the number: ");
do { number = input.nextInt();
total += number;
count++;
if(number > 0){
positive++;
}
else if(number < 0){
negative++;
}
}
while(number != 0);
average = total / count;
System.out.println("The number of positives is "+ positive);
System.out.println("The number of negatives is "+ negative);
System.out.println("The total is "+ total);
System.out.println("The average is "+ average);
}
}
// use type conversion while evaluating average
import java.util.Scanner;
public class newClass{
public static void main(String[] args){
int pos=0;
int neg=0;
int total=0;
Scanner sc= new Scanner(System.in);
System.out.println("enter the total numbers user want to enter");
int i=sc.nextInt();
int [] num= new int[i];
System.out.println("Please enter number");
for (int j = 0; j < num.length; j++)
{
num[j] = sc.nextInt();
if(num[j]>0)
pos++;
else
neg++;
total=total+num[j];
}
System.out.println(num.length);
double avg =(float)total/(num.length);
System.out.println("positive count="+pos+"negative count="+neg+" average ="+avg);
}
}
import java.util.Scanner;
public class NewClass{
public static void main(String[] args) {
int N=0,p=0,t=0;
System.out.println("enter
integers");
Scanner obj=new Scanner
(System.in);
int a=obj.nextInt();
while (a!=0){
t+=a;
if (a<0){
N++;
}
else if (a>0){
p++;
}
int b=obj.nextInt();
a=b;
}
System.out.println("the
number of negative
numbers is\t\t"+N);
System.out.println("the
number of positive number
is\t\t"+p);
float l=N+p;
float average=(float) t/l;
System.out.println("the total
is\t\t"+t);
System.out.println("the
average is\t\t"+average);
}
}
My task is to ask the user how many Fibonacci numbers they want and to print that, then also show the average of those numbers. I made a method to show the Fibonacci numbers in my code below, but I'm having a hard time figuring out how to incorporate the average part of the program. Not asking for you to do it for me because this is homework for class, but it would be nice to know where I am supposed to write the average part of the program.
import java.util.Scanner;
public class Clancy_Hw_03_04{
public static void main (String[] args){
Scanner input = new Scanner(System.in);
int number = 0;
System.out.println ("Enter N: ");
number = input.nextInt();
System.out.println("\n\nFibonacci series for " + number +" numbers : ");
for(int i=1; i<=number; i++){
System.out.print(fibonacciLoop(i) +" ");
}
}
public static int fibonacciLoop(int number){
if(number == 1 || number == 2){
return 1;
}
int fibo1=1, fibo2=1, fibonacci=1;
for(int i= 3; i<= number; i++){
fibonacci = fibo1 + fibo2;
fibo1 = fibo2;
fibo2 = fibonacci;
}
return fibonacci;
}
}
Your problem might be easier to solve if you printed the fibonacci numbers from within your function, fibonacciLoop(), and not try to return each successive number and print it from the main loop. Then the sum and average are right at your fingertips.
For instance:
public static void main(...) {
...
System.out.println("\n\nFibonacci series for " + number +" numbers : ");
fibonacciLoop(i);
}
static void fibonacciLoop(int number) {
int fibo1 = 1, fibo2 = 1, sum = 0, average = 0, fibonacci;
for (int i = 1; i <= 2 && i <= number; i++) {
System.out.print("1 ");
sum += 1;
}
for (i = 3; i <= number; i++) {
fibonacci = fibo1 + fibo2;
System.out.print(fibonacci + " ");
fibo1 = fibo2; fibo2 = fibonacci;
sum += fibonacci;
}
System.out.println(" average = " + (float)sum/number)
}
Edit: Made sure to convert sum to a float before calculating average
Keep computing the sum and then divide it by number.
You can do something like...
int sum = 0;
for(int i=1; i<=number; i++){
int fibo = fibonacciLoop(i);
System.out.print(fibo +" ");
sum += fibo;
}
System.out.println("Average = " + sum/number);
I hope this time I got your question right.
Put another integer in your main function, to store the total value:
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int number = 0;
int sum = 0; // Add this here
}
Then, inside your fibonacci function, add to that variable:
public static int fibonacciLoop(int number)
{
if(number == 1 || number == 2)
{
sum += 1;
return 1;
}
int fibo1=1, fibo2=1, fibonacci=1;
for(int i= 3; i<= number; i++)
{
fibonacci = fibo1 + fibo2;
fibo1 = fibo2;
fibo2 = fibonacci;
}
sum += fibonacci; //Put this here, outisde the loop
return fibonacci;
}
When you want to output the average, simply divide sum by number:
System.out.println("Average is: " + (sum / number));
Here is what I have to do:
"Write a segment of code that reads a sequence of integers from the keyboard until the user enters a negative number. It should then output a count of the number of even integers and the number of odd integers read (not including the final negative value in either count). Remember - 0 is an even number. For example, if the sequence is:
2
7
15
5
88
1243
104
-1
Then the output should be
Number of even integers: 3
Number of odd integers: 4
My code just keeps going even after inputting -1. I have a feeling I am missing a {somewhere or wrote the code wrong. Here is my code:
int oddCount = 0, evenCount = 0;
Scanner in = new Scanner(System.in);
while (oddCount>=0&&evenCount>=0){
System.out.print("Enter an integer: ");
int temp = in.nextInt();
if (temp>0) {
if (temp%2==0)
evenCount = evenCount + 1;
else oddCount = oddCount + 1;
while (temp>0);
System.out.println("Number of even integers: "+evenCount);
System.out.println("Number of odd integers: " +oddCount);
}
}
I think using do While loop will also prevent your isses,
int oddCount = 0;
int evenCount = 0;
Scanner in = new Scanner(System.in);
do {
System.out.print("Enter an integer: ");
int temp = in.nextInt();
if (temp > 0) {
(temp % 2==0)? evenCount++:oddCount++;
}
} while (temp > 0);
Your loop will always continue because while (oddCount>=0&&evenCount>=0) will always be true in your case. Try it like this:
int oddCount = 0, evenCount = 0;
Scanner in = new Scanner(System.in);
boolean continue = true;
while (continue){
System.out.print("Enter an integer: ");
int temp = in.nextInt();
if (temp>0) {
if (temp%2==0)
evenCount++;
else
oddCount++;
}
else {
System.out.println("Number of even integers: "+evenCount);
System.out.println("Number of odd integers: " +oddCount);
continue = false;
}
}
Your code reads just one integer, then stuck at this line: while (temp>0);
The code below solve your problem:
int oddCount = 0, evenCount = 0, temp;
Scanner in = new Scanner(System.in);
System.out.print("Enter an integer: ");
while (true) {
temp = in.nextInt();
if (temp < 0) {
break;
} else if (temp % 2 == 0) {
evenCount = evenCount + 1;
} else {
oddCount = oddCount + 1;
}
}
System.out.println("Number of even integers: " + evenCount);
System.out.println("Number of odd integers: " + oddCount);
There are a few problems here.
First, since you never decrease oddCount and evenCount, the condition oddCount>=0&&evenCount>=0 will always be true.
Second, you have an empty loop that loops endlessly, since it's condition is true and it has no body: while (temp>0);
I'd just take out the first loop, which is redundant, and use a do while loop:
int oddCount = 0;
int evenCount = 0;
Scanner in = new Scanner(System.in);
do {
System.out.print("Enter an integer: ");
int temp = in.nextInt();
if (temp > 0) {
if (temp % 2==0) {
evenCount++;
} else {
oddCount++;
}
}
} while (temp > 0);
System.out.println("Number of even integers: " + evenCount);
System.out.println("Number of odd integers: " + oddCount);
Try this..
Scanner s=new Scanner(System.in);
int evenCount=0, oddCount=0;
while(true)
{
System.out.println("Enter a number");
int n=s.nextInt();
if(n<0) break;
if(n%2==0) evenCount+=n;
else oddCount+=n;
}
System.out.println("even count "+evenCount);
System.out.println("odd count "+oddCount);
I am doing an assignment and have most of what i think will work down, i have to promt the user for random numbers, then display how many negatives as well as positives then sum and average, can anyone help me as i cannot get my code to display the correct amount of values for negative or positive or get it to sum. Here is what i have so far( i also added the break in the end , otherwise it went to an infinate loop)
//Random number evaluation
package chapter_4;
import java.util.Scanner;
public class Four_One {
public static void main(String[] args) {
int positive = 0;
int negative = 0;
int sum = 0;
int count = 0;
Scanner input = new Scanner(System.in);
System.out.print("Enter in a value, if 0 is entered program stops: ");
int data = input.nextInt();
while (data != 0) {
sum += data;
if (data < 0)
negative++;
else if (data > 0)
positive++;
count++;
System.out.println("The number of positives is: " + positive);
System.out.println("The number of negatives is:" + negative);
System.out.println("The total is: " + sum);
System.out.println("The average is: " + sum / data);
break; }
}
}
You are not asking for the nextInt() within the while loop - you do want to ask more than once, correct?
Your average is not an average. I suggest dividing by count instead of data.
at the end of the loop (instead of the break) add data = input.nextInt();
btw for average you should display sum/(double)count (the cast to double is there so you'll see the fraction)
This might help you.
Explanation: Using a do-while loop might be easier because it asks for input and than checks the input. Also put the input statement inside the do-While loop so it asks for multiple inputs.
public static void main(String[] args)
{
int positive = 0;
int negative = 0;
int sum = 0;
int count = 0;
Scanner input = new Scanner(System.in);
int data = 0;
do
{
System.out.print("Enter in a value, if 0 is entered program stops: ");
data = input.nextInt();
sum += data;
count ++;
if(data < 0)
negative ++;
else if(data > 0)
positive ++;
}
//Stops if the value of data is ZERO(0) and continues if it's not
while(data != 0);
System.out.println("Positive Numbers = " + positive);
System.out.println("Negative Numbers = " + negative);
System.out.println("Sum of Numbers = " + sum);
System.out.println("Total Numbers = " + count);
}
import java.util.Scanner;
public class Four_One {
public static void main(String[] args) {
int positive = 0;
int negative = 0;
int sum = 0;
int count = 0;
Scanner input = new Scanner(System.in);
System.out.print("Enter in a value, if 0 is entered program stops: ");
int data = input.nextInt();
while (data != 0) {
sum += data;
if (data < 0)
negative++;
else if (data > 0)
positive++;
count++;
data = input.nextInt();
}
System.out.println("The number of positives is: " + positive);
System.out.println("The number of negatives is:" + negative);
System.out.println("The total is: " + sum);
System.out.println("The average is: " + sum / (double)count);
}
}