Looping problem in C programming

In summary, to solve the problem of prompting the user for a number and using it to print "hello world" a given number of times, you need to use the scanf function to get the input from the user and then use a for loop with the input variable as the test condition. This will allow you to print the statement for the desired number of times.
  • #1
Dave Ritche
70
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
  • #2
You need to prompt the user to give you a number, look up the scanf function.
 
  • Like
Likes Dave Ritche
  • #3
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?
 
  • #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.
 
  • #5
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
  • #6
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..
 
  • #7
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.
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
  • #8
Thank
Mark44 said:
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.
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..
 
  • #9
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!
 

What is a looping problem in C programming?

A looping problem in C programming refers to a situation where a program gets stuck in an infinite loop, causing it to continuously execute the same set of instructions without ever terminating.

What causes a looping problem in C programming?

A looping problem in C programming can be caused by a number of factors, including incorrect loop conditions, missing or incorrect loop termination statements, or logic errors in the loop body.

How can I prevent a looping problem in C programming?

To prevent a looping problem in C programming, it is important to carefully check and test your loop conditions and termination statements. It is also helpful to use debugging tools or print statements to track the program's execution and identify any logic errors.

What are some common techniques for troubleshooting a looping problem in C programming?

Some common techniques for troubleshooting a looping problem in C programming include using breakpoints and stepping through the code, reviewing the loop logic, and checking for any unintended changes to loop variables.

Are there any best practices for using loops in C programming to avoid looping problems?

Yes, there are several best practices for using loops in C programming to avoid looping problems. These include initializing loop variables, using appropriate loop control statements, and double-checking the loop conditions and termination statements.

Similar threads

  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
14
Views
188
  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
7
Views
477
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
33
Views
2K
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
22
Views
921
Back
Top