Want to learn how to print a list of natural numbers using a simple loop in C? This beginner-level C program helps you understand the basics of the for loop by printing numbers from 1 to any number you choose. Whether you're just starting or brushing up your loop concepts, this is a great example to follow.
      
        In this guide, you'll write a program that takes input from the user (N), and prints the first N natural numbers using a for loop. This exercise is helpful in learning how loops, variables, and user inputs work together in C programming.
      
πΈ C Program to Print First N Natural Numbers
#include <stdio.h>
/*
   C Program to print the first N natural numbers using a for loop.
   Example output: 1 2 3 4 5 6 7 8 9 10 ... up to N
*/
int main()
{
    // Declare variables: 'n' to store the limit and 'i' for the loop counter
    int n, i;
    // Ask the user to enter the value of N
    printf("Enter the value of N: ");
    scanf("%d", &n);
    // Print the heading before the loop starts
    printf("\nFirst %d Natural Numbers:\n", n);
    // Loop from 1 to N
    for (i = 1; i <= n; i++)
    {
        // Print each number on the same line
        printf("%d ", i);
    }
    return 0;
}
        
      π Sample Output
Enter the value of N: 5
First 5 Natural Numbers:
1 2 3 4 5
        
      
      π Program Explanation
        This program begins by asking the user to enter a value for N. The for loop starts at 1 and continues until it reaches N. Each number is printed one by one on the same line with spaces. If you input 5, it prints 1 2 3 4 5. Itβs a straightforward way to understand how loops work in real code.
      
You can try different values of N to see how the output changes. This helps build a strong understanding of loop behavior and sequence generation.
π Why This Program Matters
        This type of program is a great way to learn how for loops work in C. It shows how to take user input, how to run a loop a certain number of times, and how to print results step by step. These are important skills for writing larger and more complex programs later.
      
π¬ Frequently Asked Questions
      What is a natural number in this program?
      Natural numbers here mean counting numbers starting from 1, such as 1, 2, 3, and so on. This program prints them in order up to the value you enter.
      Can I write this using a while loop instead of for?
      Yes, you can use a while loop. The logic remains the same, but you need to manage the loop counter and update it manually.
      What happens if I input a negative number?
      If you enter a negative number, the loop won't run because the condition i <= n fails right away. You can add a check to avoid this situation.
      Can this be changed to print only even or odd numbers?
      Definitely. Just add a condition inside the loop. For even numbers, use i % 2 == 0. For odd numbers, use i % 2 != 0.
      Is there a maximum number I can enter for N?
      The maximum depends on your system and the data type used. With an int in most systems, you can safely go up to around 2 billion, but much smaller values are better for output readability.