Write a BASIC program to print the natural numbers from 1 to n using suitable looping statements. Receive the value “n” from the user.
Answers
Answer:
There are various ways to print n numbers. For this post I am concentrating on for loop to print natural numbers.
Step by step descriptive logic to print natural numbers from 1 to n.
Input upper limit to print natural number from user. Store it in some variable say N.
Run a for loop from 1 to N with 1 increment. The loop structure should be like for(i=1; i<=N; i++). At this point you might be thinking of various things such as.
Why starting from 1? Because we need to print natural numbers from 1.
Why going till N? Because we need to print natural numbers up to N.
Why increment loop counter by 1? Because difference between two natural numbers is 1. Therefore if n is one natural number then next natural number is given by n+1.
Inside the loop body print the value of i. You might think, why print value of i inside loop? Because we need to print natural numbers from 1 to N and from loop structure it is clear that i will iterate from 1 to N. So to print from 1 to N print the value of i.