HI
can u plz help me with this?

#include<stdio.h>
#include<conio.h>
void main ()
{
int i,j;
printf ("\t*");
for (i=2; i<=5; i++)
{
i=i+1;
printf ("*",i);
for (i=3; i>=1; i--)
{
printf ("*");
}
printf ("\n");
}
getch();
}

its suppose to give an output in the form of diamond made of *
but its not...
plzz help....

Recommended Answers

All 22 Replies

I don't see you putting any spaces to the left of your rows. C won't automatically middle align it for you...

I don't see you putting any spaces to the left of your rows. C won't automatically middle align it for you...

ok,will it be ok if i use \t in my printf statements??
10:printf ("\t*",i);
13:printf ("\t*");

well there is 1 big problem its output goes on and on without having any end and not in diamond form i just cant understand it...

It may seem strange at first, but try making the diamond yourself. Go through it slowly and see exactly what your thinking is on how to do it. Unless you can understand it yourself, you won't be able to make a computer do it.

It may seem strange at first, but try making the diamond yourself. Go through it slowly and see exactly what your thinking is on how to do it. Unless you can understand it yourself, you won't be able to make a computer do it.

ok thanks but atleast can i have a little hint or my mistake in this program??

printf ("*",i); What is that supposed to do? Read up on printf. Just a hint, it doesn't print "*" i times. And adding a tab before every *? I don't think thats what you want.

I cant do it I think I should give up....

Hey all the beautiful help given to you above should increase the interest in you to build the program.The help given above is splendid read it again.

Well try this:

012345678
0123*5678
012***678
01*****78
0*******8
*********
0*******8
01*****78
012***678
0123*5678
012345678

If you assume the space to be a linear array from 0 to 8 as shown above which can be filled by * or " " then see the pattern you need to fill stars in and " " in and run your code. The above pattern might help you visualize it.

Hey all the beautiful help given to you above should increase the interest in you to build the program.The help given above is splendid read it again.

Well try this:

012345678
0123*5678
012***678
01*****78
0*******8
*********
0*******8
01*****78
012***678
0123*5678
012345678

If you assume the space to be a linear array from 0 to 8 as shown above which can be filled by * or " " then see the pattern you need to fill stars in and " " in and run your code. The above pattern might help you visualize it.

thats a great idea but I m a beginner and I'm still not using arrays just loops.

thats a great idea but I m a beginner and I'm still not using arrays just loops.

That was an idea you can just use loops not arrays ok... Just loops
is enough.
Outer loop to count the rows and inner loop with some trick of yours within a if statement may be to print the *'s and " "s.

I cant do it I think I should give up....

Maybe you should. Programming can be confusing and frustrating. If you can't deal with that even from a simple problem like this, maybe programming isn't for you.

That was an idea you can just use loops not arrays ok... Just loops
is enough.
Outer loop to count the rows and inner loop with some trick of yours within a if statement may be to print the *'s and " "s.

oh ok i got ur point. thanks! that gave me a new hope.

Maybe you should. Programming can be confusing and frustrating. If you can't deal with that even from a simple problem like this, maybe programming isn't for you.

instead of discouraging if u give me some simpler idea i'd be thankfull and let me tell u im a beginner started using c a month b4 and know nothing about it and i'm sure u also had problems in the beginning...

My problem was not with your skill level because yes, I have been there. My problem was with your attitude.

My problem was not with your skill level because yes, I have been there. My problem was with your attitude.

ok, im gonna give it a try. Actually i was stressed.

Hey all the beautiful help given to you above should increase the interest in you to build the program.The help given above is splendid read it again.

Well try this:

012345678
0123*5678
012***678
01*****78
0*******8
*********
0*******8
01*****78
012***678
0123*5678
012345678

If you assume the space to be a linear array from 0 to 8 as shown above which can be filled by * or " " then see the pattern you need to fill stars in and " " in and run your code. The above pattern might help you visualize it.

hey it worked u r really genius u shud be a teacher u have very good method ur idea worked and i did it here it is

#include<stdio.h>
#include<conio.h>
void main ()
{
clrscr();
 for (int i=1; i<=5; i++)
 {
   for (int j=1; j<=5;j++)
   {
     if (i==1 && j==3)
       printf ("\t\t\t*");
     if (i==2 && j==2)
       printf ("\t\t*");
     if (i==2 && j==3)
       printf ("\t*");
     if (i==2 && j==4)
       printf ("\t*");
     if (i==3)
     printf ("\t*");
     if (i==4 && j==2)
     printf ("\t\t*");
     if (i==4 && j==3)
     printf ("\t*");
     if (i==4 && j==4)
     printf ("\t*");
     if (i==5 && j==5)
     printf ("\t\t\t*");
   }
   printf("\n");
 }
 getch();
}

thanks again! If there are any errors then plz tell me...

Okay let me be brief,

1) Don't use conio.h
2) Don't use void main. int main is the standard way
3) so many if else statements? what if you need the diamond to be bigger. The following code is as good as yours

puts("    *");
  puts("   ***");
  puts("  *****");
  puts(" *******");
  puts("*********");
  puts(" *******");
  puts(" *******");
  puts("  *****");
  puts("   ***");
  puts("    *");

Here is a different way of doing it, in c

#include<stdio.h>
#include<math.h>
int main ()
{

  int i, j, n=5; // n controls the size of the diamond. 

  /*Remember that there is a restriction in the number of char the console window can put in a line, more than that the text will be wrapped messing up the diamond*/

  for (i=n; i>-n-1; i--){
    printf ("\n%*c", abs(i)+1, ' '); //Space to be provided
    for (j=2*(n-abs(i))+1;j;j--)
      putchar('*');
  }
  getchar();
  return 0;
}

There are many flaws in your code as the multiple number of if's used and one need to change many lines of the program to get the required output.Try to write program such that number of stars can be varied without going through entire program. There are many ways to code it in a better way one of them already shown above by "Prabakar".
I will just stick to the concept of visualization I told earlier and write program as shown below:

#include<stdio.h>
#define MAXSTAR_LENGTH 9 //Number of stars you want(some odd number)

int main()
{
    int i=0,n,mid=MAXSTAR_LENGTH/2,lcount=0;

    while(lcount<MAXSTAR_LENGTH)
    {
	      for(n=0;n<MAXSTAR_LENGTH;n++)
	      {
			  if(n >= mid-i && n <= mid+i)
			  printf("*");
			  else
			  printf(" ");
	      }
	      printf("\n");
	      lcount++;
	      
              if(lcount<=mid)
	                      i++;
	      else
	                      i--;
    }

    return 0;
}

This is not the best program you can write for the above requisite but this one goes with my earlier idea of a space and filling lines with "*" or " ". So posting this.

thanks again for help...i got u.

not to be discouraging, but to add to death_oclock was saying, programming is not for everybody. everybody can do it but it requires LOTS of time to master. I just finished my first semester for my bachelors and spent 10-30 hours a week on computer science homework for 4 months and i am just getting a grip on understanding the basics of C.

Death_oclock was just implying that the time you take to LEARN C can and most often will be frustrating, if you were truly being serious in regards to giving up on this program that fast due to frustration you might want to consider doing something else with your time. There will be much larger walls to climb then this one, and if you are prepared to climb them then good for you, it is an incredible power to fully understand how to code C or C++. But it will take time to achieve that power.

>>programing is not for everybody... it requires LOTS of time to master

Well, if you ask me, programming is fun. And fun is for everybody.
So, If It requires a lot of time then its just that you are having a lot of fun.
>>the time you take to LEARN C can and most often will be frustrating
And if fun turns frustrating, Dani can help you out:)

>>There will be much larger walls to climb then this one

Yes, Much more fun

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.