Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package exercises5;
- public class Exercise8 {
- /*
- *Write a program that calculates the numeric centre
- *between 1 and N. A numeric centre is a number that splits
- *a number series starting on 1 into 2 series whose additions are equal
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- int ask, totalsum, sumtick, sum, totaltick;
- System.out.print("Enter a number to check if it has a numeric centre: ");
- ask= Console.readInt();
- totaltick= 1;
- totalsum= 0;
- while (totaltick <= ask) {
- totalsum+= totaltick;
- totaltick++;
- }
- sumtick= 1;
- sum= 0;
- while (sum != (((totalsum - sumtick)/2)) && sumtick != ask) {
- sum+= sumtick;
- sumtick++;
- }
- if (sum == ((totalsum - sumtick)/2)) {
- System.out.println("The numeric centre is " + sumtick);
- } else {
- System.out.println("The number has no numeric centre");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement