Loops boolean and repetition
import java.util.*;
public class Assignment4 {
public Assignment4() {
}
public static void main(String[] args){
int num, max, min, total=0, sum=0;
Scanner input = new Scanner(System.in);
System.out.println("Enter a positive integer. Enter 0 to quit. ");
num = input.nextInt(); //collects data from what the user inputs
max = num; min = num;
while(num > 0) {
total++;
if(num > max)
max = num;
if(num < min)
min = num;
sum += num;
if(isPrime(num))
System.out.println("The number "+num+" is a prime number.”); else
System.out.println("The number "+num+" is not a prime number.");
System.out.println("Enter a positive integer. Enter 0 to quit.");
num = input.nextInt(); }
System.out.println("The maximum positive number is: "+max);
System.out.println("The minimum positive number is: "+min);
System.out.println("The sum is: "+sum);
System.out.println("The count of number(s) is: "+total);
System.out.println("The average is “+(double)(sum/total)); }
public static boolean isPrime(int j) {
int f;
for(f=2; f<Math.sqrt(j); f++)
if(j%f==0)
return false;
return true;
Length of string
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter a string.");
String word1 = keyboard.next();
System.out.println("Please enter another string.");
String word2 = keyboard.next();
System.out.println();
if(word1.length()%2==0) {
System.out.println("The first string's length is even.”); } else {
System.out.println("The first string's length is odd.”); }
if(word2.length()%2==0) {
System.out.println("The second string's length is even.”); } else {
System.out.println("The second string's length is odd.”); }
System.out.println();
if(word1.equals(word2)) {
System.out.println("The two strings are the same.”); } else
System.out.println("The two strings are different.”); {
System.out.println();
if(word2.length()>word1.length()) {


You've reached the end of your free preview.
Want to read both pages?
- Spring '14
- Letter case, Natural number, System.out.println