Thread: square of asterisk

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    82

    square of asterisk

    a simple program to print a square of asterisk, side is entered by the user. i wrote it without a function, but i can't do it with a function. what is the return type of the function? void or char? with void i use printf('*'); but then in the main function what should i use to print the square?

    Code:
    #include <stdio.h>#include <math.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    char print(int x);
    
    
    int main() {
        int x1;
        scanf_s("%d",&x1);
        printf("%s",print(x1));
        _getch();
        return 0;
    }
    
    
    char print(int x) {
        int j;
        for(int i = 1; i <= x; i++) {
            for(j = 1; j <= x; j++) {
            return '*';
            }
            if (j==x) return '\n';
        }
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    This is the third thread you've created in an hour, with no responses on the first two.

    Perhaps you should focus on one problem at a time.



    With regard to this program - if you want your function to print the square (as its name implies), you should have your print statement within that function instead of in "main()". Since the function is only responsible for printing, you don't need to return a value from it (make its return type void).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help...draw a square within a square pattern
    By gzmiido in forum C++ Programming
    Replies: 6
    Last Post: 10-27-2013, 10:18 AM
  2. How can I get this asterisk shape?
    By shansajid in forum C Programming
    Replies: 5
    Last Post: 01-01-2013, 07:10 AM
  3. line-square and circle-square intersections
    By tomast in forum Game Programming
    Replies: 1
    Last Post: 11-16-2008, 08:12 AM
  4. Password Asterisk
    By $l4xklynx in forum C Programming
    Replies: 5
    Last Post: 10-30-2008, 03:08 PM
  5. for loops using square roots, square, Cube
    By lotf in forum C Programming
    Replies: 3
    Last Post: 03-21-2004, 04:29 AM