Looping problem in C programming

Click For Summary
The discussion centers on creating a C program that prompts the user for a number and then uses that input to determine how many times to print "Hello World." The user is guided to use the `scanf` function to read the input into a variable, which will dictate the loop's execution. The main question revolves around how to structure the `for` loop using the user-provided input. It is clarified that the loop's initialization and condition can utilize variables, allowing for dynamic control based on user input. An example is provided to illustrate how to set up the loop correctly, demonstrating that the loop can iterate a variable number of times based on the user's input.
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 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 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 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 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 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 ·
2
Replies
38
Views
2K
  • · Replies 43 ·
2
Replies
43
Views
6K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
Replies
86
Views
2K
Replies
16
Views
3K
  • · Replies 14 ·
Replies
14
Views
34K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K