Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9204475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:43:11+00:00 2026-06-17T23:43:11+00:00

I want to create a loop in Java that mathematically takes a variable, say

  • 0

I want to create a loop in Java that mathematically takes a variable, say one million, then reduces it by the steps detailed in the program & table below.

package com.sbs.test.maths;

import org.testng.annotations.Test;

public class Maths {
  @Test
  public void test() {
    int i = 1000000;
    System.out.println( i );
    while( i > 1 ) {
      i = reduce( i );
      System.out.println( i );
    }
  }

  private int reduce( int value ) {
    if ( value > 1 && value <= 10 ) value -= 1;
    else if ( value > 10 && value <= 100 ) value -= 10;
    else if ( value > 100 && value <= 1000 ) value -= 100;
    else if ( value > 1000 && value <= 10000 ) value -= 1000;
    else if ( value > 10000 && value <= 100000 ) value -= 10000;
    else if ( value > 100000 && value <= 1000000 ) value -= 100000; 
    else if ( value > 1000000 && value <= 10000000 ) value -= 1000000; 
    else if ( value > 10000000 && value <= 100000000 ) value -= 10000000; 
    return value;
  }
}

i.e.

value     reduce-by   result
1000000 - 100000    = 900000
900000  - 100000    = 800000
...
200000  - 100000    = 100000
100000  - 10000     = 90000
90000   - 10000     = 80000
...
20000   - 10000     = 10000
10000   - 1000      = 9000
9000    - 1000      = 8000
...
2000    - 1000      = 1000
1000    - 100       = 900
900     - 100       = 800
...
200     - 100       = 100
100     - 10        = 90
90      - 10        = 80

Can this be done without a nasty if cascade for detecting if the number is 100, 1000, 10000, 100000 or more?

I found a javascript example that I almost got to work in Java, but it wasn’t working for all variations. I ran the program below, but as you will see, it gets stuck at the point where we need to lose a zero on the end of the reduction.

System.out.println( "----------------------------------------------" );
for ( int i = 1000000; i > 0; i -= (int) Math.pow( 10, Math.floor( Math.log( i ) / Math.log(10) ) ) ) {
  System.out.println( i );
}

The output:

1000000
900000
800000
700000
600000
500000
400000
300000
200000
100000

Unfortunately maths is not my strong point, so any help on this is much appreciated.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T23:43:12+00:00Added an answer on June 17, 2026 at 11:43 pm
        int total = 1000000;
        int reducedBy = 100000;
        while (total > 0 && reducedBy >= 1) {
            System.out.println(total);
            total -= reducedBy;
            if (total == reducedBy)
                reducedBy /= 10;
        }
        System.out.println(total);
    

    In case you still need it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

let's say that I want to create a new string during a for loop,
I want to create a loop that will iterate over several strings, but unable
I am playing with Java and want to do a simple while loop that
In Java (Android), is there a way to create a loop that goes through
in my python code i want to create a list of lists...in a loop
I want to use nokogiri to loop through a html and create an object
I want create wordpress website into which I want create user management... That means
i want create a custom json data from the mssql 2008 results so that
I want create a excel with Apache POI in java and I must insert
I want to create a basic side-scrolling game in Java like the 1995 game

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.