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;
}