Looping problem in C programming

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
14 replies · 2K views
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:
Physics 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.
 
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?
 
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?
 
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
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!
 
Can you please show me an example?
 
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
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!