473,625 Members | 2,733 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

While loop problem

5 New Member
My task is to take a user input that ranges from 1-50 and use it to create a triangle of asterisks. I.E:

Enter number: 5

*
**
***
****
*****
****
***
**
*

I know that i need 2 seperate embedded while loops and that the first one creates a row of asterisks equal to the row number until you reach the input and the second subtracts 1 asterisk until you reach 0.

I can not figure out what is wrong with my code to do this...can someone help please.

Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2.  
  3. public class triangle {
  4.  
  5.     public static void main(String[] args) {
  6.         Scanner kbd = new Scanner(System.in);
  7.         String asterisk;
  8.         System.out.print("Enter a triangle size (between 1 and 50): ");
  9.         int number = kbd.nextInt();
  10.         asterisk = "*";
  11.         int length = asterisk.length();
  12.         int lines = 0;
  13.         while (lines <= number){
  14.             System.out.println(asterisk);
  15.             lines++;
  16.             while (length <= number) {
  17.                 System.out.print(asterisk);
  18.                 length++;
  19.             }
  20.         }
  21.         while (lines > 0 ) {
  22.             System.out.println(asterisk);
  23.             lines--;
  24.             while (length <= number) {
  25.                 System.out.print(asterisk);
  26.                 length++;
  27.             }
  28.         }
  29.  
  30.  
  31.     }
  32. }
  33.  
  34.  
Mar 2 '09 #1
2 3526
JosAH
11,448 Recognized Expert MVP
This has been discussed before; have a look at this thread.

kind regards,

Jos
Mar 2 '09 #2
bharat bhaskar
1 New Member
Use this code
i am writing this in c language.
#include <stdio.h>
#include<conio. h>

void main()
{
int i,j,k,l,n;

scanf("%d",&n);
k=n;
l= 1
for(i=0;i<=n;i+ +)
{

for(j=0;j<=k;j+ +)
{
printf(" ");
if(j>=k-l)
{
printf("*");
}
}
l= 2*l + 1;
k = n+1;
printf("/n");


}
}
Apr 5 '12 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
1292
by: Bluexcell | last post by:
While: This repeatedly tests the expression and, if it is true, executes the first suite; if the expression is false (which may be the first time it is tested) the suite of the else clause, if present, is executed and the loop terminates. The problem with using While as a scanner loop is the loop cycle is carried out at the natural cycle rate and is fixed, also one would have to define a function that contained a while loop in order to...
9
9180
by: Ben | last post by:
I have two 'Do While Not' statements, that are getting information from the same recordset. If I comment out the first one I can get the results for the second one, and vice-versa. Why is this happening? Is it because it's already at the EOF? If so how do I get it back to the BOF for the 2nd 'Do While Not' statement? '---------------------------------------- 'Create an ADO recordset object Set rs_Report =...
147
10080
by: Michael B Allen | last post by:
Should there be any preference between the following logically equivalent statements? while (1) { vs. for ( ;; ) { I suspect the answer is "no" but I'd like to know what the consensus is
12
1971
by: Howard | last post by:
Hello everyone (total VB.NET beginner here), I'm reading the "SAMS Teach Yourself VB.NET In 21 Days" book, and came across an exercise that I can't get to work. The exercise asks that you create a game that makes the user guess a number from 1-100, and you tell the user "lower" or "higher" as they input their guesses, until they guess the correct number, at which point you then tell the user "Correct". I tried using the While Loop, and...
7
3192
by: DaVinci | last post by:
I am writing a pong game.but met some problem. the ball function to control the scrolling ball, void ball(int starty,int startx) { int di ,i; int dj,j; di = 1; dj = 1; i = starty;
1
1683
by: pauljturner99 | last post by:
Hi, I'm trying to pass a parameter from a for loop to the nested while loop but only the first counter is passed. Here is the code: dim ctr redim ctr(5) ctr(0) = 2 ctr(1) = 4 ctr(2) = 6
14
19407
by: Jan Schmidt | last post by:
Hi, in a nested do-while-loop structure I would like to "continue" the outer loop. With goto this should be no problem in while-loops. However, for do-while I cannot get it to work (without a strange workaround construct): -- do { // ...
2
2066
by: d3vkit | last post by:
Okay so I can NOT get my while loop to work. It's the most confusing thing I've ever come across. It was working fine and then suddenly, nothing. No error. The page just dies. I am using PHP5 with mysql. Same problem in Fx 2.0 and IE 7. I have the query sort of spread out across three pages to make it easier for me to generate it; I have a pagination script which checks what is being accessed from the query string (is it the whole blog or an...
7
2402
by: Problematic coder | last post by:
Dim objdr As Data.OracleClient.OracleDataReader = Nothing Dim objcnn As Data.OracleClient.OracleConnection = Nothing Dim objcom As Data.OracleClient.OracleCommand objcom = New Data.OracleClient.OracleCommand(BuildSQL("Notes", id), objcnn) objdr = objcom.ExecuteReader objdr.Read() If objdr.HasRows Then 'Everything OK so far While objdr.Read() 'Jumps straight to End If here 'do something, it never gets here
5
2217
by: =?Utf-8?B?V2lsbGlhbSBGb3N0ZXI=?= | last post by:
Good evening all, I am trying to write a process that uses a while loop to cycle multiple files from an array throught the StreamReader Process. The whole thing works using: Dim Import_File_Reader As System.IO.StreamReader While ...
0
8253
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8692
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8635
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8497
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7182
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4089
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4192
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2621
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1499
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.