Monday, November 18, 2019

C++ BLACKJACK

#include <iostream>
#include <ctime>
#include <stdlib.h>
#include <time.h>
#include <string>
using namespace std;
int draw_card( int& hand , bool& has_aces , bool& has_elev , string& card_disp );
int final_score( int& hand , bool& has_elev );
int main()
{

srand(time(NULL));
int player_raw_hand = 0, dealer_raw_hand = 0;
int player_final_hand = 0, dealer_final_hand = 0;
bool player_has_aces = false, dealer_has_aces = false;
bool player_has_elev = false, dealer_has_elev = false;
int player_chips = 100, computer_chips = 100;
bool hit;
int card_id;
int suit;
int number;
//strings will be used for displaying hands
string player_string = "";
string dealer_string = "";

//MASTER GAME LOOP
while(player_chips > 0)
{
    player_raw_hand=0;
    player_final_hand=0;
 player_has_aces=false;
 player_has_elev=false;
    dealer_raw_hand=0;
    dealer_final_hand=0;
 dealer_has_aces=false;
 dealer_has_elev=false;

 player_string.clear();
 dealer_string.clear();

    
    draw_card(dealer_raw_hand,dealer_has_aces,dealer_has_elev,dealer_string);
    dealer_final_hand=final_score(dealer_raw_hand,dealer_has_elev);
   
    do
    {
        cout<<"chips: "<<player_chips<<endl;
        cout << "Player Hand: " << player_string << endl;
        cout << "Dealer Hand: " << dealer_string << endl;
        cout<<"player_hand: "<<player_final_hand<<endl;
        cout<<", Computer hand: "<<dealer_final_hand<<endl;
        
        cout<<"hit";
        cin>>hit;
        
        if(hit)
         cout << draw_card(player_raw_hand,player_has_aces,player_has_elev,player_string) << endl;
       
        player_final_hand=final_score(player_raw_hand,player_has_elev);
    }
    while(hit&&player_raw_hand <=21);
   
    draw_card(dealer_raw_hand,dealer_has_aces,dealer_has_elev,dealer_string);
    dealer_final_hand=final_score(dealer_raw_hand,dealer_has_elev);
   
    if (player_final_hand>21)
    {
        cout<<"BUST !"<<endl;
        cout << "Player Hand: " << player_string << endl;
        cout << "Dealer Hand: " << dealer_string << endl;
        cout<<"player_hand: "<<player_final_hand<<endl;
        cout<<", Computer hand: "<<dealer_final_hand<<endl;
        player_chips--;
    }
    else if(player_final_hand >= dealer_final_hand)
    {
        cout<<"You Win !"<<endl;
        cout << "Player Hand: " << player_string << endl;
        cout << "Dealer Hand: " << dealer_string << endl;
        cout<<"player_hand: "<<player_final_hand<<endl;
        cout<<", Computer hand: "<<dealer_final_hand<<endl;
        player_chips++;
    }
    else
    {
        cout<<"lose!"<<endl;
        cout << "Player Hand: " << player_string << endl;
        cout << "Dealer Hand: " << dealer_string << endl;
        cout<<"player_hand: "<<player_final_hand<<endl;
        cout<<", Computer hand: "<<dealer_final_hand<<endl;
        player_chips--;
    }
}  
return 0;
}
int draw_card( int& hand , bool& has_aces , bool& has_elev , string& card_disp )
{
    int card_id;
    int raw_val;
    int card_suit;
    card_id = rand()%52;
    raw_val = card_id%13;
    card_suit = card_id/13; //card suit is 0,1,2, or 3
   
    //spades=0,hearts=1,clubs=2,diamonds=3
   
    switch(raw_val)
    {
      case 0:
       card_disp+="king ";
       break;
      case 1:
       card_disp+="ace ";
       break;
      case 2:
       card_disp+="2 ";
       break;
      case 3:
       card_disp+="3 ";
       break;
      case 4:
       card_disp+="4 ";
       break;
      case 5:
       card_disp+="5 ";
       break;
      case 6:
       card_disp+="6 ";
       break;
      case 7:
       card_disp+="7 ";
       break;
      case 8:
       card_disp+="8 ";
       break;
      case 9:
       card_disp+="9 ";
       break;
      case 10:
       card_disp+="10 ";
       break;
      case 11:
       card_disp+="jack ";
       break;
      case 12:
       card_disp+="queen ";
       break;
     
      default:
       card_disp+="ERROR ";
       break;
   
 }
   
   
   
   
    switch(card_suit)
    {
     case 0:
      card_disp+=" of spades , ";
      break;
     case 1:
      card_disp+=" of hearts , ";
      break;
     case 2:
      card_disp+=" of clubs , ";
      break;
     case 3:
      card_disp+=" of diamonds , ";
      break;
     default:
      card_disp+= " of ERROR , ";
      break;
 }
   
   
   
    if( raw_val == 1 ) //if we draw an ace, then automatically we "have aces"
     has_aces = true;
    
    if(raw_val == 0 || raw_val > 9)
        hand+=10;
    else if(raw_val > 1 && raw_val < 10)
        hand+=raw_val;
    else
        hand++;
       
    if( has_aces && hand+10 <= 21 )
     has_elev = true;
    
    return raw_val;
}
int final_score( int& hand , bool& has_elev )
{
 if( has_elev )
  return hand+10;
 else
  return hand;
}

Thursday, October 24, 2019

C++ Write a code that will print only prime factors.

Code

#include <iostream>
using namespace std;
void prime_factor(int n);

int main()
{
int n;
cout<<"Enter a number then press Enter : ";cin>>n;

prime_factor(n);
return 0;

}

//adding a function to print prime factor
void prime_factor(int n)
{
int exponent,i; // this variable will count the number of times prime factors was running

for( i = 2; i<=n; i++)
// set i = 2 because prime numbers starts at 2
{


exponent = 0; // this variable will count the number of times prime factors was running.
while(n % i == 0)
{
n=n/i; // dividing the value of n and sending back to while loop if it factorable by i again.
exponent++;// adding one to exponent. 
// everytime i gonna transfer a value its gonna keep adding 1 to exponent for that transferred number. 
// if i =2 transferred and n is 20 . the prime factors for 20 //is 2, 2, 5. so for 2 exponent will be count for 2. when the for loop will //transfer for different number it will count for that number.  

if (exponent !=0)
// exponent cant be 0
{
cout<<i<<"^"<<exponent;// print prime factors and exponent. 
if(n!=1)
{
cout<<", ";
}
}

}

}




 OUTPUT - 



Monday, September 30, 2019

C++ code Diamond shape of space

Code
#include <iostream>
using namespace std;

int main()
{
   int i,j,r;
    cout << " Input number of rows : ";
    cin >> r;
   for(i=1;i<r;i++)
   {
    for(j=0;j<=r-i;j++)
     cout<<"*";
     for(j=2;j<=2*i-1;j++)
    cout<<" ";
       for(j=0;j<=r-i;j++)
     cout<<"*";
     cout<<endl;
   }
   for(i=r-1;i>=1;i--)
   {
     for(j=0;j<=r-i;j++)
     cout<<"*";
     for(j=2;j<=2*i-1;j++)
    cout<<" ";
       for(j=0;j<=r-i;j++)
     cout<<"*";
     cout<<endl;
   }
   cout<<endl;
   return 0;
}

Output:

Monday, September 23, 2019

C ++ write nested for loops to produce a triangular array of stars, in the form below.

C ++ write nested for loops to produce a triangular array of stars, in the form below.
**
****
*****
****
**





Code

#include <iostream>
using namespace std;
int main()
{
 int i, j, k;
 for(i=1; i<3; i++)
 {
  cout<<"* ";
 }
 cout<<endl;
 for(j=1; j<5; j++)
 {
  cout<<"* ";
 }
 cout<<endl;
 for(k=1; k<=5; k++)
 {
  cout<<"* ";
 }
 cout<<endl;
 for(j=1; j<5; j++)
 {
  cout<<"* ";
 }
 cout<<endl;

 for(i=1; i<3; i++)
 {
  cout<<"* ";
 }
 cout<<endl;
 return 0;

}

Code - 2

#include <iostream>
using namespace std;
int squaresum(int n) ;

int main()
{
int n;
 cout<<"Enter the number of row : ";cin>>n;cout<<endl;
     for(int i=0; i<=n; i++)
     {
      for(int j=1; j<=i; j++)
      {
       cout<<"*";
   }
   cout<<endl;
  }
  for(int i=n-1; i>1; i--)
  {
   for(int j=1; j<=i; j++)
      {
       cout<<"*";
   }
   cout<<endl;
  }
}

OUTPUT


Write C++ statement that compute the sum of squres of given positive integer

Code


#include <iostream>
using namespace std;
int squaresum(int n) ;


int main()
{
int n;
 cout<<"Enter a positive integer : ";cin>>n;
 cout << squaresum(n) << endl;
 return 0;
}

int squaresum(int n)
{

 int sum = 0;
 for (int i = 1; i <= n; i++)
  sum += (i * i);
 return sum;
}


OUTPUT


C++ code that will print only prime factor of given number

Code

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
 int n, i;
 cout<<"Please input an integer then press ENTER : ";cin>>n;

 while(n%2 == 0)
 {
 cout<<2<<" ";
 n = n/2;
 }
 for( i =3; i<=n; i=i+2)
 {
  while(n%i == 0)
  {
   cout<<i<<" ";
   n=n/i;
  }
 }
 if(n>2)
 {
  cout<<n<<" ";
 }

 return 0;
}


Monday, September 16, 2019

Write a program to print half pyramid / triangle using alphabets

Code

#include <iostream>
using namespace std;
int main()
{
 int i, j;
    char input, alphabet = 'A';
    cout << "Enter the uppercase character you want to print in the last row: ";
    cin >> input;
    for(i = 1; i <= (input-'A'+1); ++i)
    {
        for( j = 1; j <= i; ++j)
        {
            cout << alphabet << " ";
        }
        ++alphabet;
        cout << endl;
    }
    return 0;
}



Output - 



C++ BLACKJACK

#include <iostream> #include <ctime> #include <stdlib.h> #include <time.h> #include <string> using namesp...