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 11 of 11

Thread: floyd triangle

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default floyd triangle

    Write a program that accepts the line number from the user and prints only that particular line from the Floyd triangle.

    Example:
    Input: 2
    Output: 2 3

    Input: 3
    Output: 4 5 6


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: floyd triangle

    Did you have any specific java programming questions? That sounds like a homework assignment. We'll help you with any java programming problems writing the program.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2014
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: floyd triangle

    no my assignment is the above given problem. i'm getting a triangle but not getting a single line which is required. pl do help

  4. #4
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: floyd triangle

    Post any code you have so far and explain where in the code you need the help.
    People will be able to help you easier if they have something visual to look at .

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  5. #5
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: floyd triangle

    If you already have the code to print the entire triangle it should be rather trivial to limit the output to the desired line.

  6. #6
    Junior Member
    Join Date
    Sep 2014
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: floyd triangle

    import java.util.Scanner;

    class FloydTriangle
    {
    public static void main(String [] args)

    {
    int n, i, c;


    Scanner in = new Scanner(System.in);

    System.out.println("Enter the number of rows of Floyd's triangle to print\n");
    n = in.nextInt();

    int a=n*(n-1)/2+1;
    for (c = 1; c <= n; c++)
    {
    System.out.print(a+"");
    a++;
    }
    System.out.println("\t");
    }
    }
    i have the code.can someone explain this line (int a=n*(n-1)/2+1

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: floyd triangle

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.

    explain this line (int a=n*(n-1)/2+1;
    It defines an int variable: a, evaluates an expression and assigns the value of that expression to a.
    If you don't understand my answer, don't ignore it, ask a question.

  8. The Following User Says Thank You to Norm For This Useful Post:

    divy (September 10th, 2014)

  9. #8
    Junior Member
    Join Date
    Sep 2014
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: floyd triangle

    whats a unit test program ?
    how t write it for the above program?

  10. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: floyd triangle

    Do you have code that compiles and executes manually and gives the desired results?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #10
    Junior Member
    Join Date
    Sep 2014
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: floyd triangle

    Quote Originally Posted by Norm View Post
    Do you have code that compiles and executes manually and gives the desired results?

    n i dont have...what is that i could do now ?

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

    Default Re: floyd triangle

    If you have working code that prints out all lines of the triangle all you need to do is add some sort of control that only prints the desired line. What kind of control statement do you think that would be?
    Improving the world one idiot at a time!

Similar Threads

  1. Inverted Right Triangle
    By KillerToFu in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 29th, 2013, 09:12 AM
  2. need help with a mumbers triangle
    By umairbaloch in forum What's Wrong With My Code?
    Replies: 7
    Last Post: December 16th, 2012, 10:05 AM
  3. help me draw a triangle....
    By beandip408 in forum Object Oriented Programming
    Replies: 10
    Last Post: October 28th, 2010, 05:49 PM
  4. [HELP] TRIANGLE!
    By kramista in forum Loops & Control Statements
    Replies: 10
    Last Post: July 29th, 2010, 12:58 PM
  5. Triangle Question
    By Leeds_Champion in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 29th, 2009, 11:33 PM