rushdie

Presentatio 3 Home work exersices

Nov 22nd, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.97 KB | None | 0 0
  1. package HWpresentation3;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Exercise1 {
  6.  
  7. public static void main(String[] args) {
  8. // TODO Auto-generated method stub
  9. Scanner s = new Scanner(System.in);
  10. int location = 1, newNum = 0, dig, x, temp;
  11.  
  12. System.out.println("Please, Enter a number with at least 5 digits and less than 12 digits? ");
  13. x = s.nextInt();
  14. temp = x;
  15.  
  16. while (temp > 0) {
  17. dig = temp % 10; // to get the right most digit
  18. // if the digit is divisible by 2 then it is even
  19. if (dig % 2 == 0) {
  20. newNum = newNum + location * dig;// location set the base 10 of the number
  21. location = location * 10; // up it by 10 for 100s is 1000
  22. }
  23. temp = temp / 10; // cut the last digit of the number
  24. }
  25. System.out.println("The number is : " + newNum);
  26.  
  27. }
  28. }
  29. package HWpresentation3;
  30.  
  31. import java.util.Scanner;
  32.  
  33. public class Exercise2 {
  34.  
  35. public static void main(String[] args) {
  36. // TODO Auto-generated method stub
  37. Scanner s = new Scanner(System.in);
  38. int location = 1, temp, Num1, Num2, dig1, dig2,newNum=0;
  39.  
  40. System.out.println("Please, Enter the first number ? ");
  41. Num1 = s.nextInt();
  42. System.out.println("Please, Enter the second number ? ");
  43. Num2 = s.nextInt();
  44. temp = Num2;
  45. while (location > 0)
  46. {
  47. dig1 = Num2 % 10; // to get the right most digit of num 2
  48. newNum = newNum + location * dig1;// location set the base 10 of the number
  49. location = location * 10; // up it by 10 for 100s is 1000
  50. dig2 = Num1 % 10;// to get the right most digit of num 1
  51. newNum = newNum + location * dig2;
  52. location = location * 10; // set the base of the next digit
  53.  
  54. Num2 = Num2 / 10; // cut the last digit of the num2
  55. Num1 = Num1 / 10; // cut the last digit of the num1
  56. }
  57. System.out.println("The number is : " +newNum);
  58.  
  59. }
  60. }
  61. package HWpresentation3;
  62. import java.util.Scanner;
  63.  
  64. public class Exercise3 {
  65.  
  66. public static void main(String[] args) {
  67. // TODO Auto-generated method stub
  68. Scanner s = new Scanner(System.in);
  69. int maxDigit=0, Num, curDigit, temp;
  70.  
  71. System.out.println("Please, Enter the first number ? ");
  72. Num = s.nextInt();
  73. temp = Num;
  74. while (temp > 0)
  75. {
  76. curDigit = temp % 10; // to get the right most digit of num
  77.  
  78. if(curDigit > maxDigit) //Check which digit is bigger
  79. {
  80. maxDigit = curDigit ;
  81. }
  82. temp = temp /10; // Cut the last digit from the number
  83. }
  84. System.out.println("The number is : " +maxDigit);
  85.  
  86. }
  87. }
  88. package HWpresentation3;
  89.  
  90. import java.util.Scanner;
  91.  
  92. public class Exercise4 {
  93.  
  94. public static void main(String[] args) {
  95. // TODO Auto-generated method stub
  96. Scanner s = new Scanner(System.in);
  97. int temp, Numbase10=0, Num, power=0;
  98.  
  99. System.out.println("Please, Enter A Bianary number - Example 1001 ? ");
  100. Num = s.nextInt();
  101. while (true)
  102. {
  103. if(Num == 0){ // If num of digits = 0 Exit while Else
  104. break;
  105. }else
  106. {
  107. temp = Num % 10; //get the last digit
  108. Numbase10 += temp*Math.pow(2,power); //use 2^ power = 0 Times the digit read
  109. Num = Num / 10; // remove digit
  110. power++; // add 1 to power next digit 2^1 time Digit
  111. }
  112. }
  113.  
  114. System.out.println("The number is : " +Numbase10); // print the base 10 Number
  115.  
  116. }
  117. }
  118. package HWpresentation3;
  119.  
  120. import java.util.Scanner;
  121.  
  122. public class Exercise5 {
  123.  
  124. public static void main(String[] args) {
  125. // TODO Auto-generated method stub
  126. Scanner s = new Scanner(System.in);
  127. int temp=0, Num;
  128. String DecimalNum = "";
  129. System.out.println("Please, Enter A Decimal number - Example 0-9 allowed ? ");
  130. Num = s.nextInt();
  131.  
  132. while (Num > 0) {
  133. temp = Num % 2;
  134. DecimalNum = temp + DecimalNum;
  135. Num = Num / 2;
  136. }
  137. System.out.println("The Decimal number is : " + (DecimalNum)); // print the base 10 number
  138. }
  139.  
  140. }
  141. package HWpresentation3;
  142.  
  143. import java.util.Scanner;
  144.  
  145. public class Exercise6 {
  146.  
  147. public static void main(String[] args) {
  148. // TODO Auto-generated method stub
  149.  
  150. int base, i, j, k;
  151.  
  152. Scanner s = new Scanner(System.in);
  153. System.out.println("Enter any number of stars for the base :");
  154. base = s.nextInt();
  155.  
  156. for (i = 1; i <= base; i++) { //loop until base entered reached
  157. for (j = 1; j <= i; j++)//prints the space
  158. System.out.print(" ");
  159. for (j = 1; j <= base - i; j++)//prints the stars = base entered - 1 star
  160. System.out.print("*");
  161. System.out.println(); // new Line
  162.  
  163. }
  164. }
  165. }
  166. package HWpresentation3;
  167.  
  168. import java.util.Scanner;
  169.  
  170. public class Exercise7 {
  171.  
  172. public static void main(String[] args) {
  173. // TODO Auto-generated method stub
  174.  
  175. int rows = 1, base, i, j, k;
  176.  
  177. Scanner s = new Scanner(System.in);
  178. System.out.println("Enter any number of stars for the base :");
  179. base = s.nextInt();
  180.  
  181. for (i = 1; i <= base ; i++) { // prints the first triangle of stars - left
  182. for (j = 1; j <= i; j++)
  183. System.out.print("*");
  184.  
  185. for (k = 1; k <= (base * 2) - 2 * i; k++) // print the empty spaces
  186. System.out.print(" ");
  187.  
  188. for (j = 1; j <= i; j++)// Prints the Right stars
  189. System.out.print("*");
  190.  
  191. System.out.println();
  192. }
  193.  
  194.  
  195. }
  196.  
  197.  
  198. }
  199. package HWpresentation3;
  200.  
  201. import java.util.Random;
  202. import java.util.Scanner;
  203.  
  204. public class Exercise8 {
  205.  
  206. // TODO Auto-generated method stub
  207. public static void main(String args[]) {
  208. Random random = new Random();
  209. Scanner input = new Scanner(System.in);
  210. int MIN = 1; //Initilize our input from 1 - 100
  211. int MAX = 100;
  212. int comp = random.nextInt(MAX - MIN + 1) + MIN; // Computer Randomly choose a number
  213. int user;
  214. int guesses = 0; // counter for how many time user gussed
  215. do {
  216. System.out.print("Guess a number between 1 and 100: ");// prompt user to try find number Computer selected
  217. user = input.nextInt();
  218. guesses++;
  219. if (user > comp) // check the gussed number
  220. System.out.println("My number is less than " + user + "."); //prompt user if number gussed smaller
  221. else if (user < comp)
  222. System.out.println("My number is greater than " + user + ".");
  223. else // user got a hit and find the Random number generated by computer
  224. System.out.println("Well done! " + comp + " was my number! You guessed it in " + guesses + " guesses.");
  225. } while (user != comp); // Condition found the number
  226.  
  227. }
  228.  
  229. }
  230.  
  231.  
  232. package HWpresentation3;
  233.  
  234. import java.util.Scanner;
  235.  
  236. public class Exercise9 {
  237.  
  238. public static void main(String[] args) {
  239. // TODO Auto-generated method stub
  240. Scanner s = new Scanner(System.in);
  241.  
  242. int col, frame, rows, subF;
  243. //System.out.println("Enter n the number of squares you wish?");
  244. // n = s.nextInt();
  245. int n = (int)(Math.random()*10);
  246. System.out.println("The number Randomly selected is d: "+n);
  247. for (frame = 1; frame <= n; frame++) {
  248. for (subF = 1; subF <= n; subF++) {
  249. for (rows = 1; rows <= n; rows++) {
  250. for (col = 1; col <= n; col++) {
  251. System.out.print("*");
  252. }
  253. System.out.print(" ");
  254. }
  255. System.out.println();
  256. }
  257. System.out.println();
  258. }
  259. }
  260. }
Add Comment
Please, Sign In to add comment