program to print a triangle of astericks

  • Thread starter VISHNU VARDHAN REDDY UNDYALA
  • Start date
V

VISHNU VARDHAN REDDY UNDYALA

Hi,
Could anyone over here, write a program in C using only for loop to
print the following output

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

I tried but could only get one side of the triangle...

Quick reply is appreciated. Thanks,
 
V

VISHNU VARDHAN REDDY UNDYALA

this is my code

#include<stdio.h>
int main()
{
int i,j,k;

for(i=1 ; i<=8 ; i++)
{
for (j=7 ; j>=1 ; j--)
printf(" ");

{for(k=1 ; k<=i ; k=k+1)
printf("*");
printf("\n");}

}
}
please help me in finding where I did wrong. Thanks,
Vishnu
 
N

nelu

VISHNU said:
this is my code

#include<stdio.h>
int main()
{
int i,j,k;

for(i=1 ; i<=8 ; i++)
{
for (j=7 ; j>=1 ; j--)
printf(" ");

{for(k=1 ; k<=i ; k=k+1)
printf("*");
printf("\n");}

}
}
please help me in finding where I did wrong. Thanks,
Vishnu

You also need to print the apropriate amount of space characters before
you start printing stars. You do print stars, but it's always the same
number of stars. Also, your image shows stars like: 1 3 5 7... starting
from one using step 2. This program doesn't do that.
 
V

VISHNU VARDHAN REDDY UNDYALA

hiiii,
can u help in writing tha code
please send me tha code
it is very urgent to me i know i have to do spacing
but i can't maintaining tha loops for that
i am waiting 4 ur reply
thanx
vishnu
 
A

AllaTurca

Here it is. Variable i represents the row and n is the key variable.

#include<stdio.h>

void PrintTriangle(int noOfMaxAsterix);

int main(void)
{
int noOfMaxAsterix = 11;
PrintTriangle(noOfMaxAsterix);
return 0;
}

void PrintTriangle(int noOfMaxAsterix)
{
int n, i, j, k, l;
n = (noOfMaxAsterix - 1) / 2;

for (i = 0; i < n + 1; ++i) {
for (j = 0; j < n - i; ++j)
putchar(' ');
for (k = 0; k < 2 * i + 1; ++k)
putchar('*');
for (l = 0; l < n - i; ++l)
putchar(' ');
putchar('\n');
}
}
 
K

Keith Thompson

VISHNU VARDHAN REDDY UNDYALA said:
can u help in writing tha code
please send me tha code
it is very urgent to me i know i have to do spacing
but i can't maintaining tha loops for that
i am waiting 4 ur reply

Give us your instructor's e-mail address so we can send the code to
him directly. I'm sure you wouldn't want to take credit for somebody
else's work.

Don't worry, we'll be sure to mention your name.
 
N

nelu

void PrintTriangle(int noOfMaxAsterix)
{
int n, i, j, k, l;
n = (noOfMaxAsterix - 1) / 2;

for (i = 0; i < n + 1; ++i) {
for (j = 0; j < n - i; ++j)
putchar(' ');
for (k = 0; k < 2 * i + 1; ++k)
putchar('*');
for (l = 0; l < n - i; ++l)

Do you really need j, k, l? You could've used only one. Also, do you
really need the last for, just to print spaces?

Since you gave him the code you should've also add some comments so if
he doesn't bother to study at least he has some understanding of what's
going on.
 
V

VISHNU VARDHAN REDDY UNDYALA

thanx friends
i am getting output spacing is also correct but new line printing is
not getting properly

main()
{
int i,j,k,l,n,ast;
n=(ast-1)/2;
for(i=0;i<n+1;++i)
{
for(j=0;j<n-i;++j)
printf(" ");
for(k=0;k<2*i+1;++k)
printf("*")
for(l=0;l<n-i;++l)
printf("\n");
}}
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hi,
Could anyone over here, write a program in C using only for loop to
print the following output

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

I tried but could only get one side of the triangle...

Quick reply is appreciated. Thanks,

This is not a C question, and thus is off topic here.

But here's a hint

Assuming that the top line of the triangle is #1, and the next line down is
line #2, try to relate the number of stars in a line to the line number. Also,
try to relate the number of spaces before the first star to the line number.
You should be able to come up with two formula, one for stars and one for
spaces, that vary properly with line number.

Now, re-read the information on printf() format codes, paying particular
attention to the "field width" and "precision" specifiers. See if there is a
way to use the two formula from above to regulate your printf() output.

You should be able to reconstruct the triangle with /one/ for() loop and /one/
printf(), within certain limits of triangle size.



- --
Lew Pitcher

Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.7 (GNU/Linux)

iD8DBQFDYstkagVFX4UWr64RAv/fAKDgEjxdeDH5642W/MX0ct6Fz8j8dACg7Qt0
BxFRlt/IzSFt+qoKGC4Oh2g=
=+wmS
-----END PGP SIGNATURE-----
 
A

AllaTurca

nelu yazdi:
Do you really need j, k, l? You could've used only one. Also, do you
really need the last for, just to print spaces?

Of course only one of j, k, l could be used however in order to
emphasize the usage of for loops and the algorithm, I used them. If I
were assigned to give an executable I would do it as you suggested.
Since you gave him the code you should've also add some comments so if
he doesn't bother to study at least he has some understanding of what's
going on.

I gave him the code but I also wanted him to understand the logic. The
question has a good structure to make somebody learn loops hence I
inforced the reader to understand by tracing what is going on.
 
V

VISHNU VARDHAN REDDY UNDYALA

Hello Everyone over there..thanks for helping me out with the
program...by the way this is not an assignment..its a question in the
text ("C How to learn by Dietal&Dietal), which I was trying to solve
since two days..as me being beginner it took me so long. Anyways, I
appreciate your responce..and hope you will help me in the future, when
i get stuck solving the questions. Thank you,
Vishnu
 
K

Keyser Soze

AllaTurca said:
Here it is. Variable i represents the row and n is the key variable.

#include<stdio.h>

void PrintTriangle(int noOfMaxAsterix);

int main(void)
{
int noOfMaxAsterix = 11;
PrintTriangle(noOfMaxAsterix);
return 0;
}

void PrintTriangle(int noOfMaxAsterix)
{
int n, i, j, k, l;
n = (noOfMaxAsterix - 1) / 2;

for (i = 0; i < n + 1; ++i) {
for (j = 0; j < n - i; ++j)
putchar(' ');
for (k = 0; k < 2 * i + 1; ++k)
putchar('*');
for (l = 0; l < n - i; ++l)
putchar(' ');
putchar('\n');
}
}

When I tried to complete this assignment this is what I came up with.

It works, but I may have misunderstood the OPs request.

I thought he wanted a solution that used only one for loop.

#include <stdio.h>
#include <string.h>

int main(int argc, char* argv[])
{
int l,j;
for(l=j=5; j>=0; --j)
{
printf("%s%s\n",&" "[l-j],&"***********"[(j<<1)]);
}
return 0;
}
 
S

Simon Biber

Lew said:
Assuming that the top line of the triangle is #1, and the next line down is
line #2, try to relate the number of stars in a line to the line number. Also,
try to relate the number of spaces before the first star to the line number.
You should be able to come up with two formula, one for stars and one for
spaces, that vary properly with line number.

Now, re-read the information on printf() format codes, paying particular
attention to the "field width" and "precision" specifiers. See if there is a
way to use the two formula from above to regulate your printf() output.

You should be able to reconstruct the triangle with /one/ for() loop and /one/
printf(), within certain limits of triangle size.

Does this count?

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void draw_triangle(int n)
{
int i;
char *a = malloc(n * 2 + 2);
if(!a) exit(EXIT_FAILURE);
memset(a, '*', n * 2 + 1);
a[n * 2 + 1] = 0;

for(i = 0; i < n; i++)
{
printf("%*s\n", n + i, a + 2 * (n - i));
}

free(a);
}

int main(int argc, char **argv)
{
if(argc == 2)
{
draw_triangle(strtoul(argv[1], 0, 0));
}
else
{
printf("Requires one argument for number of lines\n");
}
return 0;
}

One for loop, one printf statement. There are no limits to triangle size
except available memory. I just think the memset is cheating. Can you do
better?
 
P

pete

VISHNU said:
Hi,
Could anyone over here, write a program in C using only for loop to
print the following output

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

I tried but could only get one side of the triangle...

Quick reply is appreciated. Thanks,

/* BEGIN new.c */

#include <stdio.h>
#include <string.h>

char *str_rev(char *s);

int main(void)
{
char string[] = " ";
size_t n = sizeof string - 1;

fputs(string, stdout);
putchar('*');
str_rev(string);
puts(string);
str_rev(string);
while (n--) {
string[n] = '*';
fputs(string, stdout);
putchar('*');
str_rev(string);
puts(string);
str_rev(string);
}
return 0;
}

char *str_rev(char *s)
{
char *t, swap;
char *const p = s;

if (*s != '\0') {
t = s + strlen(s + 1);
while (t > s) {
swap = *t;
*t-- = *s;
*s++ = swap;
}
}
return p;
}

/* END new.c */
 
N

Niklas Norrthon

VISHNU VARDHAN REDDY UNDYALA said:
Hi,
Could anyone over here, write a program in C using only for loop to
print the following output

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

Difficult with only a for-loop. You need some kind of output-function too.


#include <stdio.h>

int main(void)
{
int i;

for (i = 0; i < 1; ++i) {
printf(" *\n"
" ***\n"
" *****\n"
" *******\n"
" *********\n"
"***********\n");
}
return 0;
}


/Niklas Norrthon
 
A

Arne Schmitz

Keith said:
Give us your instructor's e-mail address so we can send the code to
him directly. I'm sure you wouldn't want to take credit for somebody
else's work.

Don't worry, we'll be sure to mention your name.

Exactly what I thought. :) Why don't people get it that they won't learn
anything that way?

Arne
 
M

Mike Wahler

Arne Schmitz said:
Exactly what I thought. :) Why don't people get it that they won't learn
anything that way?

Too many 'students' are not interested in learning,
but only in acquiring a diploma or certificate.

"i r a progrmr. Sez rite hear on ths papr, see?"

-Mike
 
J

Joe Wright

pete said:
VISHNU said:
Hi,
Could anyone over here, write a program in C using only for loop to
print the following output

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

I tried but could only get one side of the triangle...

Quick reply is appreciated. Thanks,


/* BEGIN new.c */

#include <stdio.h>
#include <string.h>

char *str_rev(char *s);

int main(void)
{
char string[] = " ";
size_t n = sizeof string - 1;

fputs(string, stdout);
putchar('*');
str_rev(string);
puts(string);
str_rev(string);
while (n--) {
string[n] = '*';
fputs(string, stdout);
putchar('*');
str_rev(string);
puts(string);
str_rev(string);
}
return 0;
}

char *str_rev(char *s)
{
char *t, swap;
char *const p = s;

if (*s != '\0') {
t = s + strlen(s + 1);
while (t > s) {
swap = *t;
*t-- = *s;
*s++ = swap;
}
}
return p;
}

/* END new.c */

Or this..

#include <stdio.h>

#define HIGH 6

int main(void) {
char line[HIGH * 2];
int i, j, k, l;
for (i = 0; i < HIGH; ++i) {
l = 0;
j = HIGH - i - 1;
while (j--) line[l++] = ' ';
k = i * 2 + 1;
while (k--) line[l++] = '*';
line[l] = '\0';
puts(line);
}
return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top