Looping problem in C programming

Click For Summary

Discussion Overview

The discussion revolves around a problem in C programming related to using loops to print a statement a user-defined number of times. Participants explore how to implement a loop that takes user input for the number of iterations after the program has started.

Discussion Character

  • Technical explanation
  • Homework-related
  • Exploratory

Main Points Raised

  • One participant expresses confusion about how to set up a loop to print a statement based on user input after the program is compiled.
  • Another participant suggests using the scanf function to prompt the user for input.
  • Several participants inquire about the structure of the for loop, specifically how to replace placeholders in the loop header with appropriate values based on user input.
  • A participant clarifies that the test condition in the for loop can include a variable rather than a constant, which helps to address the confusion about loop initialization.
  • Examples of loop structures are provided, with one participant sharing a countdown loop as an illustration.

Areas of Agreement / Disagreement

Participants generally agree on the use of scanf for user input and the flexibility of the for loop structure, but there remains some confusion about the specifics of implementing the loop based on user input. The discussion does not reach a consensus on a single solution.

Contextual Notes

Some participants express uncertainty about how to structure the loop correctly after receiving user input, indicating a need for further clarification on loop initialization and conditions.

Dave Ritche
Messages
70
Reaction score
6
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:
Technology news on Phys.org
You need to prompt the user to give you a number, look up the scanf function.
 
  • Like
Likes   Reactions: Dave Ritche
newjerseyrunner said:
You need to prompt the user to give you a number, look up the scanf function.
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?
 
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.
 
If this thread looks a bit confusing so far, that's because part of another thread was merged with this one. Carry on... :smile:
 
  • Like
Likes   Reactions: Dave Ritche
jtbell said:
If this thread looks a bit confusing so far, that's because part of another thread was merged with this one. Carry on... :smile:
Thanks..
 
Please use code tags around your code. In other words, put
C:
 at the top, and
at the bottom. I have done this in the code of yours that I copied.
Dave Ritche said:
Thanks!
Here is the program and my problem:
C:
#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?
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?
C:
for ( ?; ?; ?)
{
   printf("Hello world \n");
}
 
Last edited:
  • Like
Likes   Reactions: Dave Ritche
Thank
Mark44 said:
Please use code tags around your code. In other words, put
C:
 at the top, and
at the bottom. I have done this in the code of yours that I copied.
Dave Ritche said:
Thanks!
Here is the program and my problem:
C:
#include<stdio.h>
#include<conio.h>
int main(){
   int a,b;
   printf("how many times you want to print hello world!");
   scanf("%d",&a);
   ...
}
.
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?
C:
for ( ?; ?; ?)
{
   printf("Hello world \n");
}
Thanks but i could'nt understand it..
 
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?
C:
for ( ?; ?; ?)
{
   printf("Hello world \n");
}
In other words, what should replace the ?'s above?
 
  • #10
T
Mark44 said:
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?
C:
for ( ?; ?; ?)
{
   printf("Hello world \n");
}
In other words, what should replace the ?'s above?
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?
 
  • #11
Dave Ritche said:
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?
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.
 
  • Like
Likes   Reactions: Dave Ritche
  • #12
Thanks
Mark44 said:
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.
Thanks Mark!now i got the point.thank you very much!
 
  • #13
Can you please show me an example?
 
  • #14
Dave Ritche said:
Can you please show me an example?
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
 
  • Like
Likes   Reactions: Dave Ritche
  • #15
Mark44 said:
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
Thanks!
 

Similar threads

Replies
38
Views
4K
  • · Replies 43 ·
2
Replies
43
Views
8K
  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
Replies
86
Views
3K
Replies
16
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 25 ·
Replies
25
Views
4K
  • · Replies 14 ·
Replies
14
Views
35K
  • · Replies 5 ·
Replies
5
Views
3K