Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ZionSV

macrumors newbie
Original poster
Dec 1, 2008
9
0
Hello, I am in an intro to programming class and I'm stuck on a problem.

I am supposed to turn any number, i, into base k from 2 - 16

now I can do it with base 1 - 9 with this code


Code:
            int i, k;
            int v = 1;
           

            System.out.print("\n\t Enter an integer: ");
            keysIn = keyboard.next();
            i = Integer.parseInt(keysIn);
            

            System.out.print("\n\t Enter the base: ");
            keysIn = keyboard.next();
            k = Integer.parseInt(keysIn);
            
            
            while (v <= i) {
            	v *= k;
            }
            
            while (v > 0) {
            	
            	if (i < v) {
            		System.out.print(0);
            	}
            	else {
            		
            		int output = (i - (i % v))/v;
            		
            		System.out.print(output);
            		
            		i -= v*output;
            	}
            	
            	v = v/k;
            }

The problem is I don't know how to use the letters A -F for base 10 - 16

Any help would be greatly appreciated, btw sorry this is such a noob question, I'm not very good at this yet.. :\
 

ZionSV

macrumors newbie
Original poster
Dec 1, 2008
9
0
Hahaha just looking for some help is all :D

thanks, by the way! I haven't tried to use your hint yet but thanks in advance
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.