Thread: Help C program that reads Odd numbers and evens

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    37

    Question Help C program that reads Odd numbers and evens

    This is what i got so far, i hope you can understand my code i don't know if its near right, I'm new at this, this is like my third code.

    C program that reads an odd number between 1 and 19 from user and then displays a diamond with that many rows as below.
    Your printf statements should print a single *, +, - or blank. So maximize your use of nested loops while minimizing printf statemens.
    Odd numbered rows should be drawn by *, even numbered rows should be drawn by +, while the middle raw is drawn by -.

    Suppose user enters number as 9, then your program should display the following diamond.

    *
    +++
    *****
    +++++++
    ---------
    +++++++
    *****
    +++
    *




    Code:
    #include<stdio.h>
    #include<math.h>
    
    int main() 
    {
         
        int i, j;
        
        printf("Enter a Number between 1-19 n\")
        scanf("%d", &number);
        
        for(i=1; i <= 19; i++)
        {
        printf("i=%d ", i);
       
        for(j=1; j <= i; j++) 
        {
         if (i % 2 == 0)
         printf("+ ");
          else
          printf("* ");
          }
          printf("\n");
          
          }
          
          
          return 0;
         
         }

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    You need to take the number entered and divide by 2 and add 1. This will give you the row number of the middle row. then use two "sets" of nested loops...

    Code:
    mid_row = number/2 + 1;
    // Top of diamond
    for (i = 1; i < mid_row; i++) {
      // Insert Correct number of spaces
      printf("%*c", ((mid_row * 2 - 1) - (i * 2 - 1)) / 2, ' ');
    
      for (j = 1; j < i * 2; j++) {
        if (i % 2 == 0) printf("+"); else printf("*");
      }
    
      printf("\n");
    }
    
    // middle row
      for (j = 1; j < mid_row * 2;j++) {
        printf("-");
      }
      printf("\n");
    
    //bottom of diamond
    for (i= mid_row - 1; i > 0; i--) {
      // Insert Correct number of spaces
      printf("%*c", ((mid_row * 2 - 1) - (i * 2 - 1))  /2, ' ');
      for (j = 1; j < i * 2; j++) {
        if (i % 2 == 0) printf("+"); else printf("*");
      }
      printf("\n");
    }
    Note that this is just the loop... you still need to get the number from the user (you should add a test to make sure they entered an odd number), and add the headers and int main etc...

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I like to think of this as always printing a square area. You're either printing a blank char, or you're printing the desired char (*,+, whatever).

    Now every row you print, you just two more char's to be printed until you reach the middle row. Then you start printing two more spaces on each row, until you're done.

    If you come back with another question and code, be sure to show what your current print out looks like. There's no need for us to have to run your program to help you. Help us help you.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    1
    Since the op didnt decide to post a thank you i will

    I needed help with this on my hw for a cs class and found this.

    Thank you for the help

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    37
    Quote Originally Posted by m3rc View Post
    Since the op didnt decide to post a thank you i will

    I needed help with this on my hw for a cs class and found this.

    Thank you for the help

    Hey man what school do you go to? do you go to utsa? If not, never mind.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. classify numbers update-please help
    By BJtoVisualcC++ in forum C++ Programming
    Replies: 13
    Last Post: 06-17-2007, 10:11 PM