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
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.
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
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.
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
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)
If you already have the code to print the entire triangle it should be rather trivial to limit the output to the desired line.
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
Please edit your post and wrap your code with code tags:
[code=java]
YOUR CODE GOES HERE
[/code]
to get highlighting and preserve formatting.
It defines an int variable: a, evaluates an expression and assigns the value of that expression to a.explain this line (int a=n*(n-1)/2+1;
If you don't understand my answer, don't ignore it, ask a question.
divy (September 10th, 2014)
whats a unit test program ?
how t write it for the above program?
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.
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!