Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: How to find the nth number?

1. ## How to find the nth number?

What is the program about?

This program has 2 tasks.
One is to print all the even numbers from 0 to 100
Second is to enter a number between 1 to 10 and have the program print every nth number between 0 and 100. For example, if you enter 5 then 0, 5, 10, 15, 20 .. 95, 100 is printed.

What is the problem?

I already completed task one. And second task I think I am almost done is to print the nth number but I need the formula that finds the nth number.
So how can that be done.

```import java.util.Scanner;
public class EvenNums {
public static void main( String args[])
{
int evenNum=2;
int number;
int i;
Scanner s = new Scanner(System.in);
for(evenNum=2;evenNum<=100;evenNum+=2)
System.out.print(evenNum + " ");

System.out.print("\nEnter a number between 1 to 10 to get nth result:");
number = s.nextInt();
while (number >= 1 && number <= 10)
for (i=0; i<=100; i++){

{
System.out.println(number);
}
}
}
}```  Reply With Quote

3. ## Re: How to find the nth number?

Look at the first loop, how did you get it to skip all the odd numbers? How do you think you can apply this same approach to the second loop?  Reply With Quote

4. ## Re: How to find the nth number?

How about multiplying N by K to find what is N-th number with step of size K?

More commonly it is a "linear dependence". You always can find a formula if you have only two values for it (for example 5 for N=1 and 15 for N=3). I've once tried to provide a small exercise on that topic at my site: Linear Function  Reply With Quote

5. ## Re: How to find the nth number? Originally Posted by rodiongork How about multiplying N by K to find what is N-th number with step of size K?
Why do you need to do any multiplication? The second half of that sentence is all that is needed.  Reply With Quote

6. ## Re: How to find the nth number?

Why do you need to do any multiplication?
To find N-th number with step of size K one can either add K to starting value N times or simply multiply one by another. What's wrong?  Reply With Quote