Advertisement
Guest User

Untitled

a guest
Oct 18th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. /**
  2. * Lab 5, loops - Manipulate a user inputted int using various loops
  3. *
  4. * @author (Alec Campbell)
  5. * @version (10-18-2018)
  6. */
  7.  
  8. import java.util.Scanner; //Import Scanner for console input
  9. public class Loops
  10. {
  11. public static void main (String [] args)
  12. {
  13. // Program output header
  14. System.out.println("Programmer: Alec Campbell");
  15. System.out.println("Course: COSC111, Fall 2018");
  16. System.out.println("Lab#: 5");
  17. System.out.println("Due date: Oct. 18 2018\n");
  18.  
  19. // Create new scanner object "keyIn"
  20. Scanner keyIn = new Scanner (System.in);
  21.  
  22. // Repeat the process as many times as the user wishes
  23. String choice;
  24. do
  25. {
  26. // Ask user to input a whole number then assign it to a variable
  27. System.out.print("Enter a whole number: ");
  28. int userInt = keyIn.nextInt();
  29.  
  30. // Check if userInt is even or odd and print result
  31. if (userInt % 2 == 0)
  32. System.out.printf("%d is even", userInt);
  33. else
  34. System.out.printf("%d is odd", userInt);
  35.  
  36. // Output all numbers from 1 through userInt
  37. System.out.print("\n\n");
  38. System.out.printf("Numbers from 1 to %d are:\n", userInt);
  39. for (int i = 1; i <= userInt; i++)
  40. System.out.print(i + "\t");
  41.  
  42. // Output the square of all odd numbers from 1 through userInt
  43. System.out.print("\n\n");
  44. System.out.printf("Square of odd numbers from 1 to %d are:\n", userInt);
  45. int counter = 1;
  46. while (counter <= userInt)
  47. {
  48. if (counter % 2 == 1)
  49. System.out.print((counter * counter) + "\t");
  50. counter++;
  51. }
  52.  
  53. // Output the sum of all even numbers from 1 to userInt
  54. System.out.print("\n\n");
  55. System.out.printf("Sum of even numbers from 1 to %d is:\n", userInt);
  56. int sum_of_evens = 0;
  57. counter = 1;
  58. while (counter <= userInt)
  59. {
  60. if (counter % 2 == 0)
  61. sum_of_evens += counter;
  62. counter++;
  63. }
  64. System.out.print(sum_of_evens);
  65.  
  66. // Outputs all numbers from 1 to userInt, only 5 numbers per line
  67. System.out.print("\n\n");
  68. System.out.printf("Numbers from 1 through %d (5 numbers per line):\n", userInt);
  69. for (int i = 1; i <= userInt; i++)
  70. {
  71. System.out.print(i + "\t");
  72. if (i % 5 == 0)
  73. System.out.print("\n");
  74. }
  75. // Modified program that allows the user to repeat the above steps
  76. // as many times as (s)he wishes.
  77. // Ask user if they wish to repeat the program
  78. System.out.print("\n\n");
  79. System.out.print("Do it again, yes (or no)?");
  80. choice = keyIn.next();
  81. System.out.print("\n");
  82. } while (choice.equalsIgnoreCase("yes"));
  83. }
  84. }
  85.  
  86. /*
  87. * Programmer: Alec Campbell
  88. * Course: COSC111, Fall 2018
  89. * Lab#: 5
  90. * Due date: Oct. 18 2018
  91. *
  92. * Enter a whole number: 12
  93. * 12 is even
  94. *
  95. * Numbers from 1 to 12 are:
  96. * 1 2 3 4 5 6 7 8 9 10 11 12
  97. *
  98. * Square of odd numbers from 1 to 12 are:
  99. * 1 9 25 49 81 121
  100. *
  101. * Sum of even numbers from 1 to 12 is:
  102. * 42
  103. *
  104. * Numbers from 1 through 12 (5 numbers per line):
  105. * 1 2 3 4 5
  106. * 6 7 8 9 10
  107. * 11 12
  108. *
  109. * Do it again, yes (or no)?yes
  110. *
  111. * Enter a whole number: 9
  112. * 9 is odd
  113. *
  114. * Numbers from 1 to 9 are:
  115. * 1 2 3 4 5 6 7 8 9
  116. *
  117. * Square of odd numbers from 1 to 9 are:
  118. * 1 9 25 49 81
  119. *
  120. * Sum of even numbers from 1 to 9 is:
  121. * 20
  122. *
  123. * Numbers from 1 through 9 (5 numbers per line):
  124. * 1 2 3 4 5
  125. * 6 7 8 9
  126. *
  127. * Do it again, yes (or no)?no
  128. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement