Saturday, 22 December 2012

to display the new era of programming without using semicolon

#include <iostream>
#include<conio.h>
 using namespace std ;
int main()
{
while(cout<<"the new world of programming ")   \\no semicolon here
}

to substract two numbers without using minus

#include<iostream>
#include<conio.h>
using namespace std;

int main()
{
cout<<"enter two numbers";
cin>>a>>b;
cout<<"substracting  a-b  ="<<a+~b+1;
getch();
}

to find the square root of a number without using predfined function

#include<iostream>
#include<conio.h>
using namespace std;

int main()
{ float g,r,c=0,n,pg;  //g guess pg previous guess
cout<<"enter the number to find squareroot   ";
cin>>n;
        g=n/2; pg=n;
       while(g!=pg)
       {r=n/g;
       pg=g;
       g=(g+r)/2;
       }
cout<<"square root \t"<<g<<endl;
getch();      

spiral matrix c++ program


Here middle element  is 1 and the numbers after 1 are displayed in spiral way

#include<iostream>
#include<iomanip>   // for setw(int)  space generating
using namespace std;
#include<conio.h>

int main()
{
int num,r,s=1,i,c,j,k,ord;

cout<<"Enter mid element of square matrix and get the elements in a spiral way  :";
cin>>num;
cout<<"enter odd order of square matrix";
cin>>ord;
int a[ord][ord];r=ord/2;c=ord/2;a[r][c]=num;   //r  row  c  column
for(i=1;i<=ord;i++)
{if(i%2==0)
           {for(j=0;j<s;j++) // i =even control left side
           a[r][--c]=++num;
           for(k=0;k<s;k++)
           a[--r][c]=++num;
           }
         
           else
           {for(j=0;j<s;j++)  //i =odd control right side
           a[r][++c]=++num;
           for(k=0;k<s;k++)
           a[++r][c]=++num;
           }
           s++;
}cout<<setw(ord);
for(r=0;r<ord;r++)
{  for(c=0;c<ord;c++)
  cout<<a[r][c]<<setw(ord);
  cout<<endl;
}
getch();
}

substract two numbers without using minus

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{int a,b,c=0,i,f;
cout<<"Enter two numbers to substract :";
cin>>a>>b;
if(b>a)
{i=a;f=b;}
else
{i=b;f=a;}  //  i initial value  f final value
for( ;i<f;i++)
 ++c;
(b>a)?   //conditional operators eqivalent to if (b>a)
cout<<a <<"  -  "  <<b<< "  =  "<<(char)45<< c   //ASCII value of 45 is minus -
:   // else
cout<<a <<"  -  "  <<b <<"  =  "<< c;
getch();
}

Friday, 21 December 2012

Random numbers generation c++ program

#include<iostream>
#include<conio.h>
using namespace std; 
void swap(int &a,int &b) 
int main()
{int n,s,i,p=1;
cout<<"The number of numbers u want in random  :";     cin>>n;
int a[n],j=1; 
a[0]=p;
     for(i=++p;i<=n;i++,j++)
     {
     a[j]=i;
     if(j%2!=0)
     swap(a[j-1],a[j]);
     } 

 cout<<"\nRandom list generated \n";
 for(j=0;j<n;j++)
 cout<<a[j]<<"  "; 
getch(); 
}

void swap(int &a,int &b)
{int temp=a;
a=b;
b=temp;
}

Alphabetpattern c++ program

 
#include<iostream>
using namespace std;                
#include<conio.h>
#include<math.h>
int main()
{
 int k=-1,j;
char i,m=71,n=65;
for(int x=1;x<=7;x++)    //for number of lines 7
{
        for(i=65;i<=m;i++)      //for displaying A B C ...
        {
         cout<<i<<" ";
         }
         for(j=1;j<=k;j++)      //for displaying spaces
         {
           cout<<"  ";
         }
    k=k+2;
    if(i==72)
    {
    i--;
    }
i--;
      for(;i>=n;i--) //for displaying alphabets in reverse
      {
     cout<<i<<" ";
      }
      m--;
      cout<<"\n";
 }
  getch();
}