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

Looping problem in C programming

  1. Dec 17, 2015 #1
    I know about For loops but i don't know how to solve a problem like this:
    A program in which a user wants to print a number for specific number of times but this is not initialized i mean user inputs it after the program is compiled?
     
    Last edited by a moderator: Dec 17, 2015
  2. jcsd
  3. Dec 17, 2015 #2
    You need to prompt the user to give you a number, look up the scanf function.
     
  4. Dec 17, 2015 #3
    Thanks!
    Here is the program and my problem:
    #include<stdio.h>
    #include<conio.h>
    Int main(){
    Int a,b;
    printf("how many times you want to print hello world!");
    scanf("%d",&a);
    ..........}
    My question is that how would i set the loop?
    I mean if i enter 20 then how i set this input in the loop?
     
  5. Dec 17, 2015 #4
    Hello!
    I was writing a c program yesterday but was unable to solve the problem.
    My problem is that which and how can i use a loop to print a statement for a given number of times...
    For example writing a c program to print "hello world" and that the user tells how many times he wants to print it?
    Please help!
    Thanks.
     
  6. Dec 17, 2015 #5

    jtbell

    User Avatar

    Staff: Mentor

    If this thread looks a bit confusing so far, that's because part of another thread was merged with this one. Carry on... :smile:
     
  7. Dec 17, 2015 #6
    Thanks..
     
  8. Dec 17, 2015 #7

    Mark44

    Staff: Mentor

    Please use code tags around your code. In other words, put [code=c] at the top, and [/code] at the bottom. I have done this in the code of yours that I copied.
    After the call to scanf(), a is set to the number of times the loop should run. So what should the loop header look like?
    Code (C):
    for ( ?; ?; ?)
    {
       printf("Hello world \n");
    }
     
    Last edited: Dec 17, 2015
  9. Dec 17, 2015 #8
    Thank
     
  10. Dec 17, 2015 #9

    Mark44

    Staff: Mentor

    How about now?
    After the call to scanf(), a is set to the number of times the loop should run. So what should the loop header look like?
    Code (C):
    for ( ?; ?; ?)
    {
       printf("Hello world \n");
    }
    In other words, what should replace the ?'s above?
     
  11. Dec 17, 2015 #10
    T
    That is what confuses me.In for loop,the initialization and how many times the loop should run are done when we set the loop first i.e everything is pre-set but how about setting the loop after getting the input from user?
     
  12. Dec 17, 2015 #11

    Mark44

    Staff: Mentor

    The test condition (the middle expression in the for loop header) doesn't have to include a constant. The middle expression can include a variable.
     
  13. Dec 17, 2015 #12
    Thanks
    Thanks Mark!now i got the point.thank you very much!
     
  14. Dec 17, 2015 #13
    Can you please show me an example?
     
  15. Dec 17, 2015 #14

    Mark44

    Staff: Mentor

    Code (C):
    for (i = end; i > 0; i--)
    {
       printf("%d\t", i);
    }
    If end is set to 6, say, the loop above will print 6<tab>5<tab>4<tab>3<tab>2<tab>1
     
  16. Dec 17, 2015 #15
    Thanks!
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Looping problem in C programming
  1. C++ Program problem (Replies: 4)

Loading...