Thursday, August 2, 2012

21 Matchstick Game in C


21 Matchstick Game using C Programming

Write a program for a matchstick game being played between the computer and a user.
Your program should ensure that the computer always wins. Rules for the game are as follows:

-There are 21 matchsticks.
-The computer asks the player to pick 1, 2, 3 or 4 matchsticks.
-After the person picks, the computer does its picking.
-Whoever is forced to pick up the last matchstick loses the game.

Code :
=======

#include <stdio.h>


int main(void)
{
    int i,total = 0,choice,matches = 21,left = 0,k = 0;
    while (  total < 20 )
        
    {
        
        printf("\nEnter your choice of match-sticks between 1-4 :");
        scanf("%d",&i);
        if ( i > 4 || i <= 0 )
            {
             printf("Pl.Enter a valid no between 1,2,3 or 4 :");
             continue;
            }
        total = total + i ;
        left = left - total ;
        
        if( total >= 21)
            break;
            
        choice = 5 - i ;
        printf("\nComputer chooses %d",choice);
        total = total+choice ;
        left = left - choice ;
        printf("\n \nNow total match sticks are %d",total);
        
                
  if (total == 20)
   {
       
      while (k != 1)
      {
          printf("\nEnter your choice of M-sticks you have just one left :");
          scanf("%d",&k);
          if ( k != 1)
              printf("\nPl.enter valid choice : 1 ");
          continue;
      }
          
       
       if(k == 1)
       {
           total = total + k;
           printf("\nComputer wins as you are last to choose match-stick");
           printf("\n \nNow total match sticks are %d",total);
       }
       
  
  
   }   
 
   
}
     return 0;
}
    
   

Friday, September 9, 2011

Find Binary Number Of Any Given Decimal Number


/*Find Binary Number Of Any Given Decimal Number (In Reverse Order) */


#include <stdio.h>
int main(void)
{
    int num,j,k = 0;
    printf("Enter a number whose binary you want to know :");
    scanf("%d",&num);
    j = num ;

    while ( j > 0)
    {
        k = j % 2;
        printf("%d ",k );
        j = j / 2 ;
        continue;
    }

    printf("\n");
    return 0;

}

Euclid's Geometric Position

/*According to Euclid's Geometric Position find Integer Factorization/Prime Factorization of Given Number */


#include <stdio.h>

int main(void)
{
    int i,num,j;
    printf("Enter a number whose Prime factors you want to find :");
    scanf("%d",&num);
    j = num ;    //Assigning value of num to j

    for ( i = 2 ; i <= j-1 ;i++)
    {
         while( num % i == 0)
         {
             printf("\n%d ",i );
             num = num / i ;
             continue;
         }
       
    }
 
   printf("\n");
   return 0;

 }


Monday, September 5, 2011

According to the Gregorian calendar,it was Monday on the date 01/01/1900.If any year is input through the keyboard write a program to find out what is the day on 1st January of this year



Solution code:-
=========


Easy One (Irrespective of difference between year input from year 1901) : 

/*According to the Gregorian calender, it was Monday on the date 01/01/01. If any year is input
 * through the keyboard write a program to find out what is the day on 1st January of this year?*/



#include <stdio.h>
int main(void)
{
    int yr,diff,lpyrdays,normaldays,res;
    printf("\nEnter a year whose day of 1st Jan you want to know : ");
    scanf("%d",&yr);
   
    yr = (yr - 1) ; //removing 1 year as only 1 day we are calculating of current yr
    lpyrdays =  (yr/4)  + (yr / 400) - (yr / 100 ); //Calculating leap days in that particular year
    normaldays = (yr* 365 )+ 1 + lpyrdays ; //Calculating normal days in that year & adding 1st jan's 1 day in
    res = normaldays % 7;
   
   
    if(res==0)
    printf("\nSunday");
    if(res==1)
    printf("Monday");
    if(res==2)
    printf("Tuesday");
    if(res==3)
    printf("Wednesday");
    if(res==4)
    printf("Thursday");
    if(res==5)
    printf("Friday");
    if(res==6)
    printf("Saturday");
   
    return 0;
   
   
   
   
}

Tough One (Solution Code):


#include <stdio.h>

int main(void)          //main function.. starting of c code
{
    int year,differ,lp_year,day_type;
    long int days;
  
   
    printf("Please enter the year: ");
    scanf("%d",&year);
    year=year-1; //we will find days before given year so
    differ=year-1900;


   /*as leap year is not divisible by 100.so,create 2 condition
   one difference less than 100 and greater than 100*/


   if(differ<100)
   {
   lp_year=differ/4; //caln of total no. of leap year
   days=(366*lp_year)+((differ-lp_year)*365+365+1);//see Note1
   day_type=days%7; //caln of day type sun, mon......
   }
  
   if(differ>=100)
   {
   lp_year=(differ/4)-(differ/100)+1+((year-2000)/400);//see Note2           
   days=(366*lp_year)+((differ-lp_year)*365+365+1);//see Note3
   day_type=days%7;
   }


if(day_type==0)
printf("\nSunday");
if(day_type==1)
printf("Monday");
if(day_type==2)
printf("Tuesday");
if(day_type==3)
printf("Wednesday");
if(day_type==4)
printf("Thursday");
if(day_type==5)
printf("Friday");
if(day_type==6)
printf("Saturday");
    
    
  
   
    return 0;  //int main() is function so value must be return.
               //u will read in function chapter
}



/*Note1:
-leap year has 366 day so lp_year*366
-remaining year has 365 day so (differ-lp_year)*365
-add 365 because we reduce 1 year
-add 1 to make jan 1 on which we find day type
     
Note2:
-(leap year come in every 4 year so) (differ/4) for leap year
-(leap year isn't divisible by 100 so we subtract (differ/100)
  from counting as leap year
-(leap year will be if divisible by 400 so ((year-2000)/400)
  to count that year as leap year
- we calculate from 2000 so we add 1

Note3:
-leap year has 366 day so lp_year*366
-remaining year has 365 day so (differ-lp_year)*365
-add 365 because we reduce 1 year
-add 1 to make jan 1 on which we find day type */

Wednesday, August 24, 2011

Validate if given number is Palindrome or not

#include <stdio.h>
int main(void)
{
int i,num,rev= 0,j = 0;
printf("Enter a number for Palindrome check :");
scanf("%d",&i);
num = i;

while ( i != 0 )
{
rev = i % 10;
j = rev + j * 10 ;
i = i / 10 ;
}

if ( j == num)
printf("\n %d is palindrome",num);
else
printf("\n %d is not Palindrome\n",num);
return 0;

}


Sunday, May 8, 2011

Find Prime Factorization of given number

/*Find Prime Factorization Of Any Given Number */

#include <stdio.h>
int main(void)
{
int i,num,j,k;
printf("Enter an integer whose Prime factors you want :");
scanf("%d",&num);
k = num ;
for(i = 2 ; i <= num ; i++)
{
for (j = 2 ; j <= i - 1 ; j++)
{
if ( i % j == 0)
break;
}
if (i == j)
{
while ( k % i == 0)
{
printf("%d ",i);
k = k / i ;
continue;

}


}

}
return 0;
}

Sunday, March 21, 2010

Bifurcate entered sentence into uppercase,lowercase,number,special characters & count at the end

/*End user enters sentence/s and you need to bifurcate each entered character whether its number,uppercase character,lower case character ,special character,whitespace character,tab/new line and then reprint the same sentence/s on new line*/

#include <stdio.h>
int main(void)
{
int i;
int big = 0,num = 0,sp = 0,small = 0,space = 0;
while ( (i = getchar()) != EOF )
{
if (( i >= 'a') && ( i <= 'z'))
small++;
else if (( i >= 'A') && ( i <= 'Z'))
big++;
else if (( i >= '0') && (i <= '9'))
num++;
else if ((( i >= 0) && (i <= 31)) || (( i >= 33) && ( i <= 47)) ||((i >= 58) && (i <= 64)) || (( i >= 91) && ( i <= 96)) || (( i >= 123) && ( i <= 127))) //special characters
sp++;
else if (( i == 32) || ( i == 9) || ( i = 10) || ( i = 13)) // for space,tab,newline
space++;
putchar(i);
}
printf("%d lower characters are entered \n",small);
printf("%d Uppercase characters are entered \n",big);
printf("%d White space characters are entered \n",space);
printf("%d Numerical characters are entered \n",num);
printf("%d Special characters are entered \n",sp);
return 0;
}