for loop examples in C++

In this program, we will print number in ascending and descending order,

Print natural number until 100 and also their sum

 

#include<iostream>
using namespace std;

  int main()
   {
     
     int num = 50, sum = 0;
      for(int i=1; i<=num; i++) 
       {
	 cout<<i<<" ";
         sum = sum+i;
       }
     cout<<"sum:"<<sum;
   return 0;
   }

OUTPUT

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

sum: 1275

Explanation:

remember always, adding a number is in the loop always sum will be initialized to 0 while multiplication we used sum = 1.

Print odd number in a descending order till number 100.

#include<iostream>
using namespace std;

  int main()
   {
   
     int num = 50;
     for(int i=num; i>=0; i--)  {
	if(i%2==0)
	   continue;
	else
	   cout<<i<<"  ";
      }

return 0;
   }

OUTPUT

49  47  45  43  41  39  37  35  33  31  29  27  25  23  21  19  17  15  13  11
9  7  5  3  1

skipping a number at an interval of 10 in a series

#include<iostream>
using namespace std;

  int main()
   {
     
     int num = 20;
      for(int i=1; i<=num; i++)  {
	cout<<""<<10*i<<" ";
      }

   return 0;
   }

OUTPUT

10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190 200 210 220 
230 240 250 260 270 280 290 300

Print alphabet A-Z using for loop

#include<iostream>
using namespace std;

  int main()
   {
     
     char c = 'Z';
     for(char i='A'; i<=c; i++)  {
	cout<<i<<"  ";
      }

    return 0;
   }

OUTPUT

A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T  U  V  W  X  Y  Z

another similar Print ASCII value in C++

Execute and Terminate a loop manually by Given numbers

#include<iostream> 
using namespace std;

int main()
{
 
  int first,last;

  cout<<"To set starting point to start counting: ";
  cin>>first;
  cout<<"To set ending point to finish counting : ";
  cin>>second;

  cout<<endl;

  for(int i=first;i<second+1;i++)
    cout<<i<<" "; // body of loop

  cout<<endl;

// print variable s and e values
  cout<<"1st number is "<<first<<endl;
  cout<<"last number is "<<second<<endl;

return 0;
}

OUTPUT

TO set starting point to start counting: 5
To set ending point to finish counting : 18

5  6  7  8  9  10  12  13  14  15  16  17  18

1st number is : 5
last number is:18

Calculating Total of even and odd number

#include<iostream>
using namespace std;

int main()
{
  
  int num,even=0,odd=0;

  cout<<"Enter the last Number: ";
  cin>>num;

  for(int i=0;i<=num;i=i+2)  // for even numbers
    {
      even= even+i; // store even numbers
      cout<<i<<" ";
    }
     cout<<" = "<<even;
 
     cout<<endl;

    for(int j=1; j<=num; j=j+2)  //for odd numbers
      {
        odd = odd+j;   // store odd numbers
        cout<<j<<" ";
      }
     cout<<" = "<<odd;

 return 0;
}

OUTPUT

0 2 4 6 8 = 20
1 3 5 7 9 = 25

Generate a Table of given number using for loop

#include<iostream> 
using namespace std;

int main()
{
   
   int num;

   cout<<"Enter number: ";
   cin>>num;

   for(int i =1; i<=10; i++)
     {
        cout<<num<<" x "<<i<<"\t = "<<num*i<<endl;
     }
 return 0;
}

OUTPUT

Enter number: 4

 4 x 1   = 4 
 4 x 2   = 8 
 4 x 3   = 12
 4 x 4   = 16
 4 x 5   = 20
 4 x 6   = 24
 4 x 7   = 28
 4 x 8   = 32
 4 x 9   = 36
 4 x 10  = 40

Related exercise,


C++ Examples

Like it?

Leave a Reply

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