C PROGRAMMING PROGRAM • LOOPS & NUMBER PROBLEMS
Print Numbers from 1 to N
Learn how to print numbers from 1 to n using a clear C program.
PROBLEM UNDERSTANDING
Input and expected output
Sample input
5
Sample output
1 2 3 4 5
COMPLETE C PROGRAM
Complete C implementation
#include <stdio.h>
int main(void)
{
int limit;
printf("Enter N: ");
scanf("%d", &limit);
for (int number = 1; number <= limit; number++)
printf("%d%c", number, number == limit ? '\n' : ' ');
return 0;
}CURRENT STEP
SELECTED LINE
EXPECTED OUTPUT FOR THE SAMPLE
1 2 3 4 5
Step 0 of 0
PROGRAM EXPLANATION
Algorithm and explanation
- Read the required input values.
- Increment a loop counter from 1 through N and print each value.
- Display the computed result.
Increment a loop counter from 1 through N and print each value.
EFFICIENCY
Time and space complexity
Time complexity
O(n)
Auxiliary space
O(1)
DEBUGGING CHECKLIST
Common mistakes
Check this
Use the correct format specifier for every variable.
Check this
Initialize variables before using their values.
Check this
Check braces, semicolons and input order carefully.
Try it yourself
Practice: Run the program with the sample input, predict its output, and then test one boundary case of your own.
