Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. package exercises5;
  2.  
  3. public class Exercise8 {
  4. /*
  5. *Write a program that calculates the numeric centre
  6. *between 1 and N. A numeric centre is a number that splits
  7. *a number series starting on 1 into 2 series whose additions are equal
  8. */
  9. public static void main(String[] args) {
  10. // TODO Auto-generated method stub
  11. int ask, totalsum, sumtick, sum, totaltick;
  12. System.out.print("Enter a number to check if it has a numeric centre: ");
  13. ask= Console.readInt();
  14. totaltick= 1;
  15. totalsum= 0;
  16. while (totaltick <= ask) {
  17. totalsum+= totaltick;
  18. totaltick++;
  19. }
  20. sumtick= 1;
  21. sum= 0;
  22. while (sum != (((totalsum - sumtick)/2)) && sumtick != ask) {
  23. sum+= sumtick;
  24. sumtick++;
  25. }
  26. if (sum == ((totalsum - sumtick)/2)) {
  27. System.out.println("The numeric centre is " + sumtick);
  28. } else {
  29. System.out.println("The number has no numeric centre");
  30. }
  31. }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement