Print Natural Numbers in C
  • Twitter
  • Facebook
  • Snapchat
  • Instagram

Print First N Natural Numbers in C Using For Loop – Beginner-Friendly Guide

šŸ“‘ On this page:
  • Introduction
  • C Program to Print First N Natural Numbers
  • Sample Output
  • Program Explanation
  • Why This Program Matters
  • Frequently Asked Questions
  • Practice Assignments
šŸ“š In this tutorial, you will learn:
  • How to print first N natural numbers in C using for loop
  • How to take user input in C
  • How for loop works with step-by-step explanation
  • How to write clean and structured C code
  • Practice assignments to strengthen your skills

Introduction

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

If you enter 10, the output will be:

Enter the value of N: 10
First 10 Natural Numbers:
1 2 3 4 5 6 7 8 9 10

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.

Let's break down the code step by step:

  1. Include Header File: #include <stdio.h> includes the standard input/output library needed for printf() and scanf().
  2. Declare Variables: int n, i; declares two integer variables — n to store the user's input and i as the loop counter.
  3. Get User Input: printf("Enter the value of N: "); prompts the user, and scanf("%d", &n); reads the value entered by the user.
  4. Print Heading: printf("\nFirst %d Natural Numbers:\n", n); displays a heading showing how many numbers will be printed.
  5. For Loop: for (i = 1; i <= n; i++) runs the loop from 1 to N:
    • Initialization: i = 1 starts the loop counter at 1.
    • Condition: i <= n checks if the counter is within the limit.
    • Update: i++ increments the counter after each iteration.
  6. Print Number: printf("%d ", i); prints the current value of i on the same line.
  7. Return: return 0; indicates successful program execution.

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.

šŸ’” Key Takeaway: This simple program teaches you three essential programming concepts: user input, loops, and output formatting.

šŸ’» Practice Exercise

Challenge: Modify the program to print numbers from 1 to N in reverse order (N to 1).

šŸ” Click to Show Solution
#include <stdio.h>

int main() {
    int n, i;
    
    printf("Enter the value of N: ");
    scanf("%d", &n);
    
    printf("\nFirst %d Natural Numbers in Reverse Order:\n", n);
    
    for (i = n; i >= 1; i--) {
        printf("%d ", i);
    }
    
    return 0;
}

Frequently Asked Questions

1. 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.

2. 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.

3. 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.

4. 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.

5. 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.

šŸ’” Tip: Always test your program with different values of N to understand how the loop behaves with different inputs.

Practice Assignments Using For Loop

Assignment Set 1: Basic For Loop Programs

  • 1. Write a program in C using for loop to display the first N natural numbers.
  • 2. Write a program in C to print ODD numbers from 1 to N using for loop.
  • 3. Write a program in C to print EVEN numbers from 1 to N using for loop.
  • 4. Write a program in C to print all uppercase alphabets using for loop.
  • 5. Write a program in C to print all lowercase alphabets using for loop.
  • 6. Write a program in C using for loop to read 10 numbers and find their sum and average.
  • 7. Write a C program to read an integer and print its multiplication table using for loop.
  • 8. C program to check entered number is ZERO, POSITIVE or NEGATIVE using for loop.
  • 9. Write a program in C to find factorial of a number using for loop.
  • 10. Write a C program to find sum and average of first N natural numbers.
  • 11. C program to reverse a given number using for loop.
  • 12. C program to check if a number is an Armstrong number using for loop.
  • 13. C program to print Fibonacci series up to N using for loop.
  • 14. C program to check if a number is prime using for loop.
  • 15. C program to convert decimal number to binary using for loop.

Assignment Set 2: Advanced For Loop Programs

  • 1. C program to find sum of squares of first N natural numbers: 1² + 2² + 3² + ... + N²
  • 2. C program to find sum of first N natural numbers: 1 + 2 + 3 + ... + N
  • 3. C program to find sum of series: 1/1! + 1/2! + 1/3! + ... + N/N!
  • 4. C program to find sum of series: 1 + 1/2 + 1/3 + 1/4 + ... + 1/N
  • 5. C program to find sum of series: 1 + 3²/3³ + 5²/5³ + 7²/7³ + ...

šŸ“– Related Tutorials

  • for Loop in C - Complete Guide
  • for Loop Examples in C
  • while Loop in C - Complete Guide
  • do-while Loop in C - Complete Guide

Previous Topic: -->> for loop in C   ||   Next topic: -->> Print Odd Numbers in C


šŸ“š Explore More Topics

šŸ—„ļø SQL Interview Questions & Answers

SQL SELECT Statement FAQ SQL Restricting & Sorting Data FAQ SQL Group Functions & Aggregated Data FAQ SQL Multiple Tables (JOINs) FAQ SQL Subqueries FAQ SQL DML Statements (Managing Tables) FAQ SQL Indexes, Synonyms & Sequences FAQ SQL DDL (Tables & Relationships) FAQ SQL Views FAQ SQL Indexing Best Practices FAQ SQL Window & Analytic Functions FAQ

šŸ Python Interview Questions & Answers

Python Interview Questions Python Syntax & Variables FAQ Python Data Types FAQ Python If-Else FAQ Python Loops FAQ Python Functions Interview Q Python String Manipulation FAQ Python Lists & Dictionaries FAQ Python Tuples & Sets FAQ Python Exception Handling FAQ Python OOP Interview Questions

ā˜• Java Interview Questions & Answers

Java Introduction Interview Q Java Development Environment FAQ Java Data Types FAQ Java Control Flow & Operators FAQ Java Basic Input/Output FAQ Java Arrays FAQ Java Strings FAQ Java Methods FAQ Java Basic OOP Concepts FAQ Java Advanced OOP Concepts FAQ Java OOP Best Practices FAQ Java Exception Handling FAQ Java Synchronization FAQ Java Threads & Concurrency FAQ Java Collection Framework FAQ Java File I/O & Serialization FAQ Java Serialization & Deserialization FAQ Java Features FAQ Java Inner & Anonymous Classes FAQ Java Memory Management FAQ Java Packages FAQ Java Wrapper Classes FAQ Java Streams & Lambda FAQ

C Language

  • Home
  • Why C Language
  • History of C Language
  • Applications of C Language
  • Introduction To C
    • What is Program?
    • Structure of C Program
    • Working Of C Program
    • CHARACTER SET
    • VARIABLES AND IDENTIFIERS
    • BUILT-IN DATA TYPES
    • OPERATORS AND EXPRESSIONS
    • CONSTANTS AND LITERALS
    • SIMPLE ASSIGNMENT STATEMENT
    • BASIC INPUT/OUTPUT STATEMENT
    • SIMPLE 'C' PROGRAMS
    • Assignments
  • Operators in C Programming
    • Arithmetic Operators
    • Assignment Operators
    • Increment and Decrement Operators
    • Relational Operators
    • Logical Operators
    • Bitwise Operators
    • Other Operators
    • Assignments
  • Conditional Statements
    • DECISION MAKING WITHIN A PROGRAM
    • CONDITIONS
    • IF STATEMENT
    • IF-ELSE STATEMENT
    • IF-ELSE LADDER
    • NESTED IF-ELSE
    • SWITCH CASE
    • Assignments
  • Loops Statements
    • Introduction to Loops
    • GO TO Statement
    • Do while Loop
    • While Loop
    • Nested While Loop
    • Difference Between While and Do while
    • Difference Between Goto and loop
    • while loop assignments
    • C FOR Loop
    • C For loop examples
    • Nested for loop
    • Nested for loop examples
    • Infinite while Loops
    • Infinite for Loops
    • Continue in Loops
    • break in Loops
    • difference while do..while & for
    • Assignments
  • Arrays
    • One Dimensional Array
    • Declaring 1D Arrays
    • Initilization of 1D arrays
    • Accessing element of one 1D Array
    • Read and Display 1D Arrays
    • Two Dimensional Arrays
    • Declare 2D Arrays
    • Read and Display 2D Arrays
    • Assignments/Examples
  • Functions
    • Introduction
    • Need For User-Defined Function
    • Multiple Function Program
    • Modular Programming
    • Elements Of User Defined Function
    • Function Definition
    • Function Declaration
    • Types of functions
    • Nesting of Function
    • Recursion
    • Passing Array To Functions
    • Scope,Visibility and Lifetime of Variables
    • Assignments
  • Structure
    • Introduction
    • Array vs Structure
    • Defining Structure
    • Declaring Structure Variables
    • Type Defined Structure
    • Accessing Structure Members
    • Structure Initilization
    • Copying & Comparing Structure Variables
    • Array of Structure
    • Arrays Within Structure
    • Structures Within Structures
    • Structures and Functions
    • Structure Examples/Assignments
  • Union
    • Define Union
    • Create and use Union
    • Difference Between Structure and Union
    • Union Examples
    • Union FAQ
  • Pointers
    • What Are Pointers In C?
    • How Do We Use Pointers In C?
    • Declaration Of A Pointer
    • The Initialization Of A Pointer
    • Syntax Of Pointer Initialization
    • Use Of Pointers In C
    • The Pointer To An Array
    • The Pointer To A Function
    • The Pointer To A Structure
    • Types Of Pointers
    • The Null Pointer
    • The Void Pointer
    • The Wild Pointer
    • The Near Pointer
    • The Huge Pointer
    • The far Pointer
    • dangling pointer
    • Accessing Pointers- Indirectly And Directly
    • Pros Of Using Pointers In C
    • Cons Of Pointers In C
    • Applications Of Pointers In C
    • The & Address Of Operator In C
    • How To Read The Complex Pointers In C?
    • Practice Problems On Pointers
  • File Processing
    • File Handling In C
    • Types Of Files In C
    • Operations Done In File Handling
    • File Examples
    • Binary Files
    • count words,lines in a file
    • Copy files
    • Update File
    • count vowels in a file
  • Preprocessor
    • Macro substitution division
    • File Inclusion
    • Conditional Compilation
    • Other directives
    • Examples
  • Dynamic Memory Allocation
    • malloc
    • calloc
    • free
    • realloc
    • Examples
  • Storage Classes
  • Graphics
  • Frequently Asked Interview Questions (FAQ)
    • Introduction To C FAQ
    • Operators FAQ
    • Conditional Statements FAQ
    • Loops FAQ
    • Arrays FAQ
    • Function FAQ
    • Structure FAQ
    • Pointers FAQ
    • Files FAQ
    • Storage classes FAQ
    • Dynamic Memory FAQ
  • Programs/Assignments
    • Introduction To C
    • Operators
    • Conditional Statements
    • Loops
    • Arrays
    • Function
    • Structure
    • Pointers
    • Files
    • Storage classes
    • Dynamic Memory
  • Case Studies
  • Multiple Choice Questions
    • Introduction To C MCQ
    • Operators MCQ
    • Conditional Statements MCQ
    • Loops MCQ
    • Arrays MCQ
    • Function MCQ
    • Structure MCQ
    • Pointers MCQ
    • Files MCQ
    • Storage classes MCQ
    • Dynamic Memory MCQ
    • More MCQ

Get in touch

  • tech2dsm@gmail.com

© Sankalan Data Tech. All rights reserved.