Win a copy of Functional Reactive Programming this week in the Other Languages forum!

# I want new Java question to Improve my skills

Lakshitha Nuwan
Greenhorn
Posts: 8
I am a beginner. so, i need some questions to improve my skills in for-loop and if else.. please help me..

Ranch Hand
Posts: 40
• 1
Welcome to the Ranch!!! A quick google search came up with this:

http://mathbits.com/MathBits/Java/Looping/MCLooping.htm

But if you are just looking for some challenges, here are a few i can think of:

1) To test both ifs and fors, write a program that counts how many of the first thousand numbers (1-1000) are even. The answer is obvious, but the program might be a tad more difficult.

2) Write a program to print one one, then two twos, then three threes, then four fours, etc.

3) For a more difficult problem, that will involve multiple loops and ifs, repeat number one, only determine how many of the first thousand numbers are divisible by two, three, four, five, ... all theway to a thousand, so that the output ends up looking like this:

Lakshitha Nuwan
Greenhorn
Posts: 8
Answers really challenged to me... but I try to get output as your thought..
Thank you for answers and looking for more challenges.

Lakshitha Nuwan
Greenhorn
Posts: 8
Is this the program for challenge 3?

class Example{
public static void main(String args[]){
int x=1;
int y;

for(int i=0; i<1000; i++){
y=1000/x;
System.out.println(x+" : "+y);
x++;

}

}
}

Ranch Hand
Posts: 40
• 1
That is a god start, but there are two things that I would like to point out. First of all, your for loop includes an incrementer, as it should (that's the i++). What this means is that every time the program runs through this loop, it adds one to i. Therefore, you should not have an additional i++ later in your code. Otherwise, every time the loop iterates (or runs through a cycle), i will be incremented twice, meaning you will skip over half the numbers.

Second, your solution should work, but it is not exactly how I intended for it to be done. You observed that the number of integers less than 1000 that are divisible by a given number is 1000 divided by that number. Had I not included a sample output, however, you would not have been able to make this observation. This may be a little advanced, as I am not sure how in depth you are at this point in Java, but a very operation is the modulo operator: %.

Modulo basically means remainder. For example, 5%2=1, because when 5 is divided by two, the remainder is one. Here are some more samples... hopefully you can catch on, and if not, just google modulo. The easiest way, then, to determine if one number is divisible by another, is to check if one modulo the other is equal to zero, meaning that the second goes into the first a whole number of times. For example:

10%2=0, so 10 is divisible by 2.
190%19=0, so 190 is divisible by 19.
39%3=0, so 39 is divisible by 3.

but,

35%8=3, and since 3 is not zero, 35 is not divisible by 8.

Now, I hope I haven't veered too off topic with this discussion, but chances are if you are learning loops now, you will be learning basic operations, +-/*%, soon. If you need anything else, please don't hesitate to ask!

Lakshitha Nuwan
Greenhorn
Posts: 8
if you leave more hints for these challenges, it will help me to understand what will you observe from it. (I can't get clear idea)
I followed about operators include remainder, Now I can do little things from it.

Henry Wong
author
Marshal
Posts: 21506
84
• 1
Lakshitha Nuwan wrote:I am a beginner. so, i need some questions to improve my skills in for-loop and if else.. please help me..

In my opinion, the best way to improve is to actually do a project. And generally, a complex project is better. This is why I always recommend doing something that you like. When you like something, you are more interested in it, and you have more incentive in succeeding.

For example, if you like puzzles, then how about writing a puzzle solver? Find something that you like, then find a project with it. Could you choose something that is too difficult? Sure, you can, but if it is for something that you like, you'll enjoy working on it, and hence, learn something from the attempt.

Henry

Lakshitha Nuwan
Greenhorn
Posts: 8
Thank you very much..
I try to make star patterns from for loop.

like this

*
**
***
****
*****
*****

*****
****
***
**
*

I can do these only from 1 star and 2 loops..
also try to do 1 star and 1 loop.

Thank you again..

Lakshitha Nuwan
Greenhorn
Posts: 8
I Think I got it.. In this program, had I broke any standard?

Aj Prieto
Ranch Hand
Posts: 75
• 1
Though your code works, there is a much simpler and cleaner solution that utilizes a nested "for" loop, which means a loop within a loop.

Also instead of having to declare a variable "y" and increment it each time it goes through a loop, you could just use the variable that was declared in the loop. See below using your code.

Lakshitha Nuwan
Greenhorn
Posts: 8
got it.. Tank you..

I want more patterns.

Aj Prieto
Ranch Hand
Posts: 75
• 1
Lakshitha Nuwan wrote:got it.. Tank you..

I want more patterns.

When I changed the code in my previous post, I didn't check to see if it worked properly, so it may not work the way it was intended to. I just used it as an example for using the variable declared in the loop.

Perhaps you could try and make an "X" pattern.

Something similar to this. This has a size of 7.

Lakshitha Nuwan
Greenhorn
Posts: 8
"X" pattern really improved my thinking side..
is this the program for "x"?

Aj Prieto
Ranch Hand
Posts: 75
Ok, now try making the "X" and the star patterns with nested loops.

Here is an article I found that may help.
Nested "for" loops