Looping problem in C programming

  • C/++/#
  • Thread starter Dave Ritche
  • Start date
  • #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:

Answers and Replies

  • #2
1,518
618
You need to prompt the user to give you a number, look up the scanf function.
 
  • Like
Likes Dave Ritche
  • #3
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
jtbell
Mentor
15,637
3,683
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
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
34,158
5,778
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.
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
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.
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
34,158
5,778
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
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
34,158
5,778
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
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
34,158
5,778
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
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!
 

Related Threads on Looping problem in C programming

Replies
2
Views
2K
Replies
4
Views
10K
Replies
1
Views
2K
Replies
3
Views
3K
Replies
29
Views
6K
  • Last Post
Replies
1
Views
1K
  • Last Post
Replies
3
Views
1K
  • Last Post
Replies
14
Views
2K
Replies
12
Views
1K
  • Last Post
Replies
1
Views
2K
Top