i want to put the answer like user enter any odd number then out put will be like this one if 3
***
* *
***
the even rows shows only 1st and last stars LIKE 5
*****
* *
*****
* *
*****

int main(){

int num,i,j,k,l;
printf("press any odd num");
scanf("%d",&num);
if(num%2==0){
printf("use odd number thanks");
}
 for(i=1;i<=num;i++){
 for(j=i;j<=i;j++)
 if(j==1&&j==num){

 printf("*");
 }
 else
 printf(" ");
 getch():
 return 0;


 }


}

Recommended Answers

All 16 Replies

#include <stdio.h>

int main() {

  int num,i, j;
  num = 2;
  while(num % 2 == 0) {
    printf("\nenter any odd num");
    scanf("%d",&num);
   getchar();  //not essential, but good.
 }

  //c'mon, this is C, we count "zero, one, two...", unless there's a
  //good reason for starting elsewhere, start at zero.

  for(i=0;i<num;i++) {  //for every row of *'s
    printf("\n");  
    if(i % 2 == 0)  {     //if the row is even
      for(j=0;j<num;j++)  //print num stars 
         printf("*");
    }
    else {                //row is odd  C odd ;)
      printf("* *");
    }
  }
  getchar();
  return 0;
}

M, don't post multiple threads next time - it's frankly confusing and irritating, and keep a good attitude. You are asking for a favor.

And always use code tags!

I (and others), won't help any more if you don't follow forum rules, and you might even be banned.

Thank You soo much but turbo c compiler is showing result like this if i print 5 then ***** in a row ????

I wrote that using Turbo C compiler and it does what I thought you wanted:

for user enters 5:

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

Do you want spaces between each star in the row of 5?

Do you want to draw a box of stars with rows of stars in the middle of it?

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

means there is an error in my compiler? or what? can you explain?

I'm sure your compiler is OK.

Just show what you want, and use code tags (and be careful, because it shows odd spacing until you post).

i need a spaces in even rows only 1st and last star should print. like 2nd box you have shown exactly like that.

It is showing the same result as i have told you before . if i am pressing 5 then it showing

*****

OK, the change will be right in this part of the code:

else {                //row is odd  C odd ;)
      printf("* *");
    }

you need to add a for(j = 0, j < num; j++) loop right below the "else", line, and change the printf(), so it prints just one *.

Inside the for loop I just mentioned, you need an if statement:

if(j ==0 || j == (num-2)) {
     print a *
   }
    print a space: " "

So that leaves you with a little coding to do, but you should be able to sort that out, in short order.

I did but the answer is same NO change in result all * are in a row :(

I'm sure you didn't.

Post your latest code, and let's see what's up with it.

int main() {



      int num,i, j;

      num = 2;

      while(num % 2 == 0) {

      //printf("\nenter any odd num");

      scanf("%d",&num);

      getchar(); //not essential, but good.

      }



      //c'mon, this is C, we count "zero, one, 

two...", unless there's a

      //good reason for starting elsewhere, start 

at zero.



      for(i=0;i<num;i++){ //for every row of *'s

      printf("\n");

      if(i % 2 == 0) { //if the row is even

      for(j=0;j<num;j++) //print num stars

      printf("*");

      }

      else { //row is odd C odd ;)

      for(j=0;j<num;j++){

     if(j ==0 || j == (num-2)) {

printf("*");

}
 
printf(" ");

}
      }

     getchar();

      return 0;

}      }

This is the program but the answer is still same in row plz chk this and help me thanks .

int main() {



      int num,i, j;

      num = 2;

      while(num % 2 == 0) {

      //printf("\nenter any odd num");

      scanf("%d",&num);

      getchar(); //not essential, but good.

      }



      //c'mon, this is C, we count "zero, one, two...", unless there's a

      //good reason for starting elsewhere, start at zero.



      for(i=0;i<num;i++){ //for every row of *'s

      printf("\n");

      if(i % 2 == 0) { //if the row is even

      for(j=0;j<num;j++) //print num stars

      printf("*");

      }

      else { //row is odd C odd ;)

      for(j=0;j<num;j++){printf("*");
if(j ==0 || j == (num-2)) {
printf("*");
}
printf(" ");
}
      }

     getchar();

      return 0;

}      }

That's one of the kinds of errors that you won't find without your code being properly indented.

I couldn't do it by looking at your code, because it's all over the place in it's indentation style.

Here's how to fix it:
Open up Turbo C and click on "Options", then "Environment", then "Editor".

In the list of Editor Options, uncheck the [] Use tab character box.
Check the [X] Use Autoindent mode, just above that.

Below that list, put in 3 for Tab Size, and tell it to use C for the default compiler.

Then use three spaces for every level of indentation in your code. Be consistent. 2 to 4 spaces, for indentation, is ideal.

Tab char's show up badly on all programming forums I've been to, so use spaces in your code.

Oh yeah! Some code:

#include <stdio.h>

int main() {
  int num,i, j;
  num = 2;
  while(num % 2 == 0) { //
    printf("\nenter any odd num");
    scanf("%d",&num);
    getchar(); //not essential, but good.
 }

 //c'mon, this is C, we count "zero, one, two...", unless there's a
 //good reason for starting elsewhere, start at zero.

  for(i=0;i<num;i++){ //for every row of *'s
    printf("\n");
    if(i % 2 == 0) { //if the row is even
    for(j=0;j<num;j++) //print num stars
      printf("*");
    }
    else { //row is odd C odd ;)
      for(j=0;j<num;j++){
        if(j ==0 || j == (num-2)) {
          printf("*");
        }
        printf(" ");
      }
    }
  }
  getchar();
  return 0;
}

It is had to tell with the indenting all messed up, but it looks like the code has the "return" statement inside the "for" loop, so it returns early, after printing the first line.

It is correct for the first line to have exactly 5 ***** on it.
It is not correct to return inside the for loop.

Thanks alott it has done by your help really i was making mistake which i caught thanks alott and thanks for all suggestion and i am following them and got the good result.

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.