The discussion centers around printing a statement multiple times in C programming based on user input. The initial query asks how to print "hello world!" a specified number of times. Participants suggest using loops, particularly "for" and "while" loops, to achieve this. Key points include the need for user input to determine how many times to print the statement, with suggestions to use a variable to store this input. A sample code snippet is provided, but it contains several syntax errors and logical issues, such as not initializing variables and an off-by-one error in the loop condition. Participants emphasize the importance of correctly using `scanf()` for input and suggest simplifying the code to directly ask for the number of repetitions. A corrected version of the code is shared, demonstrating proper syntax and logic to print "hello world!" the specified number of times, highlighting the use of a loop that iterates based on user input. The discussion concludes with a focus on improving code readability and functionality.
#1
fraz ali
4
2
I'm having some problems with printing a statement in c programming.
How do we print astatement for specific number of times as an input from user?
for example,i want to print "hello world!" on the screen and the program would ask how many times you want to print this on screen?
Thanks in advance!
I guess some time has passed already... have you found an answer?
First of all, in such cases the program would have to ask the user for an input variable value. As a result you have to have a variable (int) N which the program asks for a value from the user and then the program does something N times (that's why you'd need a for or a while loop).
So the idea is to let the program print "give me the number", you give it the number M, and it does something repeatively for M times... several days have passed, so I'm asking you:
how would you do that in a for loop? how would you do that in a while loop? If you succeeded in those, can you also write a program that does that with a recursive function? Maybe the last is a stupid thing to do for this (or a relatively new concept for you since it concerns functions), but it's fun to do things in different ways and it can also help you understand how recursive methods work in this simple context...
#5
Dave Ritche
70
6
Thanks for your reply Chris.
I had found the answer so I'm going to write that program:
#include<stdio.h>
#include<conio.h>
Int main(){
int start,end,number;
Printf("enter the starting number");
//and similarly the program would ask the user to enter the ending point then the loop itself is here:
for(number<=start;number<=end;number++)
{printf("hello world!\n");}
getch();
}
I don't know about functions and recursive functions yet.
Hey. you can easily get rid of the input starting point... can you think of the reason why?
Also I'm not used to why you'd put "num<=start" in the very first position of for, instead of just "="...
The syntax for a for loop is
for ( variable initialization; condition; variable update ) {...} http://www.cprogramming.com/tutorial/c/lesson3.html
So the very first position is the variable initialization (if you haven't defined its type before it would go as "int i=a" where a an int value). It's not a conditional to get < or >.
Thanks for your reply Chris.
I had found the answer so I'm going to write that program:
#include<stdio.h>
#include<conio.h>
Int main(){
int start,end,number;
Printf("enter the starting number");
//and similarly the program would ask the user to enter the ending point then the loop itself is here:
for(number<=start;number<=end;number++)
{printf("hello world!\n");}
getch();
}
I don't know about functions and recursive functions yet.
Your code above won't compile due to several errors.
1. "Int main" should be "int main".
2. "Printf" should be "printf".
Finally, please use code tags for your code. This means putting a
C:
tag at the beginning and a
tag at the end. The "=c" part indicates that you're writing C code. Also, use indentation to make your code more readable, as I have done below. Indentation also helps you spot missing braces.
C:
#include<stdio.h>
#include<conio.h>
int main()
{
int start,end,number;
printf("enter the starting number");
//and similarly the program would ask the user to enter the ending point then the loop itself is here:
for(number<=start;number<=end;number++)
{
printf("hello world!\n");
}
getch();
}
With the syntax errors fixed, your code will compile, but it won't run correctly.
Your code issues a prompt to the user to enter a starting number, but it doesn't actually do any input. As written, start and end are declared, but are not initialized, so their values would be whatever numbers happen to be in memory when the code runs.
Your code should prompt the user to enter a number for start (which it does), and then should use scanf() to set that variable. Your code should also prompt the user for an end value, and should use scanf() to set the end variable.
One of the lines in your code is
Code:
{printf("hello world!\n");}
It is very bad practice to have } at the end of the line like that, as it is easy to miss. At first I thought that main() was missing its ending brace.
Finally, your code, as written, has an Off By One Error (OBOE), as it will print "hello world\n" number + 1 times, one more than you intend.
#8
Dave Ritche
70
6
Thanks mark and chris!
I had found the solution and my problem was really solved.
My code that i compiled is:
C:
#include<stdio.h>
#include<conio.h>
int main()
{
int start,end,loop;
printf("enter the starting number");
scanf("%d",&start);
printf("end point");
scanf("%d",&end);
for(loop=start;loop<=end;loop++)
{
printf("hello world!/n");
}
getch();
}
yup, it works...However I don't really understand why would you need the starting point? (except for if your problem asks for you to put it).
The reason is that if I wanted a repeated print of "Hello World!" 5 times, I wouldn't have to put 0 and 4, or 1 and 5, or 2 and 6... but I would just try to make it work with telling it the number 5...doesn't this sound more logical??
Of course starting points are good for example in integrating.
Thanks mark and chris!
I had found the solution and my problem was really solved.
My code that i compiled is:
C:
#include<stdio.h>
#include<conio.h>
int main()
{
int start,end,loop;
printf("enter the starting number");
scanf("%d",&start);
printf("end point");
scanf("%d",&end);
for(loop=start;loop<=end;loop++)
{
printf("hello world!/n");
}
getch();
}
I hope there isn't any error.
Unfortunately, there is still an error, if your intention is to print that "hello world" string a specific number of times. This is the same error I pointed out in post #7, at the bottom.
Mark44 said:
Finally, your code, as written, has an Off By One Error (OBOE), as it will print "hello world\n" number + 1 times, one more than you intend.
You can make your code simpler as follows:
C:
#include<stdio.h>
#include<conio.h>
int main()
{
int num, loop;
printf("Enter the number of times to print the string: ");
scanf("%d", &num);
for(loop = 0; loop < num; loop++)
{
printf("hello world!/n");
}
getch();
}
The code above will execute the for loop num times.