Thread: Simple program

  1. #1
    Registered User
    Join Date
    Nov 2020
    Posts
    31

    Simple program

    Hello, i have this program in c


    Code:
    #include <stdio.h>
    
    int read_data() {
        int i;
        scanf("%d", &i);
        return i;
    }
    
    int main()
    {
        int i = read_data();
    
       
    if (i > 0)
          printf("*");
    
    }
    I have to expand the program (written in blue) to display stars (*) on the screen.
    How many stars shall be output is written in the variable i.

    But how can i do this, i have tried to write it in colour blue but it doesn't worked. When for example the variable i is 5 it should be written *****
    Last edited by jasmin89; 11-04-2020 at 10:46 AM.

  2. #2
    Registered User
    Join Date
    Sep 2020
    Posts
    150
    You need to use a for loop to print your stars.
    for loop in C - Tutorialspoint

  3. #3
    Registered User
    Join Date
    Nov 2020
    Posts
    31
    Thanks, but for me is not really clear how i can make this loop.

    I have tried this:

    Code:
        for (i>0)
    
            printf("*");
    How can i make this loop?

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    Try and put what you are wanting to do in your native language, in pseudocode:

    Maybe:

    while I need to print more stars
    print a star

    or:

    for each star I need to print
    print another star


    Then you need to map that to the language you are using.

  6. #6
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    Try and put what you are wanting to do in your native language, in pseudocode:

    Maybe:

    Code:
    while I need to print more stars
        print a star
    or:

    Code:
    for each star I need to print
        print another star
    Then you need to map that to the programming language you are using.

  7. #7
    Registered User
    Join Date
    Nov 2020
    Posts
    31
    I think i have solved it :

    Code:
    #include <stdio.h>
    
    int read_data() {
        int i;
        scanf("%d", &i);
        return i;
    }
    
    int main()
    {
        int i = read_data(), nStars=i;
    
        for (int i = 0; i < nStars; ++i){
            printf("*");
        }
    }

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    In main, you can simplify this:
    Code:
    int i = read_data(), nStars=i;
    to:
    Code:
    int nStars = read_data();
    For a next step, you should consider that reading an integer from the user could fail due to invalid input, so you might want to handle that in read_data.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Nov 2020
    Posts
    1
    Try this
    Code:
    /******************************************************************************
    
                                Online C Compiler.
                    Code, Compile, Run and Debug C program online.
    Write your code in this editor and press "Run" button to compile and execute it.
    
    *******************************************************************************/
    
    #include <stdio.h>
    
    int read_data() {
        int i;
        printf("Enter a number");
        scanf( "%d", &i);
        return i;
    }
    
    int main()
    {
        int i = read_data();
        //check if i greater than 0
        if (i > 0)
        {
            //loop and print
          for(int j=1; j <=i;j++)
          {
              printf("*");
          }
        }
    
        return 0;
    }
    check sample here online
    GDB online Debugger | Code, Compile, Run, Debug online C, C++

    Basically, you need to use for loop and print *
    Last edited by Salem; 11-05-2020 at 06:13 AM. Reason: Removed spammy URL's

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple program, simple problem
    By KAUFMANN in forum C Programming
    Replies: 5
    Last Post: 02-16-2011, 01:16 PM
  2. simple program, simple error? HELP!
    By colonelhogan44 in forum C Programming
    Replies: 4
    Last Post: 03-21-2009, 11:21 AM
  3. Simple program...simple problem?
    By deadherorising in forum C Programming
    Replies: 2
    Last Post: 03-12-2009, 08:37 PM
  4. Simple program, not so simple problem
    By nolsen in forum C++ Programming
    Replies: 2
    Last Post: 01-18-2008, 10:28 AM
  5. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM

Tags for this Thread