Creating a triangle with for loops
First of all, you need to make sure you're producing the correct number of *
symbols. We need to produce 1, 3, 5 et cetera instead of 1, 2, 3. This can be fixed by modifying the counter variables:
for (int i=1; i<10; i += 2)
{
for (int j=0; j<i; j++)
{
System.out.print("*");
}
System.out.println("");
}
As you can see, this causes i
to start at 1
and increase by 2
at each step as long is it is smaller than 10
(i.e., 1
, 3
, 5
, 7
, 9
). This gives us the correct number of *
symbols. We then need to fix the indentation level per line. This can be done as follows:
for (int i=1; i<10; i += 2)
{
for (int k=0; k < (4 - i / 2); k++)
{
System.out.print(" ");
}
for (int j=0; j<i; j++)
{
System.out.print("*");
}
System.out.println("");
}
Before printing the *
symbols we print some spaces and the number of spaces varies depending on the line that we are on. That is what the for loop with the k
variable is for. We can see that k
iterates over the values 4
, 3
, 2
, 1
and 0
when ì
is 1
,3
, 5
, 7
and 9
. This is what we want because the higher in the triangle we are, the more spaces we need to place. The further we get down the triangle, we less spaces we need and the last line of the triangle does not even need spaces at all.
How to make a triangle using for loop javascript
I'm sure there are better solutions (simply left-padding with spaces comes to mind), but here's the quick and dirty one I created from your own solution.
for (var i = 0; i < 5; i++) {
for (var j = 0; j < i; j++) {
document.write(" ");
}
for (var j = 5; j > i; j--) {
document.write("#");
if (j > i + 1) document.write(" ");
}
document.write('<br/>')
}
https://js.do/code/diamondsinthesky
how do i make a triangle using for loop in javascript which output matches the parameter content
let printTriangle = (num) => {
let result = "";
for ( let i = 0; i < num; i++) {
for (let j = 0; j <= i; j++) {
result += '*';
}
result += "\n";
}
return result;
}
console.log(printTriangle(5));
Drawing a triangle using a for loop
try:
#include <stdio.h>
int main ()
{
int m, n, i, j;
scanf ("%d %d", &m, &n);
for (i=0; i<m; i++)
{
for (j=0; j<n; j++)
{
if(j <= i)
printf ("+");
else
printf ("*");
}
printf ("\n");
}
return 0;
}
This way it will always print n chars and n lines
every line will start with i +'s and end with *
Hollow triangle using while loops python
Slightly changed your script:
H = int(input("Enter height of triangle: "))
C = str(input("Character: "))
if C == "":
C = "*"
rows = 1
count = 0
while rows <= H:
spaces = 0
while spaces <= (H - rows):
print(" ", end="")
spaces += 1
count = 0
while count < 2*rows-1:
count += 1
if count == 1 or count == 2*rows-1 or rows == H:
print(C, end="")
else:
print(" ", end="")
print()
rows += 1
C++ Create Triangles with For Loops
You need to output spaces between the triangles:
#include <iostream>
using namespace std;
int main() {
const int size = 6;
int uzunluk = size;
for (int i = 0; i < size; i++) {
for (int j = 0; j < uzunluk; j++) {
cout << "*";
}
for (int j = 0; j < (size - uzunluk + 1) * 2; j++){
cout << " ";
}
for (int j = 0; j < uzunluk; j++) {
cout << "*";
}
cout << endl;
uzunluk--;
}
cout << endl;
for (int i = 0; i < size; i++) {
for (int j = 0; j <= uzunluk; j++) {
cout << "*";
}
for (int j = 0; j < (size - uzunluk) * 2; j++){
cout << " ";
}
for (int j = 0; j <= uzunluk; j++) {
cout << "*";
}
cout << endl;
uzunluk++;
}
}
Related Topics
Adding Up Bigdecimals Using Streams
Printing Debug Info on Errors with Java 8 Lambda Expressions
"Loading Class Com.Mysql.Jdbc.Driver ... Is Deprecated" Message
How to Remove Leading Zeros from Alphanumeric Text
Why Can't I Declare Static Methods in an Interface
Eclipse Windowbuilder, Overlapping JPAnels
Java Synchronized Block for .Class
How to Format Localdate Object to Mm/Dd/Yyyy and Have Format Persist
How to Create a Standalone .Exe in Java (That Runs Without an Installer and a Jre)
Issues with Swingworker and Jprogressbar
Where Should I Put the Log4J.Properties File
Create a New Line in Java's Filewriter
How to Retrieve Image from Project Folder
Converting Secret Key into a String and Vice Versa