int - How to count to 100 in a different base than 10 (java program) -
import java.util.scanner; public class problem5 { public static void main(string args[]) { system.out.println("base calculator"); system.out.println("input number greater 0 , less 10 use base"); system.out.println("------------------------------------------------------------------"); system.out.println("base: "); //menu scanner input = new scanner(system.in); int base = 0; int basenumber[]; basenumber = new int[1000001]; base = input.nextint(); int remainder, tens; if (base <= 0 || base >=10) { //only working parameter system.out.println("this calculator not work bases less 1 or bases greater 9. please re-run program."); return; } else { system.out.print("(this means base " + base + " calculator. hence, allowed values corresponding base 10 values of 0 through 102 be: "); for(int = 0; i<=101; i++) { //a sample of getting when base inputed;notice how when number gets 40 base 4, not go automatically 100 if(i < base){ basenumber[i] = i; system.out.print(basenumber[i] + ", "); } if (i >= base && < 101){ tens = (i / base) * 10; remainder = % base; basenumber[i] = tens + remainder; system.out.print(basenumber[i] + ", "); } if (i >= base && == 101){ tens = (i / base) * 10; remainder = % base; basenumber[i] = tens + remainder; system.out.println(basenumber[i] + ")"); system.out.println("\n"); } } //how change above code make such when 4 4's show in base 4 fours spot (base 10 tens spot), sees 1 16 , moves base 4 sixteens spot (base 10 hundreds spot)? } } }
output
base calculator input number greater 0 , less 10 use base
base: 4 (this means base 4 calculator. hence, allowed values corresponding base 10 values of 0 through 102 be: 0, 1, 2, 3, 10, 11, 12, 13, 20, 21, 22, 23, 30, 31, 32, 33, 40, 41, 42, 43, 50, 51, 52, 53, 60, 61, 62, 63, 70, 71, 72, 73, 80, 81, 82, 83, 90, 91, 92, 93, 100, 101, 102, 103, 110, 111, 112, 113, 120, 121, 122, 123, 130, 131, 132, 133, 140, 141, 142, 143, 150, 151, 152, 153, 160, 161, 162, 163, 170, 171, 172, 173, 180, 181, 182, 183, 190, 191, 192, 193, 200, 201, 202, 203, 210, 211, 212, 213, 220, 221, 222, 223, 230, 231, 232, 233, 240, 241, 242, 243, 250, 251
the output expected are: 0,1,2,3,10,......,33,100,101,102,103,110 (base 4)
how can modify code above produce expected output?
never reinvent wheel if don't have to...
for (int i=0; i<101; i++) { system.out.print(integer.tostring(i, base)); if (i!=100) { system.out.print(", "); } }
Comments
Post a Comment