12 Replies - 1910 Views - Last Post: 15 April 2010 - 05:44 AM Rate Topic: -----

#1 muhammadlodhi  Icon User is offline

  • New D.I.C Head

Reputation: -16
  • View blog
  • Posts: 26
  • Joined: 14-April 10

Print Asterisks

Posted 14 April 2010 - 12:19 PM

i need your help in program only in C CODE user press only odd numbers then the following result has been shown like

if 3 then

***
* *
***

if 5 then
*****
* *
*****
* *
*****

in even rows the only 1st and last star shoud be prints.

This post has been edited by muhammadlodhi: 14 April 2010 - 12:25 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Print Asterisks

#2 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Print Asterisks

Posted 14 April 2010 - 12:21 PM

We don't do your homework. We need to see your work first. Post the current code in between code tags (press on "<>" symbols when inserting code), and tell us the problem. If you get syntax errors, post them aswell.
Was This Post Helpful? 1
  • +
  • -

#3 muhammadlodhi  Icon User is offline

  • New D.I.C Head

Reputation: -16
  • View blog
  • Posts: 26
  • Joined: 14-April 10

Re: Print Asterisks

Posted 14 April 2010 - 12:26 PM

View Postsarmanu, on 14 April 2010 - 11:21 AM, said:

We don't do your homework. We need to see your work first. Post the current code in between code tags (press on "<>" symbols when inserting code), and tell us the problem. If you get syntax errors, post them aswell.

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;


 }


}

This post has been edited by JackOfAllTrades: 14 April 2010 - 12:41 PM
Reason for edit:: Added code tags

Was This Post Helpful? 0
  • +
  • -

#4 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Print Asterisks

Posted 14 April 2010 - 12:36 PM

I have commented the code, and also I added good indenting. Please take a look over it:
#include <stdio.h>

int main()
{
	int num, i;
	printf("press any odd num");
	scanf("%d", &num);
	if (num % 2 == 0)
		printf("use odd number thanks");
	// We need an else here, since you want this block to be executed
	// only when an odd number was entered:
	else 
	{
	    // This loop tracks the number of rows:
	    for (i = 1; i <= num; i++)
	    {
		// Let's analyze the pattern. Take an example, let's say num is 3.
		// This needs to be printed:
		// ***
		// * *
		// ***
		// So, we see that on odd positions (rows), we need to print the amount
		// of * equal with 3 (num). So, handle this part here.
		if (i % 2 == 1) // If position is odd
		{
			// Print "*" for "num" times:
			for (int i = 1; i <= num; i++)
				printf("*");
			printf("\n"); // Print a newline for good looking output
	        }
		else // In case we are here, the position is even
		{
		     // Print the first *
		     printf("*");
		     // Loop from 2 (because the first character was already printed) to
		     // num - 1 to print the needed amount of spaces:
		     for (int i = 2; i <= num - 1; i++)
			  printf(" ");
		     printf("*\n"); // Printed spaces, let's output the *
		}
	   }
      }

     return 0;
} 


Was This Post Helpful? 0
  • +
  • -

#5 muhammadlodhi  Icon User is offline

  • New D.I.C Head

Reputation: -16
  • View blog
  • Posts: 26
  • Joined: 14-April 10

Re: Print Asterisks

Posted 14 April 2010 - 12:57 PM

View Postsarmanu, on 14 April 2010 - 11:36 AM, said:

I have commented the code, and also I added good indenting. Please take a look over it:
#include <stdio.h>

int main()
{
	int num, i;
	printf("press any odd num");
	scanf("%d", &num);
	if (num % 2 == 0)
		printf("use odd number thanks");
	// We need an else here, since you want this block to be executed
	// only when an odd number was entered:
	else 
	{
	    // This loop tracks the number of rows:
	    for (i = 1; i <= num; i++)
	    {
		// Let's analyze the pattern. Take an example, let's say num is 3.
		// This needs to be printed:
		// ***
		// * *
		// ***
		// So, we see that on odd positions (rows), we need to print the amount
		// of * equal with 3 (num). So, handle this part here.
		if (i % 2 == 1) // If position is odd
		{
			// Print "*" for "num" times:
			for (int i = 1; i <= num; i++)
				printf("*");
			printf("\n"); // Print a newline for good looking output
	        }
		else // In case we are here, the position is even
		{
		     // Print the first *
		     printf("*");
		     // Loop from 2 (because the first character was already printed) to
		     // num - 1 to print the needed amount of spaces:
		     for (int i = 2; i <= num - 1; i++)
			  printf(" ");
		     printf("*\n"); // Printed spaces, let's output the *
		}
	   }
      }

     return 0;
} 



the out put is showing only in one line of odd numbers like if i press 5 then it showing ***** ina row only thats it.
Was This Post Helpful? 0
  • +
  • -

#6 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Print Asterisks

Posted 14 April 2010 - 01:00 PM

Err, what?? Did you modify the code somehow? Because if I enter 5, the output is this:
*****
*   *
*****
*   *
*****


and if I enter 3 for example:
***
* *
***


Isn't this what you want?
Was This Post Helpful? 0
  • +
  • -

#7 muhammadlodhi  Icon User is offline

  • New D.I.C Head

Reputation: -16
  • View blog
  • Posts: 26
  • Joined: 14-April 10

Re: Print Asterisks

Posted 14 April 2010 - 01:04 PM

View Postsarmanu, on 14 April 2010 - 12:00 PM, said:

Err, what?? Did you modify the code somehow? Because if I enter 5, the output is this:
*****
*   *
*****
*   *
*****


and if I enter 3 for example:
***
* *
***


Isn't this what you want?

yes i absolutely want this but compiler is showing mw the following output for 3 *** just in a row only and for 5 ***** not showing as i want and as u show above :(
Was This Post Helpful? 0
  • +
  • -

#8 muhammadlodhi  Icon User is offline

  • New D.I.C Head

Reputation: -16
  • View blog
  • Posts: 26
  • Joined: 14-April 10

Re: Print Asterisks

Posted 14 April 2010 - 01:20 PM

View Postmuhammadlodhi, on 14 April 2010 - 12:04 PM, said:

View Postsarmanu, on 14 April 2010 - 12:00 PM, said:

Err, what?? Did you modify the code somehow? Because if I enter 5, the output is this:
*****
*   *
*****
*   *
*****


and if I enter 3 for example:
***
* *
***


Isn't this what you want?

yes i absolutely want this but compiler is showing mw the following output for 3 *** just in a row only and for 5 ***** not showing as i want and as u show above :(

man help me plz u have understand this my compiler is not given me the answer as u say i think there is some mistakes we are making
Was This Post Helpful? 0
  • +
  • -

#9 muhammadlodhi  Icon User is offline

  • New D.I.C Head

Reputation: -16
  • View blog
  • Posts: 26
  • Joined: 14-April 10

Re: Print Asterisks

Posted 14 April 2010 - 01:32 PM

i made it with someone helps but it is not showing the correct answer as i want pbl is this if i press any odd number like 5 the answer should be a like this

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

but my answer is wrong like in a row printing ***** stars



#include <stdio.h>

int main()
{
        int num, i;
        printf("press any odd num");
        scanf("%d", &num);
        if (num % 2 == 0)
                printf("use odd number thanks");
        else 
        {
           
            for (i = 1; i <= num; i++)
            {
                
                if (i % 2 == 1) // If position is odd
                {
  
                        for (int i = 1; i <= num; i++)
                                printf("*");
                        printf("\n"); // Print a newline for good looking output
                }
                else 
                {
                     
                     printf("*");

                     for (int i = 2; i <= num - 1; 
                          printf(" ");
                     printf("*\n"); 
                }
           }
      }

     return 0;
}



Admin Edit: Please use code tags when posting your code. Code tags are used like so => :code:

Thanks,
PsychoCoder :)
Was This Post Helpful? 0
  • +
  • -

#10 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Print Asterisks

Posted 14 April 2010 - 01:37 PM

Topics merged, please don't create duplicate topics.
Was This Post Helpful? 0
  • +
  • -

#11 muhammadlodhi  Icon User is offline

  • New D.I.C Head

Reputation: -16
  • View blog
  • Posts: 26
  • Joined: 14-April 10

Re: Print Asterisks

Posted 14 April 2010 - 03:32 PM

View Postmuhammadlodhi, on 14 April 2010 - 12:32 PM, said:

i made it with someone helps but it is not showing the correct answer as i want pbl is this if i press any odd number like 5 the answer should be a like this

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

but my answer is wrong like in a row printing ***** stars



#include <stdio.h>

int main()
{
        int num, i;
        printf("press any odd num");
        scanf("%d", &num);
        if (num % 2 == 0)
                printf("use odd number thanks");
        else 
        {
           
            for (i = 1; i <= num; i++)
            {
                
                if (i % 2 == 1) // If position is odd
                {
  
                        for (int i = 1; i <= num; i++)
                                printf("*");
                        printf("\n"); // Print a newline for good looking output
                }
                else 
                {
                     
                     printf("*");

                     for (int i = 2; i <= num - 1; 
                          printf(" ");
                     printf("*\n"); 
                }
           }
      }

     return 0;
}



Admin Edit: Please use code tags when posting your code. Code tags are used like so => :code:

Thanks,
PsychoCoder :)

i have made the following code but not getting the answer as i want it is like all stars ina row
      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;

}      }



My final code but not answer

i press 5 result is *****
      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;

}      }


Was This Post Helpful? 0
  • +
  • -

#12 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Print Asterisks

Posted 14 April 2010 - 03:41 PM

#include <stdio.h>

int main()
{
        int num, i;
        printf("press any odd num");
        scanf("%d", &num);
        if (num % 2 == 0)
                printf("use odd number thanks");
        else 
        {
           
            for (i = 1; i <= num; i++)
            {
                
                if (i % 2 == 1) // If position is odd
                {
  
                        for (int i = 1; i <= num; i++)
                                printf("*");
                        printf("\n"); // Print a newline for good looking output
                }
                else 
                {
                     
                     printf("*");

                     for (int i = 2; i <= num - 1; i++)
                          printf(" ");
                     printf("*\n"); 
                }
           }
      }

     return 0;
}



This worked out fine for me. Output is correct. I just changed this: for (int i = 2; i <= num - 1; to this to get it to compile: for (int i = 2; i <= num - 1; i++)

Also, in your way of doing it, you never print a newline, so it'll all be on the same line.

for(j=0;j<num;j++){printf("*"); //why do you print it out here?
if(j ==0 || j == (num-2)) {
printf("*");
}
printf(" ");
}



You really should practice good indentation.
Was This Post Helpful? 0
  • +
  • -

#13 muhammadlodhi  Icon User is offline

  • New D.I.C Head

Reputation: -16
  • View blog
  • Posts: 26
  • Joined: 14-April 10

Re: Print Asterisks

Posted 15 April 2010 - 05:44 AM

View Posterik.price, on 14 April 2010 - 02:41 PM, said:

#include <stdio.h>

int main()
{
        int num, i;
        printf("press any odd num");
        scanf("%d", &num);
        if (num % 2 == 0)
                printf("use odd number thanks");
        else 
        {
           
            for (i = 1; i <= num; i++)
            {
                
                if (i % 2 == 1) // If position is odd
                {
  
                        for (int i = 1; i <= num; i++)
                                printf("*");
                        printf("\n"); // Print a newline for good looking output
                }
                else 
                {
                     
                     printf("*");

                     for (int i = 2; i <= num - 1; i++)
                          printf(" ");
                     printf("*\n"); 
                }
           }
      }

     return 0;
}



This worked out fine for me. Output is correct. I just changed this: for (int i = 2; i <= num - 1; to this to get it to compile: for (int i = 2; i <= num - 1; i++)

Also, in your way of doing it, you never print a newline, so it'll all be on the same line.

for(j=0;j<num;j++){printf("*"); //why do you print it out here?
if(j ==0 || j == (num-2)) {
printf("*");
}
printf(" ");
}



You really should practice good indentation.

the output is is still pending it is not showing the required out put
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1