e6390af40fd246928c9f6b67562cf0fa37712fa8

10/15/2017

How to use for loop in c++ Programming | For Loop Structure and Examples in c++

In this post we will discus how to use the for loop in c++ programming language.The first thing we discus is that "What is the loop and why we use it?".So the loop is the repetition that is used to repeat the code of c++ language or any  other  language where  the loop is used.For loop is the easiest loop in c++ programming language because here we use the initial point of loop, condition and the increment  in the same brace like ( int a=1; a<=10; a++) here a=1 is the starting point of the loop and a<= is the condition of the loop and a++ is to increment in a..The syntax is given bellow..

Syntax:
#include<iostream>
using namespace std; // Header files
void  main()
{ //start of main

   for( starting pointcondition ; increment/decrements )
     {
          // body of the for
     }

     system("pause");

}//end of main


The for loop is used a lot in programming.This is also use to print diamond, Triangle, Rectangle, straight, curve and many more shapes in c++ programming. Now here we will discus some shapes.

Example No#1:
Write a c++ program to print the Table of any number from 1 to 10 that is given by the user using for Loop.
Solution:

#include<iostream>
using namespace std; // Header files
void  main()
{ //start of main

    int no;  //this is the number to take table number from the user.
    cout<<" Enter the Table Number "<<endl;
    cin>>no;

   for(int i=1;i<=10;i++)
     {
       cout<<no<< " * "<<i<<" = "<<no*i<<endl; // this line print table
     }
     system("pause");
}//end of main

Example 2 : 
Write a program that take Table number, starting and end point of the table from the user and print table in c++ programming Language.
Solution : 
#include<iostream>
using namespace std; // Header files
void  main()
//start of main
           
     int no;  //this is the variable to take table Number from the user.
     int st; //this is the starting point of the loop
     int end;//this is the ending point of the loop
     cout<<" Enter the Table Number "<<endl;
     cin>>no; 
     cout<<" Enter the starting point "<<endl;

     cin>>st; 
     cout<<" Enter the Ending Point "<<endl;

     cin>>end; 
      while(st<=end);//this is condition of loop
     {
      cout<<no<< " * "<<i<<" = "<<no*i<<endl; // this line print table
           
       st++; //this will increment  in start of 1 after every iteration.

     }
     cout<<"\n\n\n"<<endl;
     system("pause");

}//end of main

Example 2 : 
Write a program that take Table number, starting and end point of the table from the use and print table in c++ programming Language.
Solution : 
#include<iostream>
using namespace std; // Header files
void  main()
//start of main
           
     int no;  //this is the variable to take table Nimber from the user.
     int st; //this is the starting point of the loop
     int end;//this is the ending point of the loop
     cout<<" Enter the Table Number "<<endl;
     cin>>no; 
     cout<<" Enter the starting point "<<endl;
     cin>>st; 
     cout<<" Enter the Ending Point "<<endl;
     cin>>end; 
      for ( ;st<=end;  st++);//this is condition of loop
     {
      cout<<no<< " * "<<i<<" = "<<no*i<<endl; // this line print table
     }
     system("pause");

}//end of main

Example 3 : 
write a program to print half pyramid in c++ programming Language.
Solution : 
#include<iostream>
using namespace std; // Header files
void  main()
{ //start of main

     int rows;
     cout<<" Enter Number of rows do you want to print "<<endl;
     cin>>rows; // enter the  number of rows do you want to print.
     for(int i=1;i<=rows;i++)
     {
           for(int j=1;j<=i;j++)
           {
                cout<< " * ";
           }
           cout<<endl;
     }

     cout<<"\n\n\n"<<endl;
     system("pause");
}//end of main


Example 4 : 
write a program to print inverted half pyramid in c++ programming Language.
Solution : 
#include<iostream>
using namespace std; // Header files
void  main()
//start of main

     int rows;
     cout<<" Enter Number of rows do you want to print "<<endl;
     cin>>rows; // give the  number of rows do you want to print.
     for(int i=rows;i<=1;i--)
     {
           for(int j=1;j<=i;j++)
           {
                cout<< " * ";
           }
           cout<<endl;
     }

     cout<<"\n\n\n"<<endl;
     system("pause");

}//end of main




Example 4 : 
Write a program to print Floyd's Triangle in c++ programming Language.
Solution : 
 #include<iostream>
using namespace std; // Header files
void  main()
{ //start of main
     int Number=1;
     int rows; //veriable decleration
     cout<<" Enter Number of rows do you want to print "<<endl; //message
     cin>>rows; // give the  number of rows do you want to print.
    
     for(int i=1;i<rows;i++) //condition 1
     {
           for(int j=1;j<=i;j++) //condtion 2 that repeat again and again when condition 1 repeat.
           {
                cout<< Number<<"  "; //this line print star.
                ++Number;
           }
           cout<<endl; //move to next line.
     }

     cout<<"\n\n\n"<<endl; //for next line not necessery.
     system("pause");
}//end of main


For More Programs comment your requirement i will create a program.. 



TAGS
for loop, loop, tutorial, loops, guitar, programming, control flow, computer programming (conference subject), tutorials, how, learn, for, programming language (software genre), 3.3, beginners, how to use for loop in matlab, how to use for loop in html, how to use for loop in php, how to use for loop in excel, how to use for loop in javascript, how to use for loop in c++, python (programming language), how to use for loop in laravel, how to use for loop in android studio, 
c (programming language), python (software), guitar lessons, to, lesson, guitar pedals, beats, looper pedal, loop (music), pedal, python 2.7, python 3, how to use for loop in swift, how to use for loop in matlab, how to use for loop in c programming, how to use for loop in python, how to use for loop in vba, how to use for loop in shell script, how to use for loop vba, how to use for loop c++, how to use for loop, how to use for loops in java, how to use for loop, how to use for loop in c++, 
how to use for loop in php, how to use for loop in java, how to use for loop in jquery, how to use for loop in python, how to use for loop in c++ in hindi, how to use for loop in javascript, how to use for loop in python, iteration, looper, c++, basics, processing (programming language), processing.org, computer science (field of study), java (programming language), processing tutorial, processing 3, for loops in processing, for loop in c hindi, looping using for loop using for loop, 


No comments:

Post a Comment

Privacy Policy for Apps

Privacy Policy Muhammad Shahbaz built the Health Physio Association app as a Free app. This SERVICE is provided by Muhammad Shahbaz at no...