Monday, July 8, 2013

Encrypt a 4 digit number by adding 7 to each digit and swapping first with third & 2nd with 4rth digit & Decrypt to original again

Encrypt a 4 digit number in such a way that ,later on Decrypt the number to original number :

1)Your program should read a four-digit integer in such a way that it replaces each digit by(sum of that digit + 7)
2)Swap 2nd digit with 4rth
3)Swap 1st digit with 3rd
4)Print the encrypted number

#include <stdio.h>
int main(void)
{
    int num,dig,mod,total = 0,div = 1000,base = 1000,dig1,dig2,dig3,dig4;
    printf("Enter a 4 digit +ve number to be Encrypted : ");
    scanf("%d",&num);
 
    if ( num > 999 && num <= 9999)
    {
        while ( num != 0)
        {
            dig = num / div + 7;
            total = total * 10 + dig ;
            num = num % base;
            div = div / 10;
            base = base / 10;
         
        }
        printf("Total encrypted number is %d",total);
     
            dig1 = (total/1000);
            dig2 = (total % 1000 / 100);
            dig3 = (total % 100 / 10);
            dig4 = (total % 10);
            printf("\nNew Encrypted number is %d%d%d%d",dig3,dig4,dig1,dig2);
    }
    else
        printf("Please Enter a 4 digit +ve number : ");
 
 
    return 0;
}


Part 2 : Decryption to original number :

#include <stdio.h>
int main(void)
{
    int num,dig1,dig2,dig3,dig4,base = 1000,div = 1000,mod,total = 0;
    printf("\nEnter an Encrypted four digit number : ");
    scanf("%d",&num);


    if ( num > 999 && num <= 9999)
    {
        
        dig1 = num / base ;
        dig2 = (num % base)/100 ;
        dig3 = (num % 100)/10 ;
        dig4 = (num % 10);


        num = dig3*1000+dig4*100+dig1*10+dig2;


        while( num != 0)
        {
            mod = num / base - 7;
            total = total + mod * base;
            base = base / 10;
            num = num % div;
            div = div / 10;
        }
        printf("\nDecrypted number is %d",total);
    }
    else
        printf("\nPlease enter a +ve 4 digit number & try again ");
    return 0;
    

}




Thursday, July 4, 2013

Convert a Decimal number into Binary number

#include <stdio.h>
int main(void)
{
    int number,mod,base = 1,total = 0 ;


    printf("\nEnter a decimal number and I'll let you know Binary of it : ");
    scanf("%d",&number);


    while ( number != 0)
    {
       mod = number % 2 ;
       total = total + mod * base ;
       number = number/2;
       base = base * 10;
    }
 
    printf("\nBinary number of decimal %d is %d",number,total);


    return 0;
 
}

Wednesday, July 3, 2013

Convert a Binary number to Decimal number without using Pow() or Arrays or Functions



#include <stdio.h>
int main(void)
{
    long int bin;
    int total = 0 ,dec,mod,base =1 ;


    printf("\nEnter a binary number(0's & 1's),I'll tell you its decimal : ");
    scanf("%ld",&bin);


    while ( bin != 0  )
    {
        mod = bin % 10;
        total = total + base * mod;
        base = base * 2 ;
        bin =  bin / 10;
    }


    printf("\nDecimal of %ld is %d",bin,total);
    return 0;
}

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;

}