Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Lab 5, loops - Manipulate a user inputted int using various loops
- *
- * @author (Alec Campbell)
- * @version (10-18-2018)
- */
- import java.util.Scanner; //Import Scanner for console input
- public class Loops
- {
- public static void main (String [] args)
- {
- // Program output header
- System.out.println("Programmer: Alec Campbell");
- System.out.println("Course: COSC111, Fall 2018");
- System.out.println("Lab#: 5");
- System.out.println("Due date: Oct. 18 2018\n");
- // Create new scanner object "keyIn"
- Scanner keyIn = new Scanner (System.in);
- // Repeat the process as many times as the user wishes
- String choice;
- do
- {
- // Ask user to input a whole number then assign it to a variable
- System.out.print("Enter a whole number: ");
- int userInt = keyIn.nextInt();
- // Check if userInt is even or odd and print result
- if (userInt % 2 == 0)
- System.out.printf("%d is even", userInt);
- else
- System.out.printf("%d is odd", userInt);
- // Output all numbers from 1 through userInt
- System.out.print("\n\n");
- System.out.printf("Numbers from 1 to %d are:\n", userInt);
- for (int i = 1; i <= userInt; i++)
- System.out.print(i + "\t");
- // Output the square of all odd numbers from 1 through userInt
- System.out.print("\n\n");
- System.out.printf("Square of odd numbers from 1 to %d are:\n", userInt);
- int counter = 1;
- while (counter <= userInt)
- {
- if (counter % 2 == 1)
- System.out.print((counter * counter) + "\t");
- counter++;
- }
- // Output the sum of all even numbers from 1 to userInt
- System.out.print("\n\n");
- System.out.printf("Sum of even numbers from 1 to %d is:\n", userInt);
- int sum_of_evens = 0;
- counter = 1;
- while (counter <= userInt)
- {
- if (counter % 2 == 0)
- sum_of_evens += counter;
- counter++;
- }
- System.out.print(sum_of_evens);
- // Outputs all numbers from 1 to userInt, only 5 numbers per line
- System.out.print("\n\n");
- System.out.printf("Numbers from 1 through %d (5 numbers per line):\n", userInt);
- for (int i = 1; i <= userInt; i++)
- {
- System.out.print(i + "\t");
- if (i % 5 == 0)
- System.out.print("\n");
- }
- // Modified program that allows the user to repeat the above steps
- // as many times as (s)he wishes.
- // Ask user if they wish to repeat the program
- System.out.print("\n\n");
- System.out.print("Do it again, yes (or no)?");
- choice = keyIn.next();
- System.out.print("\n");
- } while (choice.equalsIgnoreCase("yes"));
- }
- }
- /*
- * Programmer: Alec Campbell
- * Course: COSC111, Fall 2018
- * Lab#: 5
- * Due date: Oct. 18 2018
- *
- * Enter a whole number: 12
- * 12 is even
- *
- * Numbers from 1 to 12 are:
- * 1 2 3 4 5 6 7 8 9 10 11 12
- *
- * Square of odd numbers from 1 to 12 are:
- * 1 9 25 49 81 121
- *
- * Sum of even numbers from 1 to 12 is:
- * 42
- *
- * Numbers from 1 through 12 (5 numbers per line):
- * 1 2 3 4 5
- * 6 7 8 9 10
- * 11 12
- *
- * Do it again, yes (or no)?yes
- *
- * Enter a whole number: 9
- * 9 is odd
- *
- * Numbers from 1 to 9 are:
- * 1 2 3 4 5 6 7 8 9
- *
- * Square of odd numbers from 1 to 9 are:
- * 1 9 25 49 81
- *
- * Sum of even numbers from 1 to 9 is:
- * 20
- *
- * Numbers from 1 through 9 (5 numbers per line):
- * 1 2 3 4 5
- * 6 7 8 9
- *
- * Do it again, yes (or no)?no
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement