Question: I am writing a program and everything here is right except for the part where I have to count the number of prime numbers entered.

I am writing a program and everything here is right except for the part where I have to count the number of prime numbers entered. The bolded section is the one in question that is meant to find how many prime numbers are entered by the user. ( The prime numbers are greater than, and not including, 1.)

/*

Assignment2

Name: Brennan Ursu

Description: This code reads and counts the amount of integers entered, the amount of prime integers, the amount of negative integers, and even integers that are divisible by 3. It then provides an output declaring each answer. */

import java.util.Scanner; // uses the Scanner package located in the "java.util" directory

public class Assignment2 { public static void main (String[] args) {

Scanner console = new Scanner(System.in); // creates a new scanner

int num = 0; // declare an integer int count = 0; // first number entered will be bigger than -1 int even = 0; // 0 because so far there are no even numbers entered int positive = 0; int neg = 0; int m=0, f = 0; int i = 0;

{ do // performs a do loop that occurs if it sees statements that it fulfills. {

if(num >= 0) // Meant to track each number entered to find the amount of numbers entered. { count++; // Then adds one to each time a number is entered. } if(num < 0) // Meant to track each number entered to find the amount of numbers entered. { count++; // Then adds one to each time a number is entered. }

for(i = 1;i <= num/2; i++) //track prime numbers { if(num %i == 0) // Adds all numbers that are prime. { f = 1; break; } } if(f == 0) positive++; //add 1 every time there is a prime number.

if (num < 0) // Adds all numbers that are negative integers to the sum of the negative integers. { neg++; }

if (num % 3 == 0) // Count all integers that are divisible by 3. { even++; } }while((num = console.nextInt()) != 0);

}

System.out.print("The count of integers is " + count + " " + "The count of prime numbers is " + positive + " " + "The count of negative integers is " + neg + " " + "The count of integers divisible by 3 is " + even + " "); while(num != 0);

{

}

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!