0

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

3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by javaAddict
0

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

0

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.

0

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());
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.