- 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:
- Include Header File:
#include <stdio.h>includes the standard input/output library needed forprintf()andscanf(). - Declare Variables:
int n, i;declares two integer variables ānto store the user's input andias the loop counter. - Get User Input:
printf("Enter the value of N: ");prompts the user, andscanf("%d", &n);reads the value entered by the user. - Print Heading:
printf("\nFirst %d Natural Numbers:\n", n);displays a heading showing how many numbers will be printed. - For Loop:
for (i = 1; i <= n; i++)runs the loop from 1 to N:- Initialization:
i = 1starts the loop counter at 1. - Condition:
i <= nchecks if the counter is within the limit. - Update:
i++increments the counter after each iteration.
- Initialization:
- Print Number:
printf("%d ", i);prints the current value ofion the same line. - 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³ + ...