u

VB Projects

Get all Visual Basic Projects here..

ASP Dot Net Projects

Get all Asp Dot Net Projects.

C++ Projects and Programs

Get all C++ Projects and Programs here...

PHP Projects

Get all PHP Projets here..

JAVA Projects

Get All JAVA Projects here...

Thursday, 7 June 2012

C++ Program for reading, writing and multiplication of the matrices

C++ Program for reading, writing and multiplication of the matrices



#include <iostream.h>
#include <iomanip.h>
#include <conio.h>

const int rows = 3;     // number of rows in matrix (must be constant)
const int columns = 3;  // number of columns in matrix (must be constant)

void ReadMatrix (float ma[rows][columns])
// this function reads matrix from keyboard
{
   int r, c;
   cout << "\nPlease enter matrix";
   for (r=0; r<rows; r++)
   {
      for (c=0; c<columns; c++)
      {
         cout << "\nElement A(" << (r+1) << "," << (c+1) << ") ";
         cin >> ma[r][c];
      }
   }
}

void WriteMatrix (float ma[rows][columns])
// this function writes matrix on screen
{
   int r, c;
   cout << "\nMatrix is:";
   for (r=0; r<rows; r++)
   {
      cout << "\n";
      for (c=0; c<columns; c++)
      {  // setw(10) adds space to make all numbers 10 characters wide
         cout << setw(10) << ma[r][c] << "  ";
      }
   }
}

void MultiplyByScalar (float sq[rows][columns], float fac)
// this function multiplies a matrix by a scalar
{
   int i, j;
   for (i=0; i<rows; i++)
   {
      for (j=0; j<columns; j++)
      {
         sq[i][j] = sq[i][j] * fac;
      }
   }
}              

void main ()
{
   // declare matrix and scalar
   float A[rows][columns], f;
   // get matrix from user
   ReadMatrix(A);
   // get scalar from user
   cout << "\n\nEnter factor ";
   cin >> f;
   // multiply matrix by scalar
   MultiplyByScalar (A, f);
   // output result matrix on screen
   WriteMatrix(A);
   // wait for user to press a key
   getch();
}

C++ Program to perform: Matricies-add,subtract & more

C++ Program to perform: Matricies-add,subtract & more


#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<iomanip.h>

   void menu();
   void determan();
   void multi();
   void add();
   void subtract();
   void exit();
   void suberror1();
   void suberror2();
   void adderror1();
   void adderror2();
   void multierror2();
   void multierror1();
   void equation();
   void equationerror1();
   void equationerror2();



   int main()
{  cout<<"\n\n\n\n\n";
   cout<<setw(40)<<"*************\n";
   cout<<setw(40)<<"| MATRACIES |\n";
   cout<<setw(40)<<"*************\n";
   cout<<setw(41)<<"*************\n\n";
   cout<<setw(48)<<"Press any key to continue...";
   getch();
   clrscr();
   menu();


   return 0;
}


   void menu()      //This is the main menu.
   {
   int todo;

   cout<<setw(20)<<"Main Menu\n\n";
   cout<<"1-Find the dertermanants of a 3*3\n";
   cout<<"2-Multiply two 3*3\n";
   cout<<"3-Add two 3*3\n";
   cout<<"4-Subtract two 3*3\n";
   cout<<"5-Solve an equation with 3 variables\n";
   cout<<"6-Exit Program\n\n";
   cout<<"What do you want to do Today?-";
   cin>>todo;
   clrscr();
   if (todo==1)
   determan();
   if (todo==2)
   multi();
   if (todo==3)
   add();
   if (todo==4)
   subtract();
   if (todo==5)
   equation();
   if (todo==7)
   exit();

    }

   void determan()      //This Figures out Determanants
   {
   int back,back2;
   double a1,a2,a3,b1,b2,b3,c1,c2,c3,determ;
   cout<<setw(20)<<"| a1 b1 c1 |\n";
   cout<<setw(20)<<"| a2 b2 c2 |\n";
   cout<<setw(21)<<"| a3 b3 c3 |\n\n";
   cout<<"Enter a1-";
   cin>>a1;
   cout<<endl;
   cout<<"Enter a2-";
   cin>>a2;
   cout<<endl;
   cout<<"Enter a3-";
   cin>>a3;
   cout<<endl;
   cout<<"Enter b1-";
   cin>>b1;
   cout<<endl;
   cout<<"Enter b2-";
   cin>>b2;
   cout<<endl;
   cout<<"Enter b3-";
   cin>>b3;
   cout<<endl;
   cout<<"Enter c1-";
   cin>>c1;
   cout<<endl;
   cout<<"Enter c2-";
   cin>>c2;
   cout<<endl;
   cout<<"Enter c3-";
   cin>>c3;
   cout<<endl;
   clrscr();
   determ=(a1*b2*c3)+(a2*b3*c1)+(a3*b1*c2)-(c1*b2*a3)-(c2*b3*a1)-(c3*b1*a2);
   if (determ==0)
   cout<<"Impossible to solve, the Determanant of a matrix cannot equal 0"<<endl;
   cout<<"                   | "<<a1<<" "<<b1<<" "<<c1<<" |\n";
   cout<<"The Determanant of | "<<a2<<" "<<b2<<" "<<c2<<" |="<<determ<<endl;
   cout<<"                   | "<<a3<<" "<<b3<<" "<<c3<<" |\n";
   cout<<endl;
   cout<<"Go Back to Main Menu Press 1 Go back to Determanants press 2-";
   cin>>back;
   clrscr();
   if (back==1)
   menu();
   if (back==2)
   determan();
   if (back>2)
   cout<<"Error Please only press 1 or 2."<<endl;
   cout<<endl;
   cout<<"Go Back to Main Menu Press 1 Go back to Determanants press 2-";
   cin>>back;
   clrscr();
   if (back2==1)
   menu();
   if (back2==2)
   determan();
   if (back2>2)
   cout<<"Error";
   }

   void multi()      //This Multiplies two matriceis
   {
   int a,b;
   double a1,a2,a3,b1,b2,b3,c1,c2,c3,d1,d2,d3,e1,e2,e3,f1,f2,f3,w1,w2,w3,w4,w5,w6,w7,w8,w9;
   cout<<setw(40)<<"| a1 b1 c1 |    | d1 e1 f1 |"<<endl;
   cout<<setw(40)<<"| a2 b2 c2 | *  | d2 e2 f2 |"<<endl;
   cout<<setw(40)<<"| a3 b3 c3 |    | d3 e3 f3 |"<<endl;
   cout<<endl;
   cout<<"Enter Value for a1- ";
   cin>>a1;
   cout<<"Enter Value for a2- ";
   cin>>a2;
   cout<<"Enter Value for a3- ";
   cin>>a3;
   cout<<"Enter Value for b1- ";
   cin>>b1;
   cout<<"Enter Value for b2- ";
   cin>>b2;
   cout<<"Enter Value for b3- ";
   cin>>b3;
   cout<<"Enter Value for c1- ";
   cin>>c1;
   cout<<"Enter Value for c2- ";
   cin>>c2;
   cout<<"Enter Value for c3- ";
   cin>>c3;
   cout<<"Enter Value for d1- ";
   cin>>d1;
   cout<<"Enter Value for d2- ";
   cin>>d2;
   cout<<"Enter Value for d3- ";
   cin>>d3;
   cout<<"Enter Value for e1- ";
   cin>>e1;
   cout<<"Enter Value for e2- ";
   cin>>e2;
   cout<<"Enter Value for e3- ";
   cin>>e3;
   cout<<"Enter Value for f1- ";
   cin>>f1;
   cout<<"Enter Value for f2- ";
   cin>>f2;
   cout<<"Enter Value for f3- ";
   cin>>f3;
   w1=(a1*d1)+(b1*d2)+(c1*d3);
   w2=(a2*d1)+(b2*d2)+(c2*d3);
   w3=(a3*d1)+(b3*d2)+(c3*d3);
   w4=(a1*e1)+(b1*e2)+(c1*e3);
   w5=(a2*e1)+(b2*e2)+(c2*e3);
   w6=(a3*e1)+(b3*e2)+(c3*e3);
   w7=(a1*f1)+(b1*f2)+(c1*f3);
   w8=(a2*f1)+(b2*f2)+(c2*f3);
   w9=(a3*f1)+(b3*f2)+(c3*f3);
   clrscr();
   cout<<"| "<<a1<<" "<<b1<<" "<<c1<<" |    | "<<d1<<" "<<e1<<" "<<f1<<" |  | "<<w1<<" "<<w4<<" "<<w7<<" |"<<endl;
   cout<<"| "<<a2<<" "<<b2<<" "<<c2<<" | *  | "<<d2<<" "<<e2<<" "<<f2<<" |= | "<<w2<<" "<<w5<<" "<<w8<<" |"<<endl;
   cout<<"| "<<a3<<" "<<b3<<" "<<c3<<" |    | "<<d3<<" "<<e3<<" "<<f3<<" |  | "<<w3<<" "<<w6<<" "<<w9<<" |"<<endl;
   cout<<endl;
   cout<<"Go back to Multiplication press 1 go back to Main Menu press 2- ";
   cin>>a;
   clrscr();
   if (a==1)
   multi();
    if (a==2)
   menu();
   if (a>2)
   multierror1();
   }

   void add()      //This adds two matriceis
   {
   int a,b;
   double a1,a2,a3,b1,b2,b3,c1,c2,c3,d1,d2,d3,e1,e2,e3,f1,f2,f3,w1,w2,w3,w4,w5,w6,w7,w8,w9;
   cout<<setw(40)<<"| a1 b1 c1 |    | d1 e1 f1 |"<<endl;
   cout<<setw(40)<<"| a2 b2 c2 | +  | d2 e2 f2 |"<<endl;
   cout<<setw(40)<<"| a3 b3 c3 |    | d3 e3 f3 |"<<endl;
   cout<<endl;
   cout<<"Enter Value for a1- ";
   cin>>a1;
   cout<<"Enter Value for a2- ";
   cin>>a2;
   cout<<"Enter Value for a3- ";
   cin>>a3;
   cout<<"Enter Value for b1- ";
   cin>>b1;
   cout<<"Enter Value for b2- ";
   cin>>b2;
   cout<<"Enter Value for b3- ";
   cin>>b3;
   cout<<"Enter Value for c1- ";
   cin>>c1;
   cout<<"Enter Value for c2- ";
   cin>>c2;
   cout<<"Enter Value for c3- ";
   cin>>c3;
   cout<<"Enter Value for d1- ";
   cin>>d1;
   cout<<"Enter Value for d2- ";
   cin>>d2;
   cout<<"Enter Value for d3- ";
   cin>>d3;
   cout<<"Enter Value for e1- ";
   cin>>e1;
   cout<<"Enter Value for e2- ";
   cin>>e2;
   cout<<"Enter Value for e3- ";
   cin>>e3;
   cout<<"Enter Value for f1- ";
   cin>>f1;
   cout<<"Enter Value for f2- ";
   cin>>f2;
   cout<<"Enter Value for f3- ";
   cin>>f3;
   clrscr();
   w1=a1+d1;
   w2=a2+d2;
   w3=a3+d3;
   w4=b1+e1;
   w5=b2+e2;
   w6=b3+e3;
   w7=c1+f1;
   w8=c2+f2;
   w9=c3+f3;
   clrscr();
   cout<<"| "<<a1<<" "<<b1<<" "<<c1<<" |    | "<<d1<<" "<<e1<<" "<<f1<<" |  | "<<w1<<" "<<w4<<" "<<w7<<" |"<<endl;
   cout<<"| "<<a2<<" "<<b2<<" "<<c2<<" | +  | "<<d2<<" "<<e2<<" "<<f2<<" |= | "<<w2<<" "<<w5<<" "<<w8<<" |"<<endl;
   cout<<"| "<<a3<<" "<<b3<<" "<<c3<<" |    | "<<d3<<" "<<e3<<" "<<f3<<" |  | "<<w3<<" "<<w6<<" "<<w9<<" |"<<endl;
   cout<<endl;
   cout<<"Go back to Addition press 1 go back to Main Menu press 2- ";
   cin>>a;
   clrscr();
   if (a==1)
   add();
    if (a==2)
   menu();
   if (a>2)
   adderror1();
   }

   void subtract()      //This Subtracts two matriceis
   {   int a;
   double a1,a2,a3,b1,b2,b3,c1,c2,c3,d1,d2,d3,e1,e2,e3,f1,f2,f3,w1,w2,w3,w4,w5,w6,w7,w8,w9;
   cout<<setw(40)<<"| a1 b1 c1 |    | d1 e1 f1 |"<<endl;
   cout<<setw(40)<<"| a2 b2 c2 | -  | d2 e2 f2 |"<<endl;
   cout<<setw(40)<<"| a3 b3 c3 |    | d3 e3 f3 |"<<endl;
   cout<<endl;
   cout<<"Enter Value for a1- ";
   cin>>a1;
   cout<<"Enter Value for a2- ";
   cin>>a2;
   cout<<"Enter Value for a3- ";
   cin>>a3;
   cout<<"Enter Value for b1- ";
   cin>>b1;
   cout<<"Enter Value for b2- ";
   cin>>b2;
   cout<<"Enter Value for b3- ";
   cin>>b3;
   cout<<"Enter Value for c1- ";
   cin>>c1;
   cout<<"Enter Value for c2- ";
   cin>>c2;
   cout<<"Enter Value for c3- ";
   cin>>c3;
   cout<<"Enter Value for d1- ";
   cin>>d1;
   cout<<"Enter Value for d2- ";
   cin>>d2;
   cout<<"Enter Value for d3- ";
   cin>>d3;
   cout<<"Enter Value for e1- ";
   cin>>e1;
   cout<<"Enter Value for e2- ";
   cin>>e2;
   cout<<"Enter Value for e3- ";
   cin>>e3;
   cout<<"Enter Value for f1- ";
   cin>>f1;
   cout<<"Enter Value for f2- ";
   cin>>f2;
   cout<<"Enter Value for f3- ";
   cin>>f3;
   clrscr();
   w1=a1-d1;
   w2=a2-d2;
   w3=a3-d3;
   w4=b1-e1;
   w5=b2-e2;
   w6=b3-e3;
   w7=c1-f1;
   w8=c2-f2;
   w9=c3-f3;
   clrscr();
   cout<<"| "<<a1<<" "<<b1<<" "<<c1<<" |    | "<<d1<<" "<<e1<<" "<<f1<<" |  | "<<w1<<" "<<w4<<" "<<w7<<" |"<<endl;
   cout<<"| "<<a2<<" "<<b2<<" "<<c2<<" | -  | "<<d2<<" "<<e2<<" "<<f2<<" |= | "<<w2<<" "<<w5<<" "<<w8<<" |"<<endl;
   cout<<"| "<<a3<<" "<<b3<<" "<<c3<<" |    | "<<d3<<" "<<e3<<" "<<f3<<" |  | "<<w3<<" "<<w6<<" "<<w9<<" |"<<endl;
   cout<<endl;
   cout<<"Go back to Subtraction press 1 go back to Main Menu press 2- ";
   cin>>a;
   clrscr();
   if (a==1)
   subtract();
    if (a==2)
   menu();
   if (a<2)
   suberror1();
   if (a=='a')
   suberror1();
   }
   void exit()      //This exits the program
   {
   char *yes;
   cout<<"Thank you for usin my Matrix program.  "<<endl;
   cout<<endl;
   cout<<"Are you sure you want to quit?(y/n)- ";
   cin>>yes;
   clrscr();
   if (yes=="n")
   menu();
   }
   void suberror1()
   {
   int c;
   cout<<"Error Please only press 1 or 2."<<endl;
   cout<<endl;
   cout<<"Go back to Subtraction press 1 go back to Main Menu press 2- ";
   cin>>c;
   clrscr();
   if (c==1)
   subtract();
   if(c==2)
   menu();
   if (c>2)
   suberror2();
   }
   void suberror2()
   {
   int d;
   cout<<"Error Please only press 1 or 2."<<endl;
   cout<<endl;
   cout<<"Go back to Subtraction press 1 go back to Main Menu press 2- ";
   cin>>d;
   clrscr();
   if (d==1)
   subtract();
   if(d==2)
   menu();
   if (d>2)
   suberror1();
   }
   void adderror1()
   {
   int c;
   cout<<"Error Please only press 1 or 2."<<endl;
   cout<<endl;
   cout<<"Go back to Addition press 1 go back to Main Menu press 2- ";
   cin>>c;
   clrscr();
   if (c==1)
   add();
   if(c==2)
   menu();
   if (c>2)
   adderror2();
   }
   void adderror2()
   {
   int d;
   cout<<"Error Please only press 1 or 2.\n";
   cout<<endl;
   cout<<"Go back to Addition press 1 go back to Main Menu press 2- ";
   cin>>d;
   clrscr();
   if (d==1)
   add();
   if(d==2)
   menu();
   if (d>2)
   adderror1();
   }
   void multierror1()
   {
   int c;
   cout<<"Error Please only press 1 or 2.\n";
   cout<<endl;
   cout<<"Go back to Multiplication press 1 go back to Main Menu press 2- ";
   cin>>c;
   clrscr();
   if (c==1)
   multi();
   if(c==2)
   menu();
   if (c>2)
   multierror2();
   }
   void multierror2()
   {
   int d;
   cout<<"Error Please only press 1 or 2.\n";
   cout<<endl;
   cout<<"Go back to Multiplication press 1 go back to Main Menu press 2- ";
   cin>>d;
   clrscr();
   if (d==1)
   multi();
   if(d==2)
   menu();
   if (d>2)
   multierror1();
   }

   void equation()
   {

   double a1,a2,a3,b1,b2,b3,c1,c2,c3,d1,d2,d3,x,y,z,m;
   cout<<setw(30)<<"A1x+B1y+C1z=D1"<<endl;
   cout<<setw(30)<<"A2x+B2y+C2z=D2"<<endl;
   cout<<setw(30)<<"A3x+B3y+C3z=D3"<<endl;
   cout<<endl;
   cout<<"Enter the value for A1-";
   cin>>a1;
   cout<<"Enter the value for A2-";
   cin>>a2;
   cout<<"Enter the value for A3-";
   cin>>a3;
   cout<<"Enter the value for B1-";
   cin>>b1;
   cout<<"Enter the value for B2-";
   cin>>b2;
   cout<<"Enter the value for B3-";
   cin>>b3;
   cout<<"Enter the value for C1-";
   cin>>c1;
   cout<<"Enter the value for C2-";
   cin>>c2;
   cout<<"Enter the value for C3-";
   cin>>c3;
   cout<<"Enter the value for D1-";
   cin>>d1;
   cout<<"Enter the value for D2-";
   cin>>d2;
   cout<<"Enter the value for D3-";
   cin>>d3;
   clrscr();
   x=((d1*b2*c3)+(d2*b3*c1)+(d3*b1*c2)-(c1*b2*d3)-(c2*b3*d1)-(c3*b1*d2))/((a1*b2*c3)+(a2*b3*c1)+(a3*b1*c2)-(c1*b2*a3)-(c2*b3*a1)-(c3*b1*a2));
   y=((a1*d2*c3)+(a2*d3*c1)+(a3*d1*c2)-(c1*d2*a3)-(c2*d3*a1)-(c3*d1*a2))/((a1*b2*c3)+(a2*b3*c1)+(a3*b1*c2)-(c1*b2*a3)-(c2*b3*a1)-(c3*b1*a2));
   z=((a1*b2*d3)+(a2*b3*d1)+(a3*b1*d2)-(d1*b2*a3)-(d2*b3*a1)-(d3*b1*a2))/((a1*b2*c3)+(a2*b3*c1)+(a3*b1*c2)-(c1*b2*a3)-(c2*b3*a1)-(c3*b1*a2));
   cout<<"In the Equations:\n";
   cout<<a1<<"x+"<<b1<<"y+"<<c1<<"z="<<d1<<endl;
   cout<<a2<<"x+"<<b2<<"y+"<<c2<<"z="<<d2<<endl;
   cout<<a3<<"x+"<<b3<<"y+"<<c3<<"z="<<d3;
   cout<<"\n\n";
   cout<<"X="<<x<<", Y="<<y<<", Z="<<z;
   cout<<"\n\n";
   cout<<"If you want to solve more equations press 1, if you want to go back\n";
   cout<<" to the Main Menu press 2-";
   cin>>m;
   clrscr();
   if (m==1)
   equation();
   if (m==2)
   menu();
   if (m>2)
   equationerror1();
   }
      void equationerror1()
   {
   int c;
   cout<<"Error Please only press 1 or 2.\n";
   cout<<endl;
   cout<<"If you want to solve more equations press 1, if you want to go back\n";
   cout<<" to the Main Menu press 2-";
   cin>>c;
   clrscr();
   if (c==1)
   equation();
   if(c==2)
   menu();
   if (c>2)
   equationerror2();
   }
   void equationerror2()
   {
   int d;
   cout<<"Error Please only press 1 or 2.\n";
   cout<<endl;
   cout<<"If you want to solve more equations press 1, if you want to go back\n";
   cout<<" to the Main Menu press 2-";
   cin>>d;
   clrscr();
   if (d==1)
   equation();
   if(d==2)
   menu();
   if (d>2)
   equationerror1();
   }

C++ Program to display Consonants and Vowels output from an entered string

C++ Program to display  Consonants and Vowels output from an entered string


#include <iostream.h>
#include <string.h>
#include <conio.h>
void main()


    {   clrscr();
    int consonant =0;
    int vowel =0;
    int k =0;
    char response[20];

    cout<<"Please enter a word "<<endl;
    cin>>response;
    while (k<20 && response[k] != '\0')


    {
        switch(response [k])


        {
        case 'a':
        case 'A':
        case 'e':
        case 'E':
        case 'i':
        case 'I':
        case 'o':
        case 'O':
        case 'u':
        case 'U':
            ++vowel;
            break;
        default:
            ++consonant;
            break;
        }
        ++k;
    }
    cout<<"There are "<<vowel<<" vowel(s) in your string"<<endl;
    cout<<"There are "<<consonant<<" consonant(s)in your string"<<endl;
    cout<<"Press any key to continue"<<endl;
    getch();
    }

C++ program to creates a magic square box of order n x n. The speciality of a magic square box is that the sum of elements of any row or any column is always equal

C++ program to creates a magic square box of order n x n. The speciality of a magic square box is that the sum of elements of any row or any column is always equal


#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <stdlib.h>
#include <alloc.h>
int arr[10][10], n, a,b,i;
void magic(int , int , int, int);

void main()
{
int j,x,k;
clrscr();
printf("\n Enter size of matrix (n X n, where n is odd):");
scanf("%d", &n);
printf("\n Enter 1st element of the %d sequential elements:",n*n);
scanf("%d", &x);
for (i=0; i<n; i++)
{
for (k=0; k<n; k++)
{
arr[i][k]=-1;
}
}
arr[0][n/2]=x;

magic(0,n/2, x, n);
printf("\n");
for (i=0; i<n; i++)
{
for (k=0; k<n; k++)
{
printf(" %3d ", arr[i][k]);
}
printf("\n");
}
getch();
}


void magic( int a, int b, int x, int n)
{
int c;
c=1;
for (x=x+1,i=2; i<=n*n; i++,c++,x++)
{
if(c<n)
{
a=a-1;
b=b-1;
if (a==-1)
a=n-1;
if (b==-1)
b=n-1;
}
else
{
a=a+1;
c=0;
}
arr[a][b]=x;
}

}

C++ Program to insert comma at appropriate position in an entered (max-15) digit number as per the mathematical theory

C++ Program to insert comma at appropriate position in an entered (max-15) digit number as per the mathematical theory


#include <iostream.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
void AddCommas( char * In, char * Out );
int main()


    {
    clrscr();
    char Input[17];
        char Output[21];
        printf("Please enter a number (max 15 char) ");
        cin.get(Input,16);
    AddCommas(Input, Output);
    cout<<Output<<endl;
    system("pause");
    getch();
        return 0;
}
void AddCommas( char * In, char * Out )


    {
    int nLength = strlen(In);
    int incr;
    int nCount;
    int nPosition = 0;
    if( (nLength % 3) == 0 )
        nCount = 0;
    else


        {
            if( ((nLength % 3) == 1) && (In[0] == '-') )
                nCount = -1;
            else
                nCount = 3 - (nLength % 3);
        }
    for ( incr = 0; incr < nLength; incr++)


        {
            if( nCount == 3 )


                {
                    Out[nPosition] = ',';
                    nPosition++;
                    nCount = 0;
                }
                Out[nPosition] = In[incr];
                nPosition++;
                nCount++;
            }
            Out[nPosition] = 0; //terminating zero
        }

C++ Program to input a number and check if it is a Magic Number

C++ Program to input a number and check if it is a Magic Number


#include<iostream.h>
#include<conio.h>
main()
{
clrscr();
int num,num1,i,j,k,flag=0,n,sum=0,c=0;
cout<<"enter any no.";
cin>>num;
num1=num;
while(flag==0)
{
while(num!=0)
{
n=num%10;
sum=sum+n;
num=num/10;
c=c+1;
}
if (c==1)
{
if (sum==1) cout<<num1<<" is a magic number";
else
cout<<num1<<" is not a magic number";
flag=1;}
num=sum;
sum=0;
c=0;
}

getch();
}

c++ Program to return the inverse of any entered number

C++ Program to return the inverse of any entered number


# include <iostream.h>
#include<conio.h>
int function(int g);
int main()
{
 int n,
     exit;
 cout <<"this program will give the inverse of any number:"<<"\t";
 cin>>n;
 function(n);
 cout<<"\n"<<"print exit to quit this program:"<<"\t";
 getch();
 return 0;
}

int function(int g)
{
 int p=1,
     f,
     i;
 while (p!=0)
{
 p=g/10;
 f=p*10;
 i=g-f;
 g=p;
 cout<<i;
}

 return i;
}

C++ Program to depict the process of linear search


C++ Program to depict the process of linear search


 #include<iostream.h>
#include<process.h>
#include<conio.h>
main()
{
clrscr();
int num[10],m;
cout<<"Enter any ten numbers as elements of an array"<<endl;
for (int i=0;i<10;i++)
{
cin>>num[i];
}
cout<<endl<<"Enter any number ";
cin>>m;
cout<<endl;
for (int j=0;j<10;j++)
{
if (num[j] == m)
{cout<<m<<" is present in the array"<<endl;
getch();
exit(0);}
}
getch();
return 0;
}

C++ Program for a Lottery Game

C++ Program for a Lottery Game



#include <iostream.h>
#include <stdlib.h>
#include <string.h>
#include <fstream.h>
#include <conio.h>


int fivemillion_winner()
{
ofstream fout;
char name[50];
char date[9];

fout.open("c:/windows/desktop/Lottery Jackpot Winners.txt", ios::out | ios::app);
cout << "Congratulations.  Enter the name you want in the high score book  ";
  cout << "\n\nEnter your Highscore name:  ";
  cin  >> name;

  cout << "\nEnter the date(dd/mm/yy):  ";
  cin  >> date;
  fout << date << "  -  " << name << endl;
  cout << "\nThankyou!";
fout.close();
return 0;
}

int main()
{
clrscr();
srand((unsigned)time(NULL));
  int player_num = 1;
  int p1guess = 0;
  int p2guess = 0;
  char over = 'y';
  int winner = rand() % 100 +1;

cout << "Welcome to the Lottery Game!!!!  The game where you can win prizes!!!.  " << endl;
cout << "Here are the Prizes\n\n";
cout << "  1. $5,000,000 - Number Bang On" << endl;
cout << "  2. $2,500,000 - Number One Off" << endl;
cout << "  3. $1,000,000 - Number Two Off" << endl;
cout << "  4. $500,000   - Number Three Off" << endl;
cout << "  5. $250,000   - Number Four Off" << endl << endl;
cout << "If you are more that five numbers off you win nothing...\n\n";

while (over != 'n') {
tryagain:;
cout << "Enter the number of Players(1 or 2):  ";
cin  >> player_num;

if (player_num > 2) {
    cout << "\nI SAID 1 OR 2!! NOT " << player_num << "!!!\n";
    goto tryagain;
    }
if (player_num == 1) {
    cout << "\nOne Player it is!!";
    cout << "\n\nEnter what you think the number is.  Between 1 and 100:  ";
    cin  >> p1guess;

       if (p1guess == winner) {
           cout << "\nTHATS THE EXACT NUMBER!! YOU WIN $5,000,000!!\n\n";
           fivemillion_winner();
           }
  else if ((p1guess == winner + 1) || (p1guess == winner - 1))
           cout << "\nYOU WIN $2,500,000.  The First Place Winner was:  " << winner << "\n\n";
  else if ((p1guess == winner + 2) || (p1guess == winner - 2))
           cout << "\nYOU WIN $1,000,000.  The First Place Winner was:  " << winner << "\n\n";
  else if ((p1guess == winner + 3) || (p1guess == winner - 3))
           cout << "\nYOU WIN $500,000.  The First Place Winner was:  " << winner << "\n\n";
  else if ((p1guess == winner + 4) || (p1guess == winner - 4))
           cout << "\nYOU WIN $250,000.  The First Place Winner was:  " << winner << "\n\n";
     else
           cout << "Sorry.  You don't win anything.  The Winning Number was:  " <<winner << "\n\n";
  }

if (player_num == 2) {
    cout << "\nTwo Player it is!!";
    cout << "\n\nPlayer 1:  Enter what you think the number is.  Between 1 and 100:  ";
    cin  >> p1guess;
    cout << "\n\nPlayer 2:  Enter what you think the number is.  Between 1 and 100:  ";
    cin  >> p2guess;

       if (p1guess == winner) {
           cout << "\nTHATS THE EXACT NUMBER PLAYER 1!! YOU WIN $5,000,000!!\n";
           fivemillion_winner();
           }
  else if ((p1guess == winner + 1) || (p1guess == winner - 1))
           cout << "\nYOU WIN $2,500,000 PLAYER 1.  The First Place Winner was:  " << winner << "\n\n";
  else if ((p1guess == winner + 2) || (p1guess == winner - 2))
           cout << "\nYOU WIN $1,000,000 PLAYER 1.  The First Place Winner was:  " << winner << "\n\n";
  else if ((p1guess == winner + 3) || (p1guess == winner - 3))
           cout << "\nYOU WIN $500,000 PLAYER 1.  The First Place Winner was:  " << winner << "\n\n";
  else if ((p1guess == winner + 4) || (p1guess == winner - 4))
           cout << "\nYOU WIN $250,000 PLAYER 1.  The First Place Winner was:  " << winner << "\n\n";
    else
           cout << "Sorry.  You don't win anything player 1.  The Winning Number was:  " <<winner << "\n\n";

       if (p2guess == winner) {
           cout << "\nTHATS THE EXACT NUMBER PLAYER 2!! YOU WIN $5,000,000!!\n";
           fivemillion_winner();
           }
  else if ((p2guess == winner + 1) || (p2guess == winner - 1))
           cout << "\nYOU WIN $2,500,000 PLAYER 2.  The First Place Winner was:  " << winner << "\n\n";
  else if ((p2guess == winner + 2) || (p2guess == winner - 2))
           cout << "\nYOU WIN $1,000,000 PLAYER 2.  The First Place Winner was:  " << winner << "\n\n";
  else if ((p2guess == winner + 3) || (p2guess == winner - 3))
           cout << "\nYOU WIN $500,000 PLAYER 2.  The First Place Winner was:  " << winner << "\n\n";
  else if ((p2guess == winner + 4) || (p2guess == winner - 4))
           cout << "\nYOU WIN $250,000 PLAYER 2.  The First Place Winner was:  " << winner << "\n\n";
       else
           cout << "Sorry.  You don't win anything player 2.  The Winning Number was:  " <<winner << "\n\n";
  }
cout << "Would you like to try again(y/n)??  ";
cin  >> over;
system("cls");
  }
  getch();
  return 0;
}

C++ Program to combine two arrays and sort them in order in one

C++ Program to combine two arrays and sort them in order in one


#include <stdio.h>
#include <conio.h>

#define NMAX 10

void printIntArray(int a[], int n);
void merge(int c[], int *nc, int a[], int na, int b[], int nb);

int main(void) {
  int x[NMAX] = {1,3,5,6,7}; /* The first sorted sequence */
  int y[NMAX] = {2,3,4}; /* The second sorted sequence */
  int z[NMAX+NMAX]; /* The merge sequence */
  int nz;

  merge(z,&nz,x,5,y,3);
  printIntArray(z,nz);
  getch();
}

void printIntArray(int a[], int n)
     /* n is the number of elements in the array a.
      * These values are printed out, five per line. */
{
  int i;

  for (i=0; i<n; ){
    printf("\t%d ", a[i++]);
    if (i%5==0)
      printf("\n");
  }
  printf("\n");
}

void merge(int c[], int *nc, int a[], int na, int b[], int nb){
  int cursora, cursorb, cursorc;

  cursora=cursorb=cursorc=0;

  while((cursora<na)&&(cursorb<nb))
    if (a[cursora]<=b[cursorb])
      c[cursorc++]=a[cursora++];
    else
      c[cursorc++]=b[cursorb++];

  while(cursora<na)
    c[cursorc++]=a[cursora++];

  while(cursorb<nb)
    c[cursorb++]=b[cursorb++];

  *nc = cursorc;
}

C++ Program to implement Linked List

C++ Program to implement Linked List


#include <iostream.h>
#include <conio.h>
struct node
{
   node* ptr;
   int data;
};

class list
{
   node* lastptr;

    public:
   list();
   ~list();
   void add(int);
   void remove(int);
   void display();
   node* searchnode(int);
   void releasenode();   
};

list::list()
{
   lastptr='\0';
}

list::~list()
{
   while (lastptr)
   {
      node* thisptr=lastptr;
      lastptr=lastptr->ptr;
      delete thisptr;
   }
}

void list::add(int value)
{
   node* thisptr=new(node);
   thisptr->data=value;
   thisptr->ptr=lastptr;
   lastptr=thisptr;
}

node* list::searchnode(int value)
{
   node* thisptr=lastptr;
   while (thisptr)
   {
      if (thisptr->data==value)
         break;
      thisptr=thisptr->ptr;
   }
   return thisptr;
}

void list::remove(int value)
{
   node* thisptr=searchnode(value);

   if (!thisptr) return;
   while (thisptr->ptr)
   {
      node* pptr=thisptr->ptr;
      thisptr->data=pptr->data;
      thisptr=thisptr->ptr;
   }
   releasenode();
}

void list::releasenode()
{
   node* thisptr=lastptr;
   node* previousptr;
   while (thisptr->ptr)
   {
      previousptr=thisptr;
      thisptr=thisptr->ptr;
   }
   if (thisptr==lastptr) lastptr='\0';
   else previousptr->ptr='\0';
   delete thisptr;
}

void list::display()
{
   node* thisptr=lastptr;

   //Loop until this pointer equal to NULL.
   while (thisptr)
   {
      cout<<"Data : "<<thisptr->data<<"\n";
      cout<<"Address : "<<thisptr->ptr<<"\n";
      thisptr=thisptr->ptr;
   }
}

void main()
{
   list linklist;
   
   //add number(1 until 7) into link list.
   linklist.add(1);      
   linklist.add(2);
   linklist.add(3);
   linklist.add(4);
   linklist.add(5);
   linklist.add(6);
   linklist.add(7);
       getch();
   //remove number 4 from the list.
   linklist.remove(4);

   //display the number in descending order.
   linklist.display();
   cout<<endl;
   cout<<"Press any key to continue"<<endl;
   getch();
}

C++ Program to accept a number x. Count number of digits in x and store it in n, form y that has the number of digits n at ten's place and the most significant digit of x at one's place. Display y. sample input: x= 16833, y = 51, since no.of digits of x is 5 and 1 is the msd of x

C++ Program to accept a number x. Count number of digits in x and store it in n, form y that has the number of digits n at ten's place and the most significant digit of x at one's place. Display y. sample input: x= 16833, y = 51, since no.of digits of x is 5 and 1 is the msd of x


 #include<conio.h>
#include<iostream.h>
main()
{
clrscr();
int x,y,z,i,n=0,num, msd, digit;
cout<<"Enter any number as x"<<endl;
cin>>x;
// computing number of digits.
num=x;
while(num>0)
{
digit=num%10;
n=n+1;
num=num/10;
}
msd=digit;
y=n*10+msd;
cout<<"The value of Y is "<<y;
getch();
return 0;
}

C++ Program for Data File Manipulation of data entry/storage of employees , Writing Structures in a data file

C++ Program for Data File Manipulation of data entry/storage of employees , Writing Structures in a data file


#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
#include<fstream.h>
// Definition of the structure
struct employee
{
int empcode;
char name[20];
int hrworked;
float rate;
};
main()
{
clrscr();
int n;
employee emp[100];
ofstream file1;
file1.open("employee.dat");
cout<<"Enter number of employees ";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"Enter employee code";
cin>>emp[i].empcode;
cout<<endl;
cout<<"Enter employee name";
cin>>emp[i].name;
cout<<"Enter hours worked";
cin>>emp[i].hrworked;
cout<<endl<<"Enter rate";
cin>>emp[i].rate;
file1.write((char *) & emp[i],sizeof(employee));
}
file1.close();
ifstream file2;
file2.open("employee.dat");
i=1;
//file1.seekg(0);
while(file2)
{
file2.read((char*) &emp[i],sizeof(employee));
cout<<"Wage is "<<emp[i].hrworked*emp[i].rate;
}
file2.close();
cout<<"Press any key to continue";
getch();
return 0;
}

C++ Program to implement bubble sort for a multi-dimensional array

C++ Program to implement bubble sort for a  multi-dimensional array[10][20].The array is stored through random generation of numbers between the defined range 1 to 500


#include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>
#include <time.h>
#include <process.h>
#include <conio.h>
#define MAX 500 //Highest possible value that
#define MIN 1 //Lowest possible value that
#define ORDER < //How you want the list sorted.
void main()


    {
    clrscr();
    int i, j, l, array[10][20];
    int row, col, x, y=0, z;
    int temp, moves=0;
    srand((unsigned) time(NULL));
    x=101;

    for(row = 0; row < 10; row++)


    {
        for(col = 0; col < 20; col++)


        {
         x = rand() % MAX + MIN;
         array[row][col] = x;
        }
    }

        for(row = 0; row < 10; row++) //displays the
        //unsorted array


            {
                for(col = 0; col < 20; col++)


                    {
                     cout<<setw(3)<<array[row][col]<<" ";
                    }
                    cout<<endl;
            }
            cout<<"This is the original array"<<endl;
            cout<<"Enter any key"<<endl;
            cin>>l;
        system("cls"); //Clears the screen of the
            //unsorted array
            for(i = 0; i < 200; i++)


                {   
                    z=1;
                   
                for(j = 0; j < 199; j++)   


                        {
                         if(array[y][z] ORDER array[y][z - 1])


                             {
                             temp = array[y][z];
                                array[y][z] = array[y][z - 1];
                                array[y][z - 1]=temp;
                                moves++;
                             }
                             z++;
                            }
                    }
                    for(row = 0; row < 10; row++) //displays the
                    //sorted array


                        {
                            for(col = 0; col < 20; col++)


                                {
                                 cout<<setw(3)<<array[row][col]<<" ";
                                }
                                cout<<endl;
                        }
            cout<<moves<<" moves where made to sort this list"<<endl;
            cout<<"Press any key to continue"<<endl;
            getch();
            }

Wednesday, 6 June 2012

C++ Program to implement Function Overloading

C++ Program to implement Function Overloading


#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
// declaration of overloaded functions
void rectangle(int,char);
void rectangle(int,int,char);
main()
{
clrscr();
rectangle(2,5,'@');
rectangle(1,'a');
rectangle(2,'b');
rectangle(3,'c');
rectangle(4,'d');
cout<<"Press any key to continue";
getch();
return 0;
}

void rectangle(int n,char ch)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<ch;
}
cout<<endl;
}
}
void rectangle(int l,int b, char ch)
{
for(int m=0;m<l;m++)
{
for(int n=0;n<b;n++)
{
cout<<ch;
}
cout<<endl;
}
}

C++ Program to Searching element in an array using binary search

C++ Program to Searching element in an array using binary search


#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
main()
{
clrscr();
int marks[100],x=0,i=0,n=0,middle=0,pos=0,first=0;
cout<<"Enter the number of elements required for storage";
cout<<endl;
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter value ";
cin>>marks[i];
}
char op='y';
while ((op=='y') || (op=='Y'))
{
int last = n-1;
pos=-1;
cout<<"Enter the element to be searched";
cout<<endl;
cin>>x;
while((pos==-1)&&(first<=last))
{
// Application of binary search
middle=(first+last)/2;
if (marks[middle]==x)
{
pos=middle;
}
if(marks[middle]<x)
first=middle+1;
else
last=middle-1;
}
if (pos==-1)
cout<<"Sorry not present in the list"<<endl;
else
cout<<"The element lies in the array at position"<<pos+1;
cout<<endl;
cout<<"Enter option y/n";
cin>>op;
}
getch();
return 0;
}


C++ Program to Adding two angles

C++ Program to Adding two angles


#include<iostream.h>
#include<conio.h>
#include<stdio.h>
// Definition of the structure angle
struct angle
{
// two member variables
int degrees;
int minutes;
};
angle sumangle(angle, angle);
// function with structure as arguments and returning structures.
main()
{
clrscr();
angle pangle[3];
for(int i=0;i<2;i++)
{
cout<<"Enter angle in degrees and minutes";
cin>>pangle[i].degrees;
cin>>pangle[i].minutes;
}
// Data entered in two array cells
pangle[2]=sumangle(pangle[0],pangle[1]);
cout<<"The sum of two angles is ";
cout<<endl;
cout<<pangle[2].degrees<<"Degrees and ";
cout<<pangle[2].minutes<<"Minutes";
cout<<endl;
cout<<endl;
cout<<"Press any key to continue";
getch();
return 0;
}
// Defining the function
angle sumangle(angle x, angle y)
{
angle z;
z.degrees=x.degrees+y.degrees;
z.minutes=x.minutes+y.minutes;
if(z.minutes>=60)
{
z.minutes=z.minutes-60;
z.degrees=z.degrees+1;
}
return z;
}

C++ Program to display Fibonacci numbers which are prime

C++ Program to display  Fibonacci numbers which are prime


#include<iostream.h>
#include<conio.h>
#include<stdio.h>
// Declaration of the function
int isprime(int);
main()
{
clrscr();
int p,a=1,b=2,c=0;
p=isprime(a);// calling function
if (p==1) cout<<a<<" ";
p=isprime(b);// calling function
if (p==1) cout<<b<<" ";
while(c<=10000)
{
c=a+b;
p=isprime(c);
if((p==1) && (c<=10000))
cout<<c<<" ";
a=b;
b=c;
}
cout<<endl;
cout<<"Press any key to continue";
getch();
return 0;
}
// Defining the function
int isprime(int x)
{
int i;
if (x==1) return 0;
else
{
int pop=1;
for(i=2;i<x;i++)
{
if((x%i==0))
pop=2;
}
return pop;
}
}

C++ Program to conduct: Array sorting Using Insertion Sorting Technique

C++ Program to conduct: Array sorting Using Insertion Sorting Technique.
Program to input any five numbers and print them in descending order


#include<iostream.h>
#include<conio.h>
main()
{
clrscr();
int number[5],j=0,a=0;
clrscr(); // clear the screen
cout<<"Enter the elements of the array"<<endl;
for(int i=1;i<=5;i++)
{
cout<<"Enter number "<<endl;
cin>>number[i];
}
cout<<"The original list is ";
for(i=1;i<=5;i++)
{
cout<<number[i]<<"\t";
}
cout<<endl;
cout<<endl<<"Thank You ! The data entry is over"<<endl;
cout<<"Press any key to continue";
getch();
cout<<"\a\a";
// sorting of data
for(int k=2;k<=5;k++)
{
a=number[k];
j=k-1;
while(a<number[j] && j>0)
{
  number[j+1]=number[j];
  j=j-1;
}
number[j+1]=a;
}
cout<<endl;
cout<<"The list in ascending order is ";
cout<<endl;
for(i=1;i<=5;i++)
cout<<number[i]<<"\t";
cout<<endl<<"Press any key to return to the editor";
getch();
return 0;
}

C++ Program implementing Bubble Sort for an entered range of numbers. Using passing of array for sorting through functions

C++ Program implementing Bubble Sort for an entered range of numbers. Using passing of array for sorting through functions


#include <iostream.h>
#include <conio.h>
int bubbleInput(int n);
double bubbleSort(double a[], int n);
double bubblePass(double a[], int n);
double bubbleSwap(double& i, double& j);
double bubbleOutput(double a[], int n);
int main ()


    {
    int n;
    clrscr();
    cout<<"Please do not exceed above 100."<<endl;
    cout<<"How many numbers would you like to sort: ";
    cin>>n;
    bubbleInput(n);
    cout<<endl;
    return 0;
}
int bubbleInput(int n)


    {
    double a[100];
    for (int i=0; i<n; i++)


        {
            cout<<"Please enter a number to sort: ";
            cin>>a[i];
        }
        bubbleSort(a, n);
        bubbleOutput(a, n);
        return 0;
    }
    double bubbleSort(double a[], int n)


    {
        for (int i=0; i<n; i++)
        bubblePass(a,n);
        return 0;
    }
    double bubblePass(double a[], int n)


    {
        for (int i=0; i<n-1; i++)
        if (a[i] > a[i+1])
        bubbleSwap(a[i], a[i+1]);
        return 0;
    }
    double bubbleSwap(double& i, double& j)


    {
        double temp = i;
        i = j;
        j = temp;
        return 0;
    }
    double bubbleOutput(double a[], int n)


    {


        for (int i=0; i<n; i++) {
            cout << a[i] << " ";
        }
        cout<<" "<<endl;
        cout<<"Press any key to continue"<<endl;
        getch();
        return 0;
        }

C++ Program implementing all four types of sorting of a string

C++ Program implementing all four types of sorting of a string


#include <iostream.h>
#include <string.h>
#include <conio.h>
void MinSort(char*, int);
void BubbleSort(char*, int);
void InsertionSort(char*, int);
void ShellSort(char*, int);
int main()

    {
    clrscr();
    char cr[] = "skdmrsrjsrkpmnoprkseef";
    cout << "Data before sorting is "<<cr << endl;
    ShellSort(cr, strlen(cr));
    cout <<"Now the change is "<< cr << endl;
    getch();
    return 0;
}
// Complexity: n^2
void MinSort(char* array, int n)


    {
    int i, j, temp_index;
    char min;
    for (i=0; i < n-1; ++i)


    {
    temp_index = i;
    min = array[i];
    for (j = i+1; j < n; ++j)


        {
        if (array[j] < min)


        {
             temp_index = j;
             min = array[j];
        }
    }
    array[temp_index] = array[i];
    array[i] = min;
    }
}
// Complexity: n^2
void BubbleSort(char* array, int n)


    {
    int i, j;
    char temp;
    for (i = 0; i < n-1; ++i)
    for (j = i+1; j > 0; --j)
    if (array[j] < array[j-1])


    {
         temp = array[j];
         array[j] = array[j-1];
         array[j-1] = temp;
    }
}
// Complexity: n^2
void InsertionSort(char* array, int n)


    {
    int i, j;
    char temp;
    for (i=1; i < n; ++i)


    {
    temp = array[i];
    j = i-1;
    while ((j >= 0) && (temp < array[j]))


        {
        array[j+1] = array[j];
        j--;
    }
    array[j+1] = temp;
    }
}
// Complexity: n^1.2
void ShellSort(char* array, int n)


    {
    int i, j, k, s, gap_cnt;
    char temp;
    int gaps[5];
    gaps[0] = 9; gaps[1] = 5; gaps[2] = 3; gaps[3] = 2; gaps[4] = 1;
    for (gap_cnt = 0; gap_cnt < 5; gap_cnt++)


    {
    k = gaps[gap_cnt];
    s = -k;
    for (i = k; i < n; ++i)


        {
        temp = array[i];
        j = i-k;
        if (s == 0)


        {
             s = -k;
             s++;
             array[s] = temp;
        }
        while (temp < array[j] && j >= 0 && j <= n) // + Bound checking


        {
                     array[j+k] = array[j];
                     j = j-k;
            }
            array[j+k] = temp;
        }
    }
}

C++ Program will ask the user for a starting number and an ending number and compute the sum of the numbers in between

C++ Program will ask the user for a starting number and an ending number and compute the sum of the numbers in between


#include<iostream.h>
#include<conio.h>

void main()
{
   char Answer;

   do{

   int Number1, Number2;
   long Total = 0;
   clrscr();
   gotoxy(25,11);
   cout<<"Enter starting number: ";
   cin>>Number1;
   gotoxy(25,12);
   cout<<"Enter ending number: ";
   cin>>Number2;

   for(int i = Number1;i <= Number2; i++)
      {
     Total += i;
      }

   clrscr();
   gotoxy(25,11);
   cout<<"The sum of the integers from "<<Number1<<" to "
       <<Number2<<" is "<<Total<<".";
   gotoxy(25,12);
   cout<<"Repeat Y/N?: ";
   cin>>Answer;
   }while( Answer =='Y' || Answer == 'y');

   clrscr();
   gotoxy(25,11);
   cout<<"-press enter to quit-";
   getch();
}

C++ Program to implement calculation on any entered two numbers

C++ Program to implement calculation on any entered two numbers


#include <iostream.h>
#include <conio.h>
float a;
float b;
float result;
int op;
int option;
int main()


    {
    do
    {
        cout << "Enter First Number \n";
        cin >> a; // Input
        cout << "Enter Second Number \n";
        cin >> b;
        cout << "Please enter an Option..\n";
        cout << "[1] Add \n";
        cout << "[2] Substract \n";
        cout << "[3] Multiply \n";
        cout << "[4] Devide \n ";

        cin >> op; // The keybord input is passed to variable 'op'

        switch (op)// switch is similar to Select Case in VB
        {
            case 1 : // Case Addition
                result=a+b;
                break;
            case 2 : //Case Substraction
                result=a-b;
                break;
            case 3 : // Case multiplication
                result=a*b;
                break;
            case 4 : // Case Division
                result=a/b;
        }
        cout <<"The Result is..= " << result << "\n\n"; //Display Result and Skipping two Lines
        cout << "Enter an Option..\n";
        cout << "[0] Exit.\n";
        cout << "[1] Continue. \n";
        cin >> option;
        // Do While loop Ends Here
    } while (option==1);    // If option is 1 ie 'Continue' the above code is Looped else ( option<>1 ) it is Escaped from Loop

    return 0;// the ' main' function returns value 0
    }

C++ Program for computation of:Natural Log (Ln),Log with Base 10(Log10),Log of your desired Base (LogA^x)

C++ Program for computation of:

1). Natural Log (Ln)
2). Log with Base 10(Log10)
3).Log of your desired Base (LogA^x)


#include<iostream.h>
#include<iomanip.h>
#include<stdlib.h>
#include<dos.h>
#include<conio.h>
#include<math.h>

class logarithem


    {
    protected:
    long ans;
    public:
     logarithem():ans(0)


         {
         /* cout<<"Enter the value of 'X':";
        cin>>ans; */
         }
        double loga(double);
        double ln(double x);
        double checking(double);
    };
    double logarithem::loga(double b)


    {
    double LOG10=0,c;
    c=ln(b);
    LOG10=c/2.30258509299404;
    return LOG10;
    }
    double logarithem:: ln(double x)


    {
        double lnX=0.000001,check;


        do{
            lnX=lnX-(exp(lnX)-x)/exp(lnX);
            check=checking(lnX);
         }while(check!=0);
        return lnX;
    }
    double logarithem::checking(double x)


        {
        static double x1=0;
        double x2;
         if(x1==0)


            {
             x1=x;
             return 1;
            }
             else


             {
                 x2=x1-x;
                 if(x2<0)
                x2*=-1;
                x1=x;
            return x2;
             }
        }
        void main()


            {
            clrscr();
            logarithem a;
            double b=0,k=0;
            int opt1;
            char ch;
                clrscr();
                highvideo();
                textcolor(BLACK);
                textbackground(3);
                one:
                cout<<"\n1. For Natural Log (ln) ";
                cout<<"\n2. For Log of Base 10";
                cout<<"\n3. For Log of x with Base a, Log a^x = Logx/Loga";
                cout<<"\n4. For Exit";
                cout<<"\n ";
                cout<<"\n\n\n\tEnter Your Option:- ";
                cin>>opt1;
             switch(opt1)


                 {
                 case 2:
                    cout<<"\n\nEnter the value of X : ";
                    cin>>b;
                    cout<<"\n\n\t( Log_10^"<<b<< " is "<<a.loga(b)<<" )";
                    cout<<endl<<"\n\tPress any key to go back....";
                    ch=getch();
                    if(ch=='\r')
                    clrscr();
                    goto one;
                 case 1:
                     cout<<"\n\nEnter the value of X"<<"\t";
                     cin>>b;
                     cout<<"\n\n\t( Ln "<<b<<" : "<<a.ln(b)<<" )";
                     cout<<endl<<"\n\tPress any key to go back....";
                     ch=getch();
                     if(ch=='\r')
                     clrscr();
                     goto one;
                 case 3:
                     cout<<"\n\nEnter the value of a :"<<"\t";
                     cin>>b;
                     cout<<"\nEnter the value of x :"<<"\t";
                     cin>>k;
                     cout<<"\n\n\t( Log a^x = Logx/Loga "<<b<<" : "<<a.ln(k)/a.ln(b)<<" )";
                     cout<<endl<<"\n\tPress any key to go back....";
                     ch=getch();
                     if(ch=='\r')
                     clrscr();
                     goto one;
                 case 4:
                    delay(100);
                    exit(0);
                 default:
                    cout<<"\n\n\tPlease! Enter the options shown above ...";
                    cout<<endl<<"\n\tPress any key to go back....";
                    ch=getch();
                    if(ch=='\r')
                    clrscr();
                    goto one;
                 }

            }

C++ Program to compute surface area of cone, cylinder, vol. of cone etc

C++ Program to compute surface area of cone, cylinder, vol. of cone etc


#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
void SAofcone();
void SAofCylinder();
void Volumeofcone();
void beginning();
void SAofCone()
     {
    clrscr();
    cout<<"This program calculates the SA of a cone. it uses 3.14 for pi."<<endl;
    float pi;
    pi= 3.14;
    float r;
    cout<< "Enter the radius of the base of the cone?"<<endl;
    cin>> r;
    float basearea;
    basearea= (r*r*pi);
    float height;
    cout<<"Enter the height of the cone?"<<endl;
    cin>> height;
    float SA;
    SA= (basearea)+(pi*r*height);
    cout<<endl;
    cout<<"The answer is:";
    cout<< SA;
    system("pause");
    beginning();
}
void SAofCylinder()


    {
    cout<<"This program calculates the surface area of a cylinder,"<<endl;
    cout<<"it uses 3.14 for pi."<<endl;
    float r;
    cout<<"Enter the radius of the base?"<<endl;
    cin>> r;
    float pi;
    pi= 3.14;
    float height;
    cout<<"Enter the height of the circle?"<<endl;
    cin>>height;
    float circumfrence;
    circumfrence= r*2*pi;
    float SA;
    SA= 2*(3.14)*(r*r)+2*(3.14)*(r)*(height);
    cout<<"The compute result is approximately:";
    cout<< SA;
    cout<<""<<endl;
    system("pause");
    beginning();
}
void Volumeofcone()


    {
    cout<<"This program calculates the volume of a cone. it used 3.14 for pi."<<endl;
    cout<<""<<endl;
    float r;
    cout<<" Enter the radius of the base?"<<endl;
    cin>> r;
    float pi;
    pi= 3.14;
    float height;
    cout<<"Enter the height of the cone?"<<endl;
    cin>> height;
    float pirsq;
    pirsq= pi*r;
    float volume;
    volume= (.333333)*(pi)*(r*r)*(height);
    cout<<"Enter the volume of the cone is approxmately:";
    cout<< volume;
    cout<<""<<endl;
    system("pause");
    beginning();
}
int main(void)


    {
    clrscr();
    beginning();
}
void beginning()
    {
    cout<<"Press any key to continue"<<endl;
    getch();
    clrscr();
    cout<<"------------------------------------\n";
    cout<<"----**OPTIONS** --------------------\n";
    cout<<"------1. find the SA of a cone------\n";
    cout<<"------2. find the SA of a cylinder--\n";
    cout<<"------3. find the volume of a cone--\n";
    cout<<"------------------------------------\n";
    cout<<"----- 4. Exit-----------------------"<<endl;
    int choice = 0;
    cin>> choice;
    if (choice == 1)
    SAofCylinder();
    if (choice == 2)
    SAofCone();
    if (choice == 3)
    Volumeofcone();
    if (choice == 4)
    exit(0);
}

C++ Program to implement Calculator functions for : +, -, *, /, ^

C++ Program to implement Calculator functions for : +, -, *, /, ^


#include <iostream.h>
#include <conio.h>
float inputOne, inputTwo, answer;
char operator_, yn;
int main()


    {
    textcolor(LIGHTBLUE);
    while (yn != 'n')


        {
            clrscr();
            cout << "First number + - / * ^ second number\n";
            cin >> inputOne >> operator_ >> inputTwo;
            if (operator_ == '+')
                answer = inputOne + inputTwo;
            if (operator_ == '-')
                answer = inputOne - inputTwo;
            if (operator_ == '*')
                answer = inputOne * inputTwo;
            if (operator_ == '/')


                {
                if (inputTwo == 0)


                        {
                        cout << "Cannot divide by 0";
                        }
                    else
                        answer = inputOne / inputTwo;
                    }
                if (operator_ == '^')


                    {
                    answer = inputOne;
                    for (int i=2; i<=inputTwo; i++)
                        answer = answer * inputOne;
                    }
                cout << endl;
                cout << inputOne << " " << operator_ << " ";
                cout << inputTwo << " = " << answer;
                cout << "\n\nSolve another? <yn> ";
                cin >> yn;
                cout << "\n";
            }
            return 0;
        }

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More