# How to find the nth number?

• October 29th, 2013, 08:23 PM
desiVirus
How to find the nth number?

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.

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

• October 29th, 2013, 11:28 PM
Junky
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?
• October 30th, 2013, 12:18 AM
rodiongork
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
• October 30th, 2013, 01:16 AM
Junky
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.
• October 30th, 2013, 06:18 AM
rodiongork
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?