NumberUtilities.java 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. public class NumberUtilities {
  2. public static String getRange(int stop) {
  3. StringBuilder sb = new StringBuilder ();
  4. for (int i=0; i < stop; i++) {
  5. sb.append(i);
  6. }
  7. //to convert back to string from int
  8. return sb.toString();
  9. }
  10. public static String getRange(int start, int stop) {
  11. StringBuilder sb = new StringBuilder ();
  12. for (int i = start; i < stop; i++) {
  13. sb.append(i);
  14. }
  15. return sb.toString();
  16. }
  17. public static String getRange(int start, int stop, int step) {
  18. StringBuilder sb = new StringBuilder ();
  19. //or can use String string " ".
  20. for (int i = start; i < stop; i += step) {
  21. sb.append(i);
  22. }
  23. return sb.toString();
  24. }
  25. public static String getEvenNumbers(int start, int stop) {
  26. StringBuilder sb = new StringBuilder ();
  27. for ( int i = start; i < stop; i ++){
  28. if ( i % 2 == 0)
  29. sb.append(i);
  30. }
  31. return sb.toString();
  32. }
  33. public static String getOddNumbers(int start, int stop) {
  34. StringBuilder sb = new StringBuilder ();
  35. for (int i = start; i < stop; i ++) {
  36. if (i % 2 != 0)
  37. sb.append(i);
  38. }
  39. return sb.toString();
  40. }
  41. public static String getExponentiations(int start, int stop, int exponent) {
  42. StringBuilder sb = new StringBuilder();
  43. for (int i = start; i <= stop; i++) {
  44. sb.append((int)Math.pow(i, exponent));
  45. }
  46. return sb.toString();
  47. }
  48. }