Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. import java.text.DecimalFormat;
  2. import java.util.Scanner;
  3. public class Ass08ID6120
  4. {
  5. public static void main(String [] args)
  6. {
  7. int size, total;
  8. Scanner arraysize = new Scanner(System.in);
  9. System.out.println("How many numbers will you enter?");
  10. size = arraysize.nextInt();
  11. int[] a = new int[size];
  12. System.out.println("Enter " + size + " integers, one per line:");
  13. for(int v = 0; v <a.length; v++)
  14. {
  15. int x;
  16. x = arraysize.nextInt();
  17. a[v] = x;
  18. }
  19. total = totalarray(a);
  20. System.out.println("The sum is: " + total);
  21. System.out.println("The numbers are:");
  22. percentage(a, total);
  23.  
  24.  
  25. //part 2 palindrome
  26.  
  27. }
  28.  
  29. private static int totalarray(int[] a)
  30. {
  31. int t = 0;
  32. for(int y = 0; y <a.length; y++)
  33. {
  34. t += a[y];
  35. }
  36. return t;
  37. }
  38.  
  39.  
  40.  
  41. public static void percentage(int[] s, int t)
  42. {
  43. double percent;
  44. DecimalFormat numberFormat = new DecimalFormat("#.0000");
  45. for(int h = 0; h <s.length; h++)
  46. {
  47.  
  48. percent = (s[h] / ((double)t)) * 100;
  49. System.out.println(s[h] + ", which is " + numberFormat.format(percent) + "% of the sum.");
  50. }
  51.  
  52. }
  53.  
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement