Output row of Asterisks using a for-loop
Yes it is :-). You count from 0 to your value, right? So read your int before the loop and use the variable as the loop end condition:
Scanner input = new Scanner(System.in);
int num = input.nextInt();
for(int i =0; i<num; i++) {
System.out.print("*");
}
How do I get a for loop to print out asterisks that form a pyramid with odd numbered rows?
The main issue was not using the row
variable in the condition check of the inner for
loop, since the number of asterisks in each row is related to the row value.
The original logic does the same thing in every inner for
loop, every time. 2*num<13
is basically the same as num<6.5
, or really num<7
for ints, meaning 7 asterisks are printed for each row.
int numberOfRows = 7;
for (int row = 0; row < numberOfRows; row++)
{
for (int num = 0; num < 2 * row + 1; num++)
{
Console.Write("*");
}
Console.WriteLine();
}
FWIW, the only reason I even answered is to provide an example that's easier to read and maintain than the other answers. For example, here the row
variable is named appropriately, illustrated by the use of a variable to contain the number of rows. If you want more rows, change numberOfRows
. If you want a different number of asterisks on each row, change the condition in the inner for
loop.
When dealing with for
loops, it's a good idea to keep the names of variables used in the iterator and condition meaningful and easy to read, so that someone who reads it later (maybe even future you!) can more easily understand the logic.
Produce following asterisk using two for loops
For starters the task can be done using only one loop. For example
#include <iostream>
#include <iomanip>
int main()
{
while ( true )
{
const char c = '*';
std::cout << "Enter a non-negative number (0 - exit): ";
unsigned int n;
if ( not ( std::cin >> n ) or ( n == 0 ) ) break;
std::cout << '\n';
for ( unsigned int i = 0; i < n; i++ )
{
std::cout << std::setw( i + 2 ) << std::setfill( c ) << '\n';
}
std::cout << '\n';
}
return 0;
}
The program output might look like
Enter a non-negative number (0 - exit): 7
*
**
***
****
*****
******
*******
Enter a non-negative number (0 - exit): 6
*
**
***
****
*****
******
Enter a non-negative number (0 - exit): 5
*
**
***
****
*****
Enter a non-negative number (0 - exit): 4
*
**
***
****
Enter a non-negative number (0 - exit): 3
*
**
***
Enter a non-negative number (0 - exit): 2
*
**
Enter a non-negative number (0 - exit): 1
*
Enter a non-negative number (0 - exit): 0
As for your code then the inner loop outputs exactly 7 characters '*'
for (int i = 1;i <= a;i++) {
cout << "*";
}
So what you do is what you get.
You could write the inner loop for example the following way
for (int j = 0;j < i; j++) {
cout << "*";
}
Writing a simple for statement program that shows asterisks
You should rewrite this using two for
loops, once controlling the rows, and the other one controlling the column.
You have 5 rows, and on each row, you have 2, 4, 6, etc... stars.
for(int i = 1; i <= 5; ++i) // five rows
{
for(int j = 1; j <= i * 2; ++j) // we have 2 stars for each row number -> 2, 4, 6, etc...
{
cout << "*";
}
cout << "\n";
}
Try to understand your problem, figure out a solution on paper and then try to implement it, it will be way simpler if you're learning how to program.
Error printing a pyramid pattern with asterisks in C
You got a logic errors for the second for
loop which does not do what you want:
// print stars:
for (j = 0; j < 2 * n + 1; j++) //<-- error #1 - n should be i as it's specific for this row
; //<-- error #2 - this termination is completely wrong, it means this `for` loop doing nothing
{
printf("*");
// go to new row
printf("\n");
}
which should be:
// print stars:
for (j = 0; j < 2 * i + 1; j++)
{
printf("*");
}
// go to new row
printf("\n");
Use of for loops to create shapes with asterisks
for (m = 0; m<10; m++) // this one loops over the rows of the shape
{
for (n = 0; n<m; n++) cout << " "; // to leave spaces before the shape
for (n = 0; n<(19-2*m); n++) cout << "*"; // to fill the shape with *
for (n = 0; n<m; n++) cout << " "; // to leave spaces after the shape
cout << endl; // change line
}
As the guys stated, the last loop is not required to get this particular shape, but since this is for your test study, make sure to understand that too, since in the test, any similar shape could pop-up, which may require all the loops (otherwise, why the teacher put it there?.
Related Topics
How to Open Different Urls At the Same Time by Using Python Selenium
How to Get Max() to Return Variable Names Instead of Values in Python
Pandas - Tokenizing Data Expected 1 Field Saw Multiple
How to Further Filter a Result of Resultset
Python Number With 1000 Separator
Python Sockets Multiple Messages on Same Connection
How to Select Percentage of Rows in Pandas Dataframe
Django: Calling .Update() on a Single Model Instance Retrieved by .Get()
Conda: Remove All Installed Packages from Base/Root Environment
Using a Global Variable With a Thread
Render_Template in Python-Flask Is Not Working
Python Searching for Partial Matches in a List
Why Am I Getting Ioerror: [Errno 13] Permission Denied
Django.Db.Utils.Operationalerror: (1045, Access Denied for User '<User>'@'Localhost'
Finding the Two Closest Numbers in a List Using Sorting