Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C program to print a number...

  1. Dec 16, 2015 #1
    I'm having some problems with printing a statement in c programming.
    How do we print astatement for specific number of times as an input from user?
    for example,i want to print "hello world!" on the screen and the program would ask how many times you want to print this on screen?
    Thanks in advance!
     
  2. jcsd
  3. Dec 16, 2015 #2

    jedishrfu

    Staff: Mentor

    Welcome to PF!

    Have you learned about for loops?

    While we can't do your homework for you, I would start there and learn more about "for" loops and how they work.

    You might even be able to find an example online.
     
  4. Dec 19, 2015 #3
    thanks.....
     
  5. Dec 23, 2015 #4

    ChrisVer

    User Avatar
    Gold Member

    I guess some time has passed already... have you found an answer?
    First of all, in such cases the program would have to ask the user for an input variable value. As a result you have to have a variable (int) N which the program asks for a value from the user and then the program does something N times (that's why you'd need a for or a while loop).
    So the idea is to let the program print "give me the number", you give it the number M, and it does something repeatively for M times... several days have passed, so I'm asking you:
    how would you do that in a for loop? how would you do that in a while loop? If you succeeded in those, can you also write a program that does that with a recursive function? Maybe the last is a stupid thing to do for this (or a relatively new concept for you since it concerns functions), but it's fun to do things in different ways and it can also help you understand how recursive methods work in this simple context...
     
  6. Dec 24, 2015 #5
    Thanks for your reply Chris.
    I had found the answer so i'm going to write that program:
    #include<stdio.h>
    #include<conio.h>
    Int main(){
    int start,end,number;
    Printf("enter the starting number");
    //and similarly the program would ask the user to enter the ending point then the loop itself is here:
    for(number<=start;number<=end;number++)
    {printf("hello world!\n");}

    getch();
    }
    I don't know about functions and recursive functions yet.
     
  7. Dec 24, 2015 #6

    ChrisVer

    User Avatar
    Gold Member

    Hey. you can easily get rid of the input starting point... can you think of the reason why?
    Also I'm not used to why you'd put "num<=start" in the very first position of for, instead of just "="...
    The syntax for a for loop is
    for ( variable initialization; condition; variable update ) {...}
    http://www.cprogramming.com/tutorial/c/lesson3.html

    So the very first position is the variable initialization (if you haven't defined its type before it would go as "int i=a" where a an int value). It's not a conditional to get < or >.
     
    Last edited: Dec 24, 2015
  8. Dec 24, 2015 #7

    Mark44

    Staff: Mentor

    Your code above won't compile due to several errors.
    1. "Int main" should be "int main".
    2. "Printf" should be "printf".

    Finally, please use code tags for your code. This means putting a [code=c] tag at the beginning and a [/code] tag at the end. The "=c" part indicates that you're writing C code. Also, use indentation to make your code more readable, as I have done below. Indentation also helps you spot missing braces.
    Code (C):
    #include<stdio.h>
    #include<conio.h>
    int main()
    {
       int start,end,number;
       printf("enter the starting number");
       //and similarly the program would ask the user to enter the ending point then the loop itself is here:
       for(number<=start;number<=end;number++)
       {
          printf("hello world!\n");
       }
       getch();
    }
    With the syntax errors fixed, your code will compile, but it won't run correctly.
    Your code issues a prompt to the user to enter a starting number, but it doesn't actually do any input. As written, start and end are declared, but are not initialized, so their values would be whatever numbers happen to be in memory when the code runs.

    Your code should prompt the user to enter a number for start (which it does), and then should use scanf() to set that variable. Your code should also prompt the user for an end value, and should use scanf() to set the end variable.

    One of the lines in your code is
    Code (Text):
    {printf("hello world!\n");}
    It is very bad practice to have } at the end of the line like that, as it is easy to miss. At first I thought that main() was missing its ending brace.

    Finally, your code, as written, has an Off By One Error (OBOE), as it will print "hello world\n" number + 1 times, one more than you intend.
     
  9. Dec 28, 2015 #8
    Thanks mark and chris!
    I had found the solution and my problem was really solved.
    My code that i compiled is:
    Code (C):

    #include<stdio.h>
    #include<conio.h>
    int main()
    {
    int start,end,loop;
    printf("enter the starting number");
    scanf("%d",&start);
    printf("end point");
    scanf("%d",&end);
    for(loop=start;loop<=end;loop++)
    {
      printf("hello world!/n");
    }
    getch();
    }
     
    I hope there isn't any error.
     
  10. Dec 28, 2015 #9

    ChrisVer

    User Avatar
    Gold Member

    yup, it works...However I don't really understand why would you need the starting point? (except for if your problem asks for you to put it).
    The reason is that if I wanted a repeated print of "Hello World!" 5 times, I wouldn't have to put 0 and 4, or 1 and 5, or 2 and 6... but I would just try to make it work with telling it the number 5...doesn't this sound more logical??:rolleyes:
    Of course starting points are good for example in integrating.
     
  11. Dec 28, 2015 #10

    Mark44

    Staff: Mentor

    Unfortunately, there is still an error, if your intention is to print that "hello world" string a specific number of times. This is the same error I pointed out in post #7, at the bottom.
    You can make your code simpler as follows:
    Code (C):

    #include<stdio.h>
    #include<conio.h>
    int main()
    {
       int num, loop;
       printf("Enter the number of times to print the string: ");
       scanf("%d", &num);
       for(loop = 0; loop < num; loop++)
       {
          printf("hello world!/n");
       }
       getch();
    }
     
    The code above will execute the for loop num times.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook