Home Answers Viewqa Java-Beginners nested for-loop

 
 


simon ledwaba
nested for-loop
1 Answer(s)      a year and a month ago
Posted in : Java Beginners

hi.write a program that will prompt the user for a positive integer(num) and then display two triangles with num number of lines to represent the following patterns of asterisks. the input num must be validated, and if invalid, the user must be allowed a maximum of 3 tries to enter the correct value.

e.g if the user enters 2,the program must display the following patterns:

*

**

**

*

View Answers

April 9, 2012 at 4:24 PM


import java.util.*;
class Triangle 
{
    public static void main(String[] args) 
    {
        Scanner input=new Scanner(System.in);
        System.out.print("Enter positive number: ");
        int num=input.nextInt();
        for(int i=1;i<=num;i++){
            for(int j=1;j<=i;j++){
                System.out.print("*");
            }
            System.out.println();
        }
           for(int i=num;i>=1;i--){
           for(int j=1;j<=i;j++){
               System.out.print("*");
           }
           System.out.println();
       }

    }
}









Related Pages:
nested for-loop
nested for-loop  hi.write a program that will prompt the user for a positive integer(num) and then display two triangles with num number of lines to represent the following patterns of asterisks. the input num must be validated
Nested Loop - Java Beginners
Nested Loop  Hi, Can anyone tell me example of Nested loop in Java? Thanks   Hi friend,public class NestedExample{ public static void main(String[] args){ /** Print Pattern I */ System.out.println("
nested for loops
that the problem lies with my nested for loop which calculates all information...nested for loops  Hello all, I have created a program(gui) that allows a user to input time and minutes in textfields and then calculates
for loop in java
for loop in java  54321 4321 321 21 1 using for loop or nested 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 5 4 3 2 1 4 3 2 1 3 2 1 2 1 1 using for loop or nested... 8 9 6 7 8 9 10 all for loop code plz friends guide me
for loop in java
for loop in java  54321 4321 321 21 1 using for loop or nested 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 5 4 3 2 1 4 3 2 1 3 2 1 2 1 1 using for loop or nested... 8 9 6 7 8 9 10 all for loop code plz friends guide me
for loop in java
for loop in java  54321 4321 321 21 1 using for loop or nested 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 5 4 3 2 1 4 3 2 1 3 2 1 2 1 1 using for loop or nested... 8 9 6 7 8 9 10 all for loop code plz friends guide me
for loop in java
for loop in java  54321 4321 321 21 1 using for loop or nested 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 5 4 3 2 1 4 3 2 1 3 2 1 2 1 1 using for loop or nested... 8 9 6 7 8 9 10 all for loop code plz friends guide me
for loop in java
for loop in java  54321 4321 321 21 1 using for loop or nested 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 5 4 3 2 1 4 3 2 1 3 2 1 2 1 1 using for loop or nested... 8 9 6 7 8 9 10 all for loop code plz friends guide me
For Loop in Java
another loop then the second loop is called nested loop. The Syntax for the For Loop... For Loop in Java - Java For Loop Examples & Syntax       The for loop is the type of  looping
Two Dimensional Array Program Using Nested For Loop
Two Dimensional Array Program Using Nested For Loop ... program. We are going to using for loop for performing the five rows and five columns. Now applying the for loop and define an integer ' i' and it should be either
Nested class
Nested class  What is nested class?  when a class is defined within another class then such a class is called a nested class. Nested... nested classes are: It is a way of logically grouping classes that are only used
nested loops
nested loops  please tell input to get the output: 12345 23456 34567 45678
loop
loop  what is the difference b/w do while,while and for loop..with example
loop
loop  i want to write my name(inder) through loop in java using star
for loop
for loop  what is for loop   Loops are used for iteration... for a number of times it is finite loop and if repetition is done infinitely it is infinite loop. Java provides several loop structures for iteration. Those
For loop
For loop  Hi! Please explain the following loop: for(i=0;i<a[j];i++) THANX!!   Please reply as soon as possible
for loop
for loop  how to performs the for loop when 2 inputs given like displays the triangle shape and how to take initial values or declaration values plz explain in detailed ex. 1 12 123 1234
Loop
to understand a loop my quesiton are: what du i do wrong in my code since.... (made in blueJ) Class Loop public void loopUpGeneral(int start, int stop... dk  Hi, Please check the for loop once again: for (int i
For Loop
For Loop  Write a program using a for loop that computes the following sum: 1/2 + 2/2 + 3/2 + 4/2 + 5/2 + ... N/2 N will be an integer limit the user enters. This is an example of my upcoming quiz. I need help on knowing
Nested try
Nested try  Hi, In eclipse without providing input i am getting different outputs ie my exception gets executed first then finally block and vice versa.pl explain me class Demo { static void nestedTry(String args[]) { try
loop
loop  strong textHello can someone help me with this problem,I need to output the following code using a loop the code is as follows with the user inputs in inverted commas,thanks. ENTER CODE(XX to Stop) CODE:"D24" QUANTITY:"2
for loop
for loop  using only two for loop how can print below numbers 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 6 6 6 6 6 6 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 8 8 8 8 8 8 8 8 7 7 7 7 7 7 7 6 6 6 6 6 6 5 5 5 5
for loop
for loop  using only two for loop how can print below numbers 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 6 6 6 6 6 6 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 8 8 8 8 8 8 8 8 7 7 7 7 7 7 7 6 6 6 6 6 6 5 5 5 5
Nested try
Nested try  Each time when i execute this code in eclipse i get different answers pl help me with this code class Demo { static void nestedTry..., visit the following link: Nested Try Example
PHP While Loop
; We can have nested while loop, we can use the alternative structure...While Control Structure: While is the simplest form of loop. While loop checks..., and become infinite loop. While loop is also known as pre-test loop. Basic
program to create nested tables
program to create nested tables  program to create nested tables
Nested classes: Examples and tutorials
Nested classes: Examples and tutorials       Nested classes Here is another advantage of the Java... within another class, such class is called a nested class. Inner classes can
Nested <c:forEach> in JSTL
Nested <c:forEach> in JSTL   ... and collections. This tag is very much similar to for loop. The tag repeats the body of the tag for each element in the array or collection. The nested <c
The nested class - Java Beginners
The nested class  Hi, What is nested class in Java? What is the benefits of nested class? Write examples of nested class. Thanks   .../java/n/java-nested-class.shtml Thanks
GROUP BY IN NESTED QUERY
GROUP BY IN NESTED QUERY  Hello Every One, Query to use GROUP BY CLAUSE IN NESTED QUERY SELECT STATEMENT in mysql database.If u know please help me.thanks
Java nested class example
Java nested class example  Give me any example of Nested Class.   Nested Class: Class defined within another class is called nested class. Nested class is divided into two parts- 1.Static 2.Non-static Static
Need help with nested queries
Need help with nested queries  Hello, Table1 has "id", "votes" columns. Table2 has "id", "Name" column. I need to retrieve Name and its corresponging Votes. I tried with nested queries but its confusing. Can anyone please help
Nested and Inner classes
Nested and Inner classes  What is the significance of Inner Classes and Static Inner Classes? or Why are nested classes used?   Hi Friend... a nested class. Inner class is a non static class declared inside another class.Non
Nested If Statement
Nested If Statement       In this section you will study about the Nested-if Statement in jsp. Nested If statement means to use the if statement inside the other if statement
Using Nested loops
Using Nested loops  How to use nested loops in java when I want to print the 10 multiples of numbers 2 to 15(in multiplication table)   public class MultiplicationTable{ public static void main(String[] args) { int
Nested Ajax--not getting output
Nested Ajax--not getting output  Hi everyone... I have two.jsp page from that , with the help of ajax i have called three.jsp. So, i have used DIV in my jsp pages . on button click,It gives me my desired answer. Same,in next
Static Nested Classes
Static Nested Classes       A nested class that is declared static is called a static nested class. Memory to the objects of  any static nested classes are allocated
Inner Nested Classes
Inner Nested Classes       Non-static nested classes are slightly different from static nested classes, a non-static nested class is actually associated
how to use group by in nested query
how to use group by in nested query  Hai, How to use group by clause in nested query select statement in subquery i am having where clause.please help me.thanks in advance
nested select tag
nested select tag  My requirement is as follows suppose combobox to select country whenever i select a country it will show corresponding states in another combo box.   JSP Code 1)country.jsp: <%@page import
Nested classes
Nested classes      ... are known as nested classes. Inner classes can be either named... class looks just like any other class, except that it is nested inside another
For..in loop
For..in loop         In this tutorial we will study for..in loop which is similar to for each loop of C#, Java and other popular languages, in this example
How to display nested ArrayList in JSF
How to display nested ArrayList in JSF  I have a class... to display it in jsf but i dont know how and i am getting mad. I have nested... but i dont know how and i am getting mad. I have nested "foreach" tags
Hibernate Criteria Nested Properties
Hibernate Criteria Nested Properties Consider the example based on Hibernate Criteria Nested Properties DetachedCriteria detachedCriteria...); List list = criteria.list(); An example of Hibernate Criteria Nested
Java for loop
Java for loop  Can a for statement loop indefinitely
Mysql Nested Select
Mysql Nested Select       Mysql Nested Select is a SELECT query that is nested within a Select, Update... illustrate an example from 'MySQL Nested Select'. To understand and elaborate example
Mysql Nested Case
Mysql Nested Case       Mysql Nested Case is a Select Query that is nested inside select, update... an example from 'MySQL Nested Case'. To understand and elaborate example we create

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.