Friday, 21 December 2012

Random generation c++ program

computerstechnologydevdora.blogspot.in
devgroupindia.webs.com

#include<iostream>   // based on the principle of prime,odd,even  numbers
using namespace std;
#include<conio.h>
#include<math.h>
int j=0,s=1;bool prime(int num);
int random(int k);
int main()
{ int d=100,i,num;
cout<<"Enter any  number  :";
cin>>num;
cout<<"random numbers generated\n";
for( i=num;i<d;)
   { cout <<abs(i)<<" ";i=random (i);}
getch();
}
int random(int k)
{
    if (prime(k))
  return (k- ++j);
  else if (k%2!=0){
  return (k+ ++s);}
  else
  return k+ ++j;
}
bool prime(int num)
{int c=1;for(int i=2;i<=num;i++)
         if(num%i==0)
         ++c;
         if (c==2)
         return 1;
         else
         return 0;
}

Tuesday, 18 December 2012

To substract two numbers without using minus sign c++ program

#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();
}



visit  computerstechnology
         Facebook Devgroup

To display the elements in a spiral way 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();
}

Tuesday, 4 December 2012

To find the squareroot of a number without using sqrt() functon

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

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

For more visit
http://computersciencedora.blogspot.in

Alphabet pattern

 
#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();
}

sum of series 1+x+(x^2)/2!+..............+(x^n)/n! c++ program

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

int fact(int);
#include<math.h>
int main()
{    double sum=1;
int x,i=1,n;
cout<<"1+x+(x^2)/2!+............+(x^n)/n!\n\n\n\n";
cout<<"enter the value of x    ";
cin>>x;
cout<<"enter th value of n      ";  cin>>n;
while(i<=n)
{
sum=sum+(pow(x,i)/fact (i));
i++;
}
double val=(double)sum;        //typecasting
cout<<"sum is "<<val;
getch();
}

int fact(int j)
{
int fact=1,k=1;
while(k<=j)
{
     fact=fact*k;
     k++;
     }
return fact;
}

to convert binary number to decimal equivalent

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

 int main()
{ int n,i,sum,rem;
    sum=0,i=0;
    cout<<"enter the binary number   ";
    cin>>n;
  while(n!=0)
  { if((n%10==0) || (n%10==1))
      {rem=n%10;
      sum=sum+(rem*pow(2,i));
      n=n/10;
      ++i;
      }
      else {cout<<"entered number is not binary";goto a;}
  }
    cout<<"the decimal number is:"<<sum;
    a:
    getch();
}