Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Code For * Triangle

Posted on 2006-06-23
Medium Priority
342 Views
what will be the code in c/c++ to generate this sequence--

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

0
Question by:sinha_anshul26
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 6
• 3
• 3
• +3

LVL 9

Expert Comment

ID: 16968869
homeWork??
0

LVL 48

Expert Comment

ID: 16968992
printf("                                    *\n");
printf("                                  ***\n");
printf("                                *****\n");
printf("                              *******\n");
printf("                                *****\n");
printf("                                  ***\n");
printf("                                    *\n");
0

LVL 9

Expert Comment

ID: 16969032
C++ ..
cout<<"   *   "<<endl;
cout<<"  ***  "<<endl;
cout<<" ***** "<<endl;
cout<<"*******"<<endl;
cout<<" ***** "<<endl;
cout<<"  ***  "<<endl;
cout<<"   *   "<<endl;

cheers;
0

LVL 15

Expert Comment

ID: 16969142
Try this:

#include <stdio.h>

main()
{
char XXX[] = { 0xBB, 0x90, 0xDF, 0x86, 0x90, 0x8A, 0x8D, 0xDF, 0x90,
0x88, 0x91, 0xDF, 0x97, 0x90, 0x92, 0x9A, 0x88, 0x90,
0x8D, 0x94, 0xD3, 0xDF, 0x86, 0x90, 0x8A, 0xDF, 0x93,
0x9E, 0x85, 0x86, 0xDF, 0x8C, 0x90, 0x9B, 0xDE, 0x00 };
char *pStr;

for (pStr=XXX; *pStr; pStr++)
printf("%c", ~(*pStr));
printf("\n");
}
0

LVL 9

Expert Comment

ID: 16969171
:o)
0

Expert Comment

ID: 16970083
#include <cmath.h>
#include <iostream>

int check=0;
int stars=0;
int spaces=0;
int n=0;
cout << "Enter the number of stars in the center line (odd natural number):";

cin >> n;

for (int line = 1; line <= n; line++)
{
spaces = abs((n+1)/2-line);
stars = n-2*spaces;
for (int x = 1; x <= spaces; x++)
cout << endl;
for (int y = 1; y <= stars; y++)
cout << "*";
cout << endl;

}

0

LVL 18

Expert Comment

ID: 16970427
// printDiamond.cpp : print diamond with asteriscs

#include <stdio.h>

void printStars(int n)
{
int i,j,k;

if (n % 2 != 1)
{
n = n-1;
printf("\nOnly odd numbers allowed. Assuming n = %d\n\n",n);
}
for (i=1; i<=n; i+=2)
{
printf("\t");      // minimum left margin
for (j=n-1; j>i; j-=2)
printf(" ");
for (k=1; k<=i; k++)
printf("*");
printf("\n");
}
for (i=n-2; i>0; i-=2)
{
printf("\t");      // minimum left margin
for (j=n-1; j>i; j-=2)
printf(" ");
for (k=1; k<=i; k++)
printf("*");
printf("\n");
}
}

void main(int argc, char* argv[])
{
printStars(7); // maximum depends on console number of collumns
}
0

LVL 9

Expert Comment

ID: 16970606
In d343seven's code ..
>>#include <cmath.h>
must be #include<cmath>

>> for (int x = 1; x <= spaces; x++)
>>          cout << endl;
must be cout<<" ";

and don't forget to add :
using namespace std; after the header files ..
0

LVL 18

Accepted Solution

Jose Parrot earned 2000 total points
ID: 16970804
Or, to be concise:

// printStarsTrick.cpp : print diamond with asteriscs
//

#include "stdafx.h"
#include <stdio.h>

void printStarsTrick(int n)
{
int i,p1,p2;
char c[200]; // arbitrary, max 199 '*'s

for (i=0; i<n; i++) c[i]=' '; c[i]='\0';
for (p1=p2=n/2;p1>0;c[p1--]=c[p2++]='*',printf("%s\n",c));
c[p1++]=c[p2--]=' ';
for (;p2>n/2;c[p1++]=c[p2--]=' ',printf("%s\n",c));
}

void main(int argc, char* argv[])
{
printStarsTrick(45);

}
0

LVL 18

Expert Comment

ID: 16970829
bpmurray wins.
0

LVL 15

Expert Comment

ID: 16970895
Bows.

I think Jose's is really cool: if that's handed in as homework, the lecturer will give anshul26 the marks he deserves.
0

Author Comment

ID: 16971107
************************************
This comment is NOT for  JOSE PERROT and d343seven
************************************
Hi there,
I am 26 yrs old and a graduate( not in computer science), I am not in school.  and I am not trying to submit my homework.
I was just tring to do some programming and at a point I was stuck, thats why I posted this question here.....but I have got beautifull responce. specially from  bpmurray
I see that  there are many sarcastic, pompus so called "EXPERTS !!".....its highly shameful
Intelligent people are always humble, Its only empty vessels that make sound.

0

LVL 9

Expert Comment

ID: 16971138
So Sorry ..
0

LVL 9

Expert Comment

ID: 16971211
This is another one ..

#include<iostream>
#include<cmath>
using namespace std;

void main()
{
int y= 30;
for(int i = 0; i<ceil(y/2); i++)
{
for(int x = 0; x<floor(y/2)-i; x++)
cout<<" ";
for(int j = ceil(y/2)-i; j<ceil(y/2)+i+1; j++)
cout<<"*";
cout<<endl;
}

for(i=ceil(y/2); i>=0; i--)
{
for(int x = floor(y/2)-i; x>0; --x)
cout<<" ";
for(int j = ceil(y/2)-i; j<ceil(y/2)+i+1; j++)
cout<<"*";
cout<<endl;
}
}

have fun ;o)

0

LVL 15

Expert Comment

ID: 16971221
Well Sinha, I'm delighted that you are so interested in computers that you have set yourself a task that pops up here (and all other similar boards) fairly regularly. If you had presented us with your attempt to solve this problem, you would have received a lot of good advice, ranging from resolving any bugs to suggestions on style, etc. The fact that you just asked for the solution to what to us looks like homework means that you get the response such a request deserves, some of which are extraordinarily humorous (I love AlexFM's).

Just to explain why homework requests are frowned upon and, in fact any "gimme a solution" request is treated this way, is because the asker is supposed to be trying to learn how to program. If we give the answer, you will never learn anything. Actually, to learn how to use some interesting mechanisms in C, you should carefully disect Jose's reply. It's actually a very slick and terse way to solve the problem.

I still think this is homework.
0

Author Comment

ID: 16974917
Thanks for the tip. I will keep that in mind for my future doubts and querries.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, itâ€™s also â€¦
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in aâ€¦
Starting up a Project
###### Suggested Courses
Course of the Month8 days, 11 hours left to enroll