whats wrong with my program ?

public class divisible {
public static void main(String args[]) {
int[] divisible;
divisible = new int[] {4,9,25,144};
for (int i=0; i<divisible.length; i++)
{
  if (divisible[i]%5== 0)
  System.out.println(divisible.length);
}

}

I want to print the total elements in an array that are divisible by 5 , how can i do this ?

/* Could be written in the following way */

public class divisible {
	public static void main(String args[]) {
	int[] divisible;
	int toTal=0;
	divisible = new int[] {4,9,25,144};
	System.out.println("The elements divisible by 5 are printed as follows:");
	for (int i=0; i<divisible.length; i++)
		  if (divisible[i]%5== 0){
		  	toTal++;
		  System.out.print(divisible[i] + " ");
		  }
	System.out.println("\nThe total number of the elements divisible by 5 is: " + toTal); 
	}
}

The output is shown as follows:
The elements divisible by 5 are printed as follows:
25
The total number of the elements divisible by 5 is: 1

how your program is working without printing the array length ? and why my program isn't working ?

We have no need to print out the length of the array. The only data printed out in your program are the length of the array rather than the elements divisible by 5. You have no code that printins any elements in the array.
In line 8 I have replaced your “divisible.length” with “divisible” so that your program may print out all the elements divisible by 5.
The modified program is written as follows:

public class divisible {
public static void main(String args[]) {
int[] divisible;
divisible = new int[] {4,9,25,144};
for (int i=0; i<divisible.length; i++)
  if (divisible[i]%5== 0)
  System.out.println(divisible[i] + " ");
	}
}

What do you mean ?
I'm printing the length of an array if the numbers are divisible by 5 but why it is not working ??

public class divisible {
public static void main(String args[]) {
int[] divisible;
divisible = new int[] {4,9,25,144};
for (int i=0; i<divisible.length; i++)
{
  if (divisible[i]%5== 0)
  System.out.println(divisible.length);
}
}

i donot wnat to print those numbers which are divisible by 5, i want to print total elements that are divisible by 5

In your program the last curly brace is missing, resulting in a compiling error.
If you want to print the total number of the elements that are divisible by 5, you have to create a counter to do the job: int counter =0. When scanning the array, if an element divisible by 5 is found you have to make counter one increment: counter++. Finally you may print the value of the counter.

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.