I wrote a program to get first 100 no. which are divisible by 3. Heres the code

import java.io.*;
class app
{
	public static void main (String args[])
	{
		for(int i=0; i<=100; i++)
		{
			int x;
		try{
			BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
			System.out.println("Enter your no.");
			x= Integer.parseInt(br.readLine());
			if(x%3==0)
			{
				System.out.println("The no. you entered is divisible by 3");
			}
			else
			{
				System.out.println("Not");
			}	
	
		}
		catch(Exception e)
		{
			System.out.println("Problem with the program");
		}
	}	}

}

Somehow it is not working please provide with the correct program

Recommended Answers

All 3 Replies

Now what i did is
after declaring integer x inside the for loop i simply said x=i and its working but the program is not terminating please provide me help

Funny, your summary of the assignment suggested to me that you wanted to print out the first 100 numbers that are evenly divided by three -that is, the list [3, 6, 9, 12...294,297,300]

Are you sure that's not the assignment? What you're doing is interesting, but I can't imagine anyone giving you the assigmnent as you've coded it.

What errors do you get?
Also it is good to declare this: BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); outside the for loop.

Also you'd better add this in order to know what was the problem:

catch(Exception e)
{
  System.out.println("Problem with the program: "+e.getMessage());
}
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.