NumberUtilities.java 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. public class NumberUtilities {
  2. public static String getRange(int stop) {
  3. StringBuilder builder = new StringBuilder();
  4. for(int i= 0; i < stop ;i++)
  5. {
  6. builder.append(i);
  7. }
  8. return builder.toString();
  9. }
  10. public static String getRange(int start, int stop) {
  11. StringBuilder builder = new StringBuilder();
  12. for(int i = start;i < stop;i++)
  13. {
  14. builder.append(i);
  15. }
  16. return builder.toString();
  17. }
  18. public static String getRange(int start, int stop, int step) {
  19. StringBuilder builder = new StringBuilder();
  20. for(int i =start; i < stop ;i = i + step)
  21. {
  22. builder.append(i);
  23. }
  24. return builder.toString();
  25. }
  26. public static String getEvenNumbers(int start, int stop) {
  27. StringBuilder builder = new StringBuilder();
  28. for(int i =start; i < stop ;i++)
  29. {
  30. if(i % 2 == 0)
  31. {
  32. builder.append(i);
  33. }
  34. }
  35. return builder.toString();
  36. }
  37. public static String getOddNumbers(int start, int stop) {
  38. StringBuilder builder = new StringBuilder();
  39. for(int i =start; i < stop ;i++)
  40. {
  41. if(i % 2 != 0)
  42. {
  43. builder.append(i);
  44. }
  45. }
  46. return builder.toString();
  47. }
  48. public static String getExponentiations(int start, int stop, int exponent) {
  49. StringBuilder builder = new StringBuilder();
  50. for(int i =start;i <= stop;i++)
  51. {
  52. int newPower = (int)Math.pow(i,exponent);
  53. builder.append(newPower);
  54. }
  55. return builder.toString();
  56. }
  57. }