i want to extract

576

to

hundreds = 5
tens = 7
ones = 6

my code does this purpose, but..

h=num/100;
   t=(num-(h*100))/10;
   o=((num-(h*100))-(t*10));

i want to rewrite my code using loop and modulus operator..

help guys. thanks

btw: i'm writing a program that converts numbr to w.ord-/.

Recommended Answers

All 7 Replies

Convert integer number to String. Length of of this String gives you info, how many digits are. Create int[string.length()] array. Use loop to transcribe all values. On fly convert particular values to int.
Find a better solution (using % and / operators).

@scias23 : if I can't get the code from working, pause for a while and ask yourself, "am i coding the right code?"
-- while!

i can't think of a solution that uses the modulo operator

Convert integer number to String. Length of of this String gives you info, how many digits are. Create int[string.length()] array. Use loop to transcribe all values. On fly convert particular values to int.
Find a better solution (using % and / operators).

Hi!
I suggest you should read on this and you will find the solution easily..
But as a help,
You can use % operator to simplify do it.

int a=563%100; //a=63
int b=a%10; //b=3

use % with / and you can simply do the job...

I hope this helps you..

Laboratory:

public class Convert {

    //1. base form
    static int[] convert(int k) {
        int[] result = null;
        return result;
    }

    static int[] convert2(int k) {
        int size = 3;//arbitrary
        int[] result = new int[size];
        return result;
    }

    static int[] convert3(int k) {
        int size = 8;
        int[] result = new int[size];
        for (int i = 0; i < size; i++) {
            result[i] = k % 10;
            // what can i do with k?
        }

        return result;
    }

    public static void main(String[] args) {
        int k = 576;
        int[] result = convert(k);
        //int[] result = convert2(k);
        //int[] result = convert3(k);
        System.out.println(k);
        System.out.println(result[0]);
        System.out.println(result[1]);
        System.out.println(result[2]);
        //System.out.println(result[3]);
    }
}

array is a good place, to store calculated values.
Write once, use many times.
Continue...

i'm writing a program that converts numbr to w.ord-/.

If your final purpose is this then I think it is more simple to convert the no to string and then process it.. It is straight forward.

i'm writing a program that converts numbers to words.. from 0-9999. it's inefficient to convert the number to string for this purpose.

If your final purpose is this then I think it is more simple to convert the no to string and then process it.. It is straight forward.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.