C Program to Print Numbers between 1000 and 1200: A Step-by-Step Guide

C Program to Print Numbers between 1000 and 1200: A Step-by-Step Guide

Are you learning C programming and looking for a concise example to print numbers between 1000 and 1200? This article provides a clear, easy-to-understand C program example and detailed explanations. It includes source code with explanations, helping you grasp the concept of loops and basic syntax in C. By the end of this guide, you'll have a solid understanding of how to write and execute a simple C program.

Introduction to C Programming

C is a powerful general-purpose programming language. It is widely used for system and application software development, as well as in embedded systems. C is known for its efficiency and control over hardware, which makes it a popular choice for tasks that require performance and low-level interaction with the system.

A Sample C Program to Print Numbers

Let's dive into a sample program to print numbers between 1000 and 1200. The following code snippet is a simple C program that demonstrates how to use a loop to print the numbers from 1000 to 1200.

Source Code

#include iostream
using namespace std;
int main()
{
tfor(int i  1000; i  1200; i  )
t{
ttcout  i  endl;
t}
treturn 0;
}

This code snippet performs the following steps:

tThe #include iostream directive imports the input/output stream library, which is essential for using cout. tThe using namespace std; line allows us to use the standard library without prefixing each function with std::. tThe int main() function is the entry point of the program. tThe for loop iterates from 1000 to 1199. The i statement increments the value of i by 1 after each iteration. tThe cout i endl; statement prints the value of i followed by a newline. The operator is used for output operations. tFinally, return 0; indicates successful program execution.

Running the C Program

To run this C program, follow these steps:

tCreate a new file with a .c extension, for example, print_numbers.c. tCopy and paste the provided source code into the file. tOpen a terminal or command prompt and navigate to the directory where your file is located. tUse the gcc compiler to compile the program with the command: t
ttgcc -o print_numbers print_numbers.c t
tRun the compiled program with: t
tt./print_numbers t
tObserve the output in the terminal, which will display the numbers from 1000 to 1200.

Understanding the Loop

The for loop is a fundamental control structure in C. Here's a breakdown of the loop:

tThe initialization: int i 1000; sets the starting value for the loop counter. tThe condition: i 1200; checks if the loop should continue. The loop continues as long as this condition is true. tThe iteration: i ; increments the value of i by 1 at the end of each iteration.

The cout statement is used to print the current value of i to the console, followed by a newline character using endl;.

Customizing the Program

You can modify the program by changing the range of the loop. For example, to print numbers from 1500 to 1800, you would change the initialization and condition of the for loop:

for(int i  1500; i  1800; i  )
{
tcout  i  endl;
}

Similarly, to print numbers from 50 to 100, you would write:

for(int i  50; i  100; i  )
{
tcout  i  endl;
}

Conclusion

This C program to print numbers between 1000 and 1200 serves as a great starting point for beginners in C programming. Understanding loops and basic I/O operations are crucial for writing more complex programs. We hope this example has helped you understand the basics and inspired you to create your own C programs.

Happy coding!