Syntax:
#include<iostream>
using namespace std; // Header files
void main()
{ //start of main
for( starting point ; condition ; 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
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
TAGS
No comments:
Post a Comment