Reputation: 19
I just started to learn C, and one question in book that I'm using is:
Use nested loops to produce the following pattern:
$
$$
$$$
$$$$
$$$$$
And of course I got stuck.
#include <stdio.h>
int main (void)
{
int i, j;
char ch = '$';
for(i = 1; i < 5; i++)
{
for(j = 1; j <=5; j++)
printf("%c", ch);
printf("\n");
}
return 0;
}
Upvotes: 1
Views: 324
Reputation: 147
Please try this code, If you want to generalized ( means user can give any number for printing the pattern you can take a input from user). like n then replace for(row = 1; row <= 5; row++) to for(row = 1; row <= n; row++).
#include<stdio.h>
int main()
{
int row, col;
for(row = 1; row <=5; row++)
{
for(col = 0; col < row; col++)
printf("$");
printf("\n");
}
return 0;
}
Upvotes: 0
Reputation: 11
#include <stdio.h>
int main (void)
{
for (int i = 1; i <= 6; i++)
{
for (int j = 1; j <= 6; j++)
{
if (i - j >= 0)
printf("$");
}
printf("\n");
}
return 0;
}
Upvotes: -1
Reputation: 146
First some basic about loops is that - When outer loop execute one time then inner loop will complete its whole iteration. in your case - for(i = 1; i < 5; i++) // outer loop for each changing value of i such as i= 1,2,3,4 Inner loop for(j = 1; j <=5; j++) // will complete its whole iteration (i.e 5 times because you are using j=1 to j<=5.
Now the come to your problem with your question is that-
for(i = 1; i < 5; i++) //here is problem this will run only 4 time because i<5, and you require 5 times as according to your output given,replace it with i<=5
{
for(j = 1; j <=5; j++) //here is also because you are using j<=5, as I mention above it will run 5 times for each value of i (in outer loop),so replace j<=5 by j<=i, because for each change value of i, you require same time execution of inner loop to print the value of "ch" variable)
printf("%c", ch);
printf("\n");
}
so here is modified code
int main() {
// your code goes here
int i, j;
char ch = '$';
for(i = 1; i < =5; i++) // outer loop
{
for(j = 1; j <=i; j++) // inner loop
printf("%c", ch);
printf("\n"); // to move on next line after the complition of inner loop
}
return 0;
}
May be this is helpful for you.
Upvotes: -2
Reputation: 350
#include <stdio.h>
int main (void)
{
int i, j;
char ch = '$';
for(i = 1; i < 5; i++)
{
for(j = 1; j <=i; j++)
printf("%c", ch);
printf("\n");
}
return 0;
}
description: first for loop is for printing the row.. and nested for loop is for no of '$' have to print
Upvotes: 0
Reputation: 1
it was simple,why because,in it you just want to print in the form of rows and columns and that should be in increasing order. in the first for loop,you are going to print rows and with included for loop you need to do it for columns.
Upvotes: -2
Reputation: 93556
for( i = 1; i <= 5; i++ )
i-1
(or 1 to i if you prefer). for( j = 0; j < i; i++ )
putchar()
.Upvotes: 0
Reputation: 51029
Since answering this question with code is cheating, here are some hints:
$
s equal to the line number.printf
doesn't add a newline character unless you tell it to, so successive calls to printf
can put characters on the same line.If you have code that doesn't work, post it. We'll be happy to help you fix it.
Edit: Based on your sample code, you have some very small problems.
First, in your outer loop, you want a <=
instead of a <
. That gets you up to 5. Second, in your inner loop, j <= 5
should be j <= i
. Though I would have written the inner loop with j starting at 0 and < i
, that's just a stylistic preference.
The printf("%c", ch)
is equivalent to printf("$")
too, in case you weren't sure.
For reference, here's my first crack at an answer. It's very similar to yours:
#include <stdio.h>
int main()
{
int line, dollar;
for (line=1; line <= 5; line++)
{
for (dollar = 0; dollar < line; dollar++)
{
printf ("$");
}
printf ("\n");
}
return 0;
}
Upvotes: 2
Reputation: 26060
The logic of what you need to do is pretty simple: you need to print 5 rows, where the i
-th row has got i
'$'
.
Pseudo code would look like this:
for any i from 1 to 5:
print '$' times i
print newline
print '$' times i
could look like this:
for any j from 1 to i:
print '$'
It shouldn't be too hard to rewrite this using C syntax.
Upvotes: 4