Tuesday, July 2, 2013

Enter a 5 digit number to see if its Palindrome number :- For Eg : 12321,45554, 11611

#include <stdio.h>
int main(void)
{
    int i,mod,total,temp;
    printf("\nEnter a +ve 5 digit number : ");
    scanf("%d",&i);
    temp = i;
   
    if ( i > 9999 && i <= 99999 )
    {
    while ( i != 0)  
    {
        mod = i % 10;
        total = total * 10 + mod ;
        i =  i / 10;
    }  
   
    if (total == temp)
        printf("%i is Palindrome ",temp);
    else
        printf("%i is not Palindrome",temp);
    }
    else
        printf("Pl. Enter a +ve 5 digit number :");
       
 return 0;
}

Sunday, June 30, 2013

Print an asterisks( * ) Square out of side size's number input

Write a Program that reads in the side of a square and then prints that square out of asterisks.Your program should work for squares of all sides sizes between 1-20.For example it should look like this :

*****
*      *
*      *
*      *
*****

Code :

#include <stdio.h>
int main(void)
{
    int i = 1,sides,j = 1,m = 1,b = 1,n;
    printf("Enter number of sides of square to be printed :");
    scanf("%d",&sides);
    while ( m <= sides)
    {
        printf("*");
        m++;
    }
 
    while ( b < sides - 1)
        {
            printf("\n*");
            n = 2;
            while ( n < sides )
            {
                printf(" ");
                n++ ;
            }
             if ( n == sides)
                 printf("*");
            b++;
        }
    printf("\n") ;
    while ( i <= sides)
    {
        printf("*");
        i++;
    }  
    return 0;
}

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;

}