Wednesday 10 April 2013

program using functions to ask user enter to number and choice its option of mathematical operation to perform in a loop and loop only breaks if user enter exit option


#include<iostream.h>
#include<conio.h>
using namespace std;
int add(int,int);
int sub(int,int);
int mul(int,int);
int divi(int,int);
main()
{
      int number,num1,sum,num2,i=1;
      while(i>0)
      {
      cout<<"\n1. ADD NUMBERS"<<endl;
      cout<<"2. SUBSTRAT NUMBERS"<<endl;
      cout<<"3. MULIPLY NUMBERS"<<endl;
      cout<<"4. DIVIDE NUMBERS"<<endl;
      cout<<"5. EXIT"<<endl;
      cout<<"\n Enter Your Choice :"<<endl;
      cin>>number;
      switch(number)
      {
                    case 1:
                         {
                                 cout<<"\nADDITION"<<endl;
                                 cout<<"\nEnter 1st Number :"<<endl;
                                 cin>>num1;
                                 cout<<"Enter 2nd Number :"<<endl;
                                 cin>>num2;
                                 sum=add(num1,num2);
                                 cout<<"\nAnswer = "<<sum<<endl;
                                 break;
                                 }
                     case 2:
                         {
                                 cout<<"\nSUBSTRACTIN"<<endl;
                                 cout<<"\nEnter 1st Number :"<<endl;
                                 cin>>num1;
                                 cout<<"Enter 2nd Number :"<<endl;
                                 cin>>num2;
                                 sum=sub(num1,num2);
                                 cout<<"\nAnswer = "<<sum<<endl;
                                 break;
                                 }
                     case 3:
                         {
                                 cout<<"\nMULTIPLICATION"<<endl;
                                 cout<<"\nEnter 1st Number :"<<endl;
                                 cin>>num1;
                                 cout<<"Enter 2nd Number :"<<endl;
                                 cin>>num2;
                                 sum=mul(num1,num2);
                                 cout<<"\nAnswer = "<<sum<<endl;
                                 break;
                                 }
                     case 4:
                         {
                                 cout<<"\nDIVISION"<<endl;
                                 cout<<"\nEnter 1st Number :"<<endl;
                                 cin>>num1;
                                 cout<<"Enter 2nd Number :"<<endl;
                                 cin>>num2;
                                 sum=divi(num1,num2);
                                 cout<<"\nAnswer = "<<sum<<endl;
                                 break;
                                 }
                      case 5:
                         {
                                 exit(0);
                                 }
                                 i++;
      }
      }
getch();
}                  
int add(int a, int b)
{
    int d;
    d=a+b;
    return d;
}
int sub(int e, int f)
{  
    int g;
    g=e-f;
    return g;
}
int mul(int h, int i)
{  
    int j;
    j=h*i;
    return j;
}
int divi(int k, int l)
{  
    int m;
    m=k/l;
    return m;
}    

program to find factorial of number,number prime or not and number even or odd through functions


#include<iostream.h>
#include<conio.h>
using namespace std;
void fact(int);
void prime(int);
void even_odd(int);
main()
{
      int number,num1,sum,num2,i=1;
      while(i>0)
      {        
      cout<<"\n1. FACTORIAL OF A NUMBER"<<endl;
      cout<<"2. NUMBER IS PRIME OR NOT"<<endl;
      cout<<"3. NUMBER IS EVEN OR ODD"<<endl;
      cout<<"4. EXIT"<<endl;
      cout<<"\n Enter Your Choice :"<<endl;
      cin>>number;
      switch(number)
      {
                    case 1:
                         {
                                 system("cls");
                                 cout<<"\nfactorial of a number"<<endl;
                                 cout<<"\nEnter Number :"<<endl;
                                 cin>>num1;
                                 fact(num1);
                                 break;
                                 }
                     case 2:
                         {
                                 system("cls");
                                 cout<<"\nNUMBER IS PRIME OR NOT"<<endl;
                                 cout<<"\nEnter 1st Number :"<<endl;
                                 cin>>num1;
                                 prime(num1);
                                 break;
                                 }
                     case 3:
                         {
                                 system("cls");
                                 cout<<"\nNUMBER IS EVEN OR ODD"<<endl;
                                 cout<<"\nEnter Number :"<<endl;
                                 cin>>num1;
                                 even_odd(num1);
                                 break;
                                 }
                      case 4:
                         {
                                 exit(0);
                                 }
                                 i++;
      }
      }
getch();
}                  
void fact(int a)
{
    int f=1,i=1;
    while(i<=a)
    {
          f=f*i;
          i++;    
    }
    cout<<"\n"<<a<<"! = "<<f<<endl;
}
void prime(int num)
{  
     int i=2;
     while(i<=num-1)
     {
   
     if(num%i==0)
     {
     cout << "\n" << num << " is not a prime number.\n";
     break;
     }
   
     i++;
     }
     if(num==1)
     {cout << "\n" << num << " is a prime number.\n";}
     if(i==num)
     cout << "\n" << num << " is a prime number.\n";
}
void even_odd(int e)
{  
    if(e%2==0)
    {cout<<e<<" is even number"<<endl;}
    else
    {cout<<e<<" is odd number"<<endl;}
}

Write a menu driven program using switch case statement (without functions) that has the following options: 1. Factorial of a number 2. Prime or not 3. Odd or even 4. Power ( x raise to power y) 5. Exit Input should be taken inside the particular case. For example ifthe user enters choice is 4. Then in case 4 your program can prompt the user to input value of x and then value of y. The switch case statement and the menu (mentioned above) should be in while(1) loop. The only way to end the program is that the user has to selection choice 5. In case 5 you can simply call a function exit (0);


#include<iostream.h>
#include<conio.h>
#include<math.h>
main()
{
      int number,num1,sum,num2,i=1;
      while(i>0)
      {
      cout<<"\n1. FACTORIAL OF A NUMBER"<<endl;
      cout<<"2. NUMBER IS PRIME OR NOT"<<endl;
      cout<<"3. NUMBER IS EVEN OR ODD"<<endl;
      cout<<"4. POWER(x raise to power y)"<<endl;
      cout<<"5. EXIT"<<endl;
      cout<<"\n Enter Your Choice :"<<endl;
      cin>>number;
      switch(number)
      {
                    case 1:
                         {
                                 system("cls");
                                 cout<<"\nfactorial of a number"<<endl;
                                 cout<<"\nEnter Number :"<<endl;
                                 cin>>num1;
                                 int f=1,i=1;
                                 while(i<=num1)
                                 {
                                 f=f*i;
                                 i++;    
                                 }
                                 cout<<"\n"<<num1<<"! = "<<f<<endl;
                                 break;
                                 }
                     case 2:
                         {
                                 system("cls");
                                 cout<<"\nNUMBER IS PRIME OR NOT"<<endl;
                                 cout<<"\nEnter Number :"<<endl;
                                 cin>>num1;
                                 int j=2;
                                 while(j<=num1-1)
                                 {
                                 if(num1%j==0)
                                 {
                                 cout << "\n" << num1 << " is not a prime number.";
                                 break;
                                 }
                                 i++;
                                 }
                                 if(j==num1)
                                 cout << "\n" << num1 << " is a prime number.";
                                 break;
                                 }
                     case 3:
                         {
                                 system("cls");
                                 cout<<"\nNUMBER IS EVEN OR ODD"<<endl;
                                 cout<<"\nEnter Number :"<<endl;
                                 cin>>num1;
                                 if(num1%2==0)
                                 {cout<<num1<<" is even number"<<endl;}
                                 else
                                 {cout<<num1<<" is odd number"<<endl;}
                                 break;
                                 }
                     case 4:
                         {
                                 system("cls");
                                 cout<<"\nPOWER(x raise to power y)"<<endl;
                                 cout<<"\nEnter value of x :"<<endl;
                                 cin>>num1;
                                 cout<<"Enter value of y :"<<endl;
                                 cin>>num2;
                                 sum=pow(num1,num2);
                                 cout<<"\n"<<num1<<" raise to power "<<num2<<" = "<<sum<<endl;
                                 break;
                                 }
                      case 5:
                         {
                                 exit(0);
                                 }
                                 i++;
      }
      }
getch();
}                  

program to ask user enter to number and choice its option of mathematical operation to perform in a loop and loop only breaks if user enter exit option


#include<iostream.h>
#include<conio.h>
using namespace std;
main()
{
      int number,num1,sum,num2,i=1;
      while(i>0)
      {
      cout<<"\n1. ADD NUMBERS"<<endl;
      cout<<"2. SUBSTRAT NUMBERS"<<endl;
      cout<<"3. MULIPLY NUMBERS"<<endl;
      cout<<"4. DIVIDE NUMBERS"<<endl;
      cout<<"5. EXIT"<<endl;
      cout<<"\n Enter Your Choice :"<<endl;
      cin>>number;
      switch(number)
      {
                    case 1:
                         {
                                 cout<<"\nADDITION"<<endl;
                                 cout<<"\nEnter 1st Number :"<<endl;
                                 cin>>num1;
                                 cout<<"Enter 2nd Number :"<<endl;
                                 cin>>num2;
                                 sum=num1+num2;
                                 cout<<"\nAnswer = "<<sum<<endl;
                                 break;
                                 }
                     case 2:
                         {
                                 cout<<"\nSUBSTRACTIN"<<endl;
                                 cout<<"\nEnter 1st Number :"<<endl;
                                 cin>>num1;
                                 cout<<"Enter 2nd Number :"<<endl;
                                 cin>>num2;
                                 sum=num1-num2;
                                 cout<<"\nAnswer = "<<sum<<endl;
                                 break;
                                 }
                     case 3:
                         {
                                 cout<<"\NMULTIPLICATION"<<endl;
                                 cout<<"\nEnter 1st Number :"<<endl;
                                 cin>>num1;
                                 cout<<"Enter 2nd Number :"<<endl;
                                 cin>>num2;
                                 sum=num1*num2;
                                 cout<<"\nAnswer = "<<sum<<endl;
                                 break;
                                 }
                     case 4:
                         {
                                 cout<<"\nDIVISION"<<endl;
                                 cout<<"\nEnter 1st Number :"<<endl;
                                 cin>>num1;
                                 cout<<"Enter 2nd Number :"<<endl;
                                 cin>>num2;
                                 sum=num1/num2;
                                 cout<<"\nAnswer = "<<sum<<endl;
                                 break;
                                 }
                      case 5:
                         {
                                 exit(0);
                                 }
                                 i++;
      }
      }
getch();
}                  

TicTacToe gme in c++


#include<iostream.h>
#include<conio.h>
using namespace std;
main()
{
char a[9]={'0','1','2','3','4','5','6','7','8'};
int n=1,m=0,num;
do
{
system("cls");
cout<<(char)4<<" Welcome into tic tac toe game "<<(char)4<<"\n"<<endl;      
cout<<"  "<<a[0]<<" | "<<a[1]<<" | "<<a[2]<<"\n";
cout<<"____________\n";
cout<<"  "<<a[3]<<" | "<<a[4]<<" | "<<a[5]<<"\n";
cout<<"____________\n";
cout<<"  "<<a[6]<<" | "<<a[7]<<" | "<<a[8]<<"\n";
if(n==m%2)
{
cout<<"\n2nd Player turn"<<endl;
cin>>num;
if(num==0||num==1||num==2||num==3||num==4||num==5||num==6||num==7||num==8)
{
 for(int k=0;k<9;k++)
 {
         if(num==k)
         {                                                                        
          a[k]='X';
         }
 }
}
}
if(m%2==0){
cout<<"\n1st Player turn"<<endl;
cin>>num;
if(num==0||num==1||num==2||num==3||num==4||num==5||num==6||num==7||num==8)
{
for(int k=0;k<9;k++)
 {
         if(num==k)
         {                                                                        
          a[k]='Y';
         }
 }
}
}
if((a[0]==a[1]&&a[1]==a[2]&&a[3]==a[0])||(a[0]==a[3]&&a[3]==a[6]&&a[0]==a[6])||(a[0]==a[4]&&a[4]==a[8]&&a[0]==a[8])||(a[1]==a[4]&&a[4]==a[7]&&a[1]==a[7])||(a[2]==a[5]&&a[5]==a[8]&&a[2]==a[8])||(a[2]==a[4]&&a[4]==a[6]&&a[2]==a[6])||(a[3]==a[4]&&a[4]==a[5]&&a[3]==a[5])||(a[6]==a[7]&&a[7]==a[8]&&a[6]==a[8]))
{
system("cls");                                                                                                                                                                                                                                                                                                                
cout<<"  "<<a[0]<<" | "<<a[1]<<" | "<<a[2]<<"\n";
cout<<"____________\n";
cout<<"  "<<a[3]<<" | "<<a[4]<<" | "<<a[5]<<"\n";
cout<<"____________\n";
cout<<"  "<<a[6]<<" | "<<a[7]<<" | "<<a[8]<<"\n";                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
cout<<"\nYOU Won!!\n"<<(char)2<<(char)2<<endl;
break;
}
else{cout<<"you lose\n"; }
m++;
}
while((a[0]!=a[1]&&a[1]!=a[2]&&a[3]!=a[0])||(a[0]!=a[3]&&a[3]!=a[6]&&a[0]!=a[6])||(a[0]!=a[4]&&a[4]!=a[8]&&a[0]!=a[8])||(a[1]!=a[4]&&a[4]!=a[7]&&a[1]!=a[7])||(a[2]!=a[5]&&a[5]!=a[8]&&a[2]!=a[8])||(a[2]!=a[4]&&a[4]!=a[6]&&a[2]!=a[6])||(a[3]!=a[4]&&a[4]!=a[5]&&a[3]!=a[5])||(a[6]!=a[7]&&a[7]!=a[8]&&a[6]!=a[8]));
cout<<"\nbye";
getch();
}