Thursday, December 18, 2014

Spaceless triangular sterric pattern

//c++ program to display pattern like that

//user only enter the size
#include <iostream>
using namespace std;
int main()
{
 int size,temp_variable;  //declaring variables
 cout<<"Enter size = ";  //asking for output
 cin>>size;     //input
 temp_variable=size;   //storing sizze in temporary variable
 if(size>0)
 {
  for(int i=1;i<=size;i++)   //outer loop controlling number of rows
  {
   for(int j=1;j<=temp_variable;j++)  //inner loop controlling number of columns
   {
    cout<<"*";
   }
   temp_variable--;   //applying decrement
   cout<<endl;
  }
 }
 else
 {
  cout<<"PLease enter size greater than zero.\n";
 }
 return 0;
}

Triangular steric pattern

//c++ program to display pattern like that

#include <iostream>
using namespace std;
int main()
{
 int size,temp_variable;   //declaring variables
 cout<<"Enter size = ";   //asking for input
 cin>>size;      //input
 temp_variable=size;    //store size in temporary variable
 if(size>0)
 {
  for(int i=1;i<=size;i++)   //outer loop controlling number of rows
  {
   for(int j=1;j<=temp_variable;j++)  //inner loop controlling number of columns
   {
    cout<<"* ";
   }
   temp_variable--;     //applying decrement
   cout<<endl;
  }
 }
 else
 {
  cout<<"Please enter size greater than zero.\n";
 }
 return 0;
}

Triangular number pattern

//c++ program to dispaly a pattern like that

//so user only has to enter the size of pattern
#include <iostream>
using namespace std;
int main()
{
 int size,temp_size;    //declaring variable
 cout<<"Enter size  = ";   //asking for output
 cin>>size;      //giving input
 temp_size=size;      //store size in a temporary variable named temp_size
 for(int i=1;i<=size;i++)    //outer loop controllin number of rows
 {
  for(int j=1;j<=temp_size;j++)  //inner loop controlling number of columns
  {
   cout<<j<<" ";
  }
  temp_size--;     //decrement
  cout<<endl;
 }
 return 0;
}

Lower triangular number pattern

//c++ program to display a pattern like that

//so user only has to enter size
#include <iostream>
using namespace std;
int main()
{
 int size;        //declaring variable
 cout<<"Enter size = ";     //asking for input
 cin>>size;        //giving input(size)
 if(size>0)        // applying check
 {
  for(int i=1;i<=size;i++)    //applying outer loop which will controll number of rows
  {
   for(int j=1;j<=i;j++)    //applying inner loop whiuch will controll number of columns
   {
   cout<<j<<" ";
  }
  cout<<endl;
  }
 }
 else
 {
  cout<<"You have enter wrong size\n";   //condition for invalid input
 }
 return 0;
}

To find the nature and value of roots of quadratic equation except for complex roots

/*c++ Program to find the nature of roots and value of
roots of a quadratic equation.User has to enter the values of coefficients
of x^2,x and the value of constant*/


#include<iostream>
#include<cmath>
using namespace std;
int main()
{
  float a,b,constt,discre,root1,root2,k;  //declaring variables
  do
  {
  cout<<"Enter coeffitient value of x^2 = ";
     cin>>a;         //input the value of a the coeffitient of x^2
  }while(a==0);        //checking condition
  cout<<"Enter coeffitient value of x = ";  //asking to give input (value of coeffitient of x)
  cin>>b;          //input the (value of coeffitient of x)
  cout<<"Enter value of constant = ";    //asking to give value of constant
  cin>>constt;          //input the value of constant
  discre=pow(b,2)-4*a*constt;      //k is used as to store a temperarly intermediate result
  k=sqrt(discre);
   if(discre==0)
   {
     root1=(-b+k)/(2*a);  
     cout<<"The equation has a single(repeated) root\n\n";
     cout<<"first root = "<<root1<<endl;
   }
  else if(discre>0)
  {  
   root1=(-b+k)/(2*a);
   root2=(-b-k)/(2*a);
   cout<<"Equation has two real roots.\n\n";
      cout<<"First real root = "<<root1<<endl;
      cout<<"Second real root = "<<root2<<endl;
     }
  else
  {
    cout<<"The equation has two complex roots\n\n";
  }
 return 0;
}