C program to print a number....

  • Thread starter fraz ali
  • Start date
  • Tags
    Program
In summary, the conversation discusses a problem with printing statements in C programming and the use of for loops to repeat a certain action a specific number of times. The participants also discuss the importance of defining a starting point in a for loop and the need for input from the user to determine the number of repetitions. They also mention the use of functions and recursive functions, and provide an example program for printing "Hello World!" a certain number of times using a for loop.
  • #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!
 
Technology news on Phys.org
  • #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.
 
  • Like
Likes fraz ali and Dave Ritche
  • #3
jedishrfu said:
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
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
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
Dave Ritche said:
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
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
Dave Ritche said:
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 to C program to print a number....

1. What is a C program to print a number?

A C program to print a number is a computer program written in the C programming language that is designed to take a number as input and display it as output.

2. How do I write a C program to print a number?

To write a C program to print a number, you will need to use the C programming language and follow the syntax and rules of the language. You will also need to use input and output functions to take and display the number.

3. Can you provide an example of a C program to print a number?

Yes, here is an example of a simple C program to print a number:

#include <stdio.h>
int main()
{
  int num;
  printf("Enter a number: ");
  scanf("%d", &num);
  printf("The number entered is %d", num);
  return 0;
}

4. What is the purpose of a C program to print a number?

The purpose of a C program to print a number is to provide a simple and efficient way to take a number as input and display it as output. This can be useful for a variety of applications, such as mathematical calculations, data analysis, and user input/output.

5. What are the benefits of using a C program to print a number?

There are several benefits of using a C program to print a number, including its speed and efficiency, as well as its versatility and portability. C is a widely-used programming language that is known for its speed and low-level control, making it a popular choice for creating efficient programs.

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
14
Views
3K
  • Programming and Computer Science
Replies
9
Views
4K
  • Programming and Computer Science
Replies
18
Views
2K
  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
Replies
17
Views
8K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
2
Views
797
  • Programming and Computer Science
Replies
2
Views
2K
Back
Top