473,609 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

program to print a triangle of astericks

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,

Nov 15 '05 #1
16 50437
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

Nov 15 '05 #2

VISHNU VARDHAN REDDY UNDYALA wrote:
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.

Nov 15 '05 #3
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

Nov 15 '05 #4
Here it is. Variable i represents the row and n is the key variable.

#include<stdio. h>

void PrintTriangle(i nt noOfMaxAsterix) ;

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

void PrintTriangle(i nt 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');
}
}

VISHNU VARDHAN REDDY UNDYALA wrote:
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,


Nov 15 '05 #5
"VISHNU VARDHAN REDDY UNDYALA" <uv*****@gmail. com> writes:
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.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #6
void PrintTriangle(i nt 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.

Nov 15 '05 #7
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");
}}

Nov 15 '05 #8
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

VISHNU VARDHAN REDDY UNDYALA wrote:
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)

iD8DBQFDYstkagV FX4UWr64RAv/fAKDgEjxdeDH564 2W/MX0ct6Fz8j8dACg 7Qt0
BxFRlt/IzSFt+qoKGC4Oh2 g=
=+wmS
-----END PGP SIGNATURE-----
Nov 15 '05 #9

nelu yazdi:
void PrintTriangle(i nt 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?


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.

Nov 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2378
by: Alex Vinokur | last post by:
------ foo.cpp ------ #include <iostream> using namespace std; int main() { #define FACTOR 10 for (unsigned long array_size = 1; ; array_size *= FACTOR) { int* p = new int;
8
1908
by: Eric Lilja | last post by:
As the title, says: Why doesn't the following program print Hi Charles<newline> when run? #include <stdarg.h> #include <stdio.h> static void va_arg_example(const char *format, ...) { va_list args; va_start(args, format); printf(format, args);
8
22466
by: Michael A. Covington | last post by:
Is there a way to make a C# program print the date on which it was compiled? Finding the file date of the executable is one way, but it's not foolproof. Thanks!
3
1566
by: C.K | last post by:
Hello all, I am still working on my calendar program, and I have decided that the best way is for my vb program to write html tags into a file and then to print the file from ie6. Everything is fine, the only thing that I would like to do is to have my program print the html file automatically. I saw the printdocument object, but I wanted to know, firstly if it's possible, and secondly if I should use this object and how? Tia
3
3612
by: MzChell06 | last post by:
Hi I need help creating a program recursive triangle in C++. Char is the only built in function and it can contain no loops. Output should be like A A B A A B C B A A B C D C B A
6
2599
by: khajeddin | last post by:
this program print its own source code without opening the source file. would some one please explain me how does it work ,and what what process takes place in this program???? #include <stdio.h> char *program = "#include <stdio.h>%cchar *program = %c%s%c;%cint main()%c{%c%cprintf(program, 10, 34, program, 34, 10, 10, 10, 9, 10, 9, 10, 10,10);%c%creturn 0;%c}%c"; int main() { printf(program, 10, 34, program, 34, 10, 10, 10, 9,...
3
3933
kaleeswaran
by: kaleeswaran | last post by:
i would like to know how to print this number like this... i have tried lot of ways but i can't so help me plzzzzzzzzzzzzzzzzzz 1 10 010 1010 01010 101010
1
4109
by: abil | last post by:
i've already build a program that contain all the price, the change given back to the customer but i dun have no idea which function i should use to do program print report... #include <cstdlib> #include <iostream> #include<iomanip> using namespace std; const int TEH_TARIK = 0; const int NESCAFE = 1; const int HORLICK = 2; const int TEH_O = 3;
2
1297
by: naveen chand | last post by:
this program is related to control spaces
0
8145
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
8588
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...
1
8236
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8410
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
5526
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4037
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
4103
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1690
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1407
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.