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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: How to find the nth number?

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

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


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default 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?
    Improving the world one idiot at a time!

  3. #3
    Member
    Join Date
    Oct 2013
    Location
    Saint-Petersburg, Russia
    Posts
    33
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default 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

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: How to find the nth number?

    Quote Originally Posted by rodiongork View Post
    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.
    Improving the world one idiot at a time!

  5. #5
    Member
    Join Date
    Oct 2013
    Location
    Saint-Petersburg, Russia
    Posts
    33
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default 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?

Similar Threads

  1. how to find number of different combination of 100
    By freakycoder in forum Algorithms & Recursion
    Replies: 3
    Last Post: January 26th, 2013, 11:16 PM
  2. The nth highest number from a list
    By sbjibo in forum What's Wrong With My Code?
    Replies: 6
    Last Post: April 30th, 2012, 11:20 AM
  3. Find the two largest number
    By vendettabf in forum What's Wrong With My Code?
    Replies: 15
    Last Post: December 29th, 2011, 01:23 PM
  4. Find total number less than average
    By maximus20895 in forum Collections and Generics
    Replies: 2
    Last Post: December 1st, 2010, 01:46 PM
  5. Replies: 4
    Last Post: June 10th, 2009, 01:04 AM