hi
I have been trying to build this array program but the result is hurrendous dont know where i have gone.
here is the codes. it is

public static int getLargest(int num[])
    {
        int i, largest=0;

        for(i=1; i<5; i++)
        {
            if(i==0)
            {
                largest = num[i];
            }
            else if(num[i]> largest)
            {
                largest = num[i];
            }
        }
        return largest;
    }

    public static void displayFactors(int num[])
    {
        int i, n;

        for(i=0; i<=5; i++)
        {
            System.out.println("factors of "+num[i]);
        }
        System.out.println("");

        for(n=1; n<=num[i]; n++) the problems is here also 
        {
            if(num[i]%n == 0)
            {
               System.out.println(n);
            }
        }
    }

    public static void reversed(int num[])
    {
        int i;

        System.out.println("\n numbers in reserved orders");

        for(i=5; i>=0; i--)
        {
            System.out.println(num[i]);
        }
    }


    public static void main(String[] args) 
    {
        Scanner userinput = new Scanner(System.in);

        int num[] = new int[6];

        int i;

        for(i=1; i<=5 ; i++)
        {
            System.out.println("the largest number is "+getLargest(num));
        }
        displayFactors(num); the error is here
        reversed(num);
    }

Recommended Answers

All 4 Replies

I would guess that you want one loop to be inside the other:

for(i=0; i<=5; i++)
        {
            System.out.println("factors of "+num[i]);
            System.out.println("");
           for(n=1; n<=num[i]; n++) the problems is here also 
           {
               if(num[i]%n == 0)
               {
                   System.out.println(n);
               }
           }
        }

Otherwise you're printing "factors of..." 5 times and then moving onto a loop where i isn't what you think it is.

ok okay

but, most importantly: we have no idea what you are trying to do with your code, neither do we have an idea what it's doing (wrong).

hi stultuske
nice to hear from you. i was trying to build an array program that display the largest number in an array, then get the factors from the said numbers and also get the reversed from also the said numbers.

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.