Program to Print First n Natural Numbers and their Sum

Example Of C++ Program of for Loop (program to print first n natural numbers and their sum) –

#include<iostream.h>
int main()
    {
     int i,sum,n;
     cout<<"How many natural numbers = " ;
     cin>>n;
     cout"\n";
     for( i = 1, sum = 0; i <= n; ++i)
          {
           cout<<"\t" <<i;              //statement-1
           sum = sum + i;               //statement-2
          }
     cout<<"\n The sum of first "<<n<<" natural numbers is = "<<sum ; //statement-3
     return 0;
    }

Output :

How many natural numbers =6
1   2   3   4   5   6
 The sum of first 6 natural numbers is = 21

Program Illustration : 

  • Initialization expression : i=1 and sum=0 separated by commas
  • Test expression : i<=n
  • update expression : ++i
All these expressions are separated by semicolon(;) .
  1. Both the variables i and sum are initialized by their first values 1 and 0 respectively.
  2. Then the test expression (i<=n), where n=6(in output) evaluates to true and the body of the loop ,i.e. statement 1 and statement-2 will be executed.
  3. Update expression (++i) will be executed and the value of i becomes 2.
  4. step 2 is repeated till the value of i becomes 7. Then the test expression will be evaluated again and results a false value. i.e (7<=6) will results a false value. And finally the loop gets terminated and reach at statement-3 and the normal flow of program will execute.
Question : What will be the value of control variable after termination of loop ?
Solution : Control variable of loop in the above program – i as it the variable, which controls the loop execution by checking the condition. We have to determine at what value of i, the test expression gets false. From the above illustration, when i = 7; the test expression results false value. So, at the end, value of i will be 7.

Some deep notes about for loop  (or) variations of for loop-  

1. Multiple Initialization Expression and Update Expression - A for loop
   may contain multiple initialization and/or multiple update expression.
   These expressions are separated by semicolon (;).

2. Prefer prefix increment/decrement over postfix increment/decrement when
   used alone - because prefix operators are faster executed than postfix.
   So,
   for (i = 0; i<n; i++)      Χ (it is not wrong, but we generally do not
                                       prefer when used alone)
   for (i = 0; i<n; ++i)      

3. Optional Expressions - In for loop, initialization expression, test
   expression and the update expression are optional. For example,
   a) skipping initialization expression -
      int i=1; sum=0;
      for(;i<=20; sum+=i,++i)
   b) skipping update expression(s) - 
      for(j=0;j!=242;)   //statement-4
      cin>>j++;         // the following loop will run until the user 
                                      enters 242.
   c) skipping both expressions (initialization and update) - 
      Let j has been initialized before in statement-4, so,
      for(;j!=242;)
      cin>>j++;

4. Infinite Loop - can be produced by omitting test expression.
   for(j=252; ; --i)
       cout<<"Infinite Loop\n";
   (or)
   for(; ;)
        cout<<"Infinite loop again\n";

5. Empty Loop - If the body of loop does not contain any statement, it is
   said to be an empty loop. For example, 
   for (i = 0; i<n; ++i)  ;
   Notice the semicolon(;) after for loop, which means that a block of
   statements after such a loop is not part of for loop, So, the control
   variable counts its value in above case.

6. Declaration of variables in the loop - 
   A variable can be declared with in a for loop. For example, 
   for (int i = 0; i<n; ++i)
   {
   :
   }
   declares the variable i in initialization expression itself.
]]>

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top