C program to print a number...

  • C/++/#
  • Thread starter fraz ali
  • Start date
  • #1
4
2

Main Question or Discussion Point

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!
 

Answers and Replies

  • #2
11,882
5,533
Welcome to PF!

Have you learned about for loops?

While we can't do your homework for you, I would start there and learn more about "for" loops and how they work.

You might even be able to find an example online.
 
  • Like
Likes fraz ali and Dave Ritche
  • #3
4
2
Welcome to PF!

Have you learned about for loops?

While we can't do your homework for you, I would start there and learn more about "for" loops and how they work.

You might even be able to find an example online.
thanks.....
 
  • Like
Likes Dave Ritche
  • #4
ChrisVer
Gold Member
3,337
440
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
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.
 
  • #6
ChrisVer
Gold Member
3,337
440
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 >.
 
Last edited:
  • #7
33,721
5,418
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 [code=c] tag at the beginning and a [/code] 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
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.
 
  • #9
ChrisVer
Gold Member
3,337
440
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??:rolleyes:
Of course starting points are good for example in integrating.
 
  • Like
Likes Dave Ritche
  • #10
33,721
5,418
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.
 
  • Like
Likes Dave Ritche

Related Threads on C program to print a number...

Replies
1
Views
2K
Replies
5
Views
10K
Replies
13
Views
536
  • Last Post
Replies
4
Views
2K
Replies
1
Views
11K
Replies
3
Views
573
Top