Advertisement
bOascUmPs

Untitled

Oct 23rd, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.45 KB | None | 0 0
  1. PL5 - EXERCICIO 5
  2.  
  3. /** Retira os números pares a um número inteiro positivo */
  4. import javax.swing.JOptionPane;
  5.  
  6. public class PL5_5 {
  7.  
  8. public static void main(String[] args) {
  9.  
  10. int res = 0, aux = 1, num, d;
  11. String vAuxiliar;
  12. vAuxiliar = JOptionPane.showInputDialog("Inserir um número inteiro positivo:");
  13. num = Integer.parseInt(vAuxiliar);
  14.  
  15. while (num != 0) {
  16. d = num % 10;
  17. if (d % 2 == 1) {
  18. res = res + d * aux;
  19. aux = aux * 10;
  20. }
  21. num = num / 10;
  22. }
  23. JOptionPane.showMessageDialog(null, "O resultado é " + res);
  24. }
  25.  
  26. }
  27. _____________________________________________________________
  28.  
  29. PL5 - EXERCICIO 7
  30.  
  31. /** */
  32.  
  33. import javax.swing.JOptionPane;
  34.  
  35. public class PL5_7 {
  36.  
  37. public static void main(String[] args) {
  38. int a, b, c, d, e, aux, num;
  39. String vAuxiliar;
  40. vAuxiliar = JOptionPane.showInputDialog("'-'");
  41. a = Integer.parseInt(vAuxiliar);
  42. vAuxiliar = JOptionPane.showInputDialog("'-'");
  43. b = Integer.parseInt(vAuxiliar);
  44. if (a>b){
  45. aux = a;
  46. a = b;
  47. b = aux;
  48. }
  49. e = 0;
  50. vAuxiliar = JOptionPane.showInputDialog("'-'");
  51. d = Integer.parseInt(vAuxiliar);
  52. for (c = 1; d > c; c++){
  53. do {
  54. vAuxiliar = JOptionPane.showInputDialog("'-'");
  55. num = Integer.parseInt(vAuxiliar);
  56. } while (num < 0);
  57. if (num % a == 0 && b % num == 0){
  58. e++;
  59. }
  60. }
  61. JOptionPane.showMessageDialog(null, "... " + e);
  62. }
  63.  
  64. }
  65.  
  66. ______________________________________________________
  67.  
  68. PL5 - EXERCICIO 9
  69.  
  70. /** Transformar um número binário num número decimal */
  71.  
  72. import javax.swing.JOptionPane;
  73.  
  74. public class PL5_9 {
  75.  
  76. public static void main(String[] args) {
  77. int numBinario, dig, numDecimal = 0, ordemPotencia = 0;
  78. String vAuxiliar;
  79. vAuxiliar = JOptionPane.showInputDialog("Introduzir um número binário:");
  80. numBinario = Integer.parseInt(vAuxiliar);
  81. while (numBinario != 0) {
  82. dig = numBinario % 10;
  83. if (dig == 1){
  84. numDecimal = (int) (numDecimal + Math.pow(2, ordemPotencia));
  85. }
  86. ordemPotencia = ordemPotencia + 1;
  87. numBinario = numBinario / 10;
  88. }
  89. JOptionPane.showMessageDialog(null, "Número em decimal: " + numDecimal);
  90. }
  91.  
  92. }
  93.  
  94.  
  95. __________________________________________________
  96.  
  97. PL5 - EXERCICIO COMPLEMENTAR 1
  98.  
  99. import javax.swing.JOptionPane;
  100.  
  101. public class PL5_EC1 {
  102.  
  103. public static void main(String[] args) {
  104.  
  105. int numero, alg, i = 0, j = 0, perc;
  106. String resp;
  107. resp = JOptionPane.showInputDialog("Qual o número?");
  108. numero = Integer.parseInt(resp);
  109.  
  110. do {
  111. j++;
  112. alg = numero % 10;
  113. if (alg % 2 == 0) {
  114. i++;
  115. }
  116. numero = numero / 10;
  117. } while (numero > 0);
  118.  
  119. perc = (int) (float) (i / j + 0.5f); /* ????????? */
  120. JOptionPane.showMessageDialog(null, "Valor=" + (int) perc);
  121. }
  122. }
  123.  
  124. _______________________________________________________________
  125.  
  126. PL5 - EXERCICIO COMPLEMENTAR 2
  127.  
  128. /* Transformar N números de binário para decimal */
  129.  
  130. import javax.swing.JOptionPane;
  131.  
  132. public class PL5_EC2 {
  133.  
  134. public static void main(String[] args) {
  135. int N, num, dig = 0;
  136. String vAuxiliar;
  137. vAuxiliar = JOptionPane.showInputDialog("Quantos números?");
  138. N = Integer.parseInt(vAuxiliar);
  139.  
  140. for (int i = 1; i <= N; i++) {
  141. int numDecimal = 0, ordemPotencia = 0;
  142. do {
  143. vAuxiliar = JOptionPane.showInputDialog("Qual o número em binário? (>0)");
  144. num = Integer.parseInt(vAuxiliar);
  145. } while (num <= 0);
  146. while (num != 0) {
  147. dig = num % 10;
  148. if (dig == 1) {
  149. numDecimal = (int) (numDecimal + Math.pow(2, ordemPotencia));
  150. } else if (dig > 1) {
  151. JOptionPane.showMessageDialog(null, "Erro: Introduzir um número válido.");
  152. num = 0;
  153. numDecimal = 0;
  154. N = N + 1;
  155. }
  156. ordemPotencia = ordemPotencia + 1;
  157. num = num / 10;
  158. }
  159. if (dig < 2) {
  160. JOptionPane.showMessageDialog(null, "Número em decimal: " + numDecimal);
  161. }
  162.  
  163. }
  164. }
  165. }
  166.  
  167.  
  168. _________________________________________________________
  169.  
  170. PL5 - EXERCICIO COMPLEMENTAR 3
  171.  
  172. /* */
  173.  
  174. import javax.swing.JOptionPane;
  175.  
  176. public class PL5_EC3 {
  177.  
  178. public static void main(String[] args) {
  179.  
  180. int num, dig, numAuxiliar = 1, persistencia = 0;
  181. String vAuxiliar;
  182. vAuxiliar = JOptionPane.showInputDialog("Qual o número?");
  183. num = Integer.parseInt(vAuxiliar);
  184. while (num > 10) {
  185. while (num != 0) { /* ????? */
  186. dig = num % 10;
  187. numAuxiliar = numAuxiliar * dig;
  188. num = num / 10;
  189. }
  190. persistencia++;
  191. num = numAuxiliar;
  192. numAuxiliar = 0;
  193. }
  194. JOptionPane.showMessageDialog(null, "Persistência: " + persistencia);
  195. }
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement