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 *n*th number but I need the formula that finds the *n*th number.

So how can that be done.

Code :

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);
}
}
}
}

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?

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

Re: How to find the nth number?

Quote:

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.

Re: How to find the nth number?

Quote:

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?