Saturday, July 12, 2014

c assignment 2

↓↑ Print range of various data types using constants declared in limits.h .
#include<stdio.h>
#include<limits.h>
int main()
{
    printf("range of char is %ld to %ld\n ",SCHAR_MIN,SCHAR_MAX);
    printf("range of int is %ld to %ld\n ",INT_MIN,INT_MAX);
    printf("range of longint is %ld to %ld\n ",LONG_MIN,LONG_MAX);
    printf("range of shortint is %d to %d\n ",SHRT_MIN,SHRT_MAX);

return 0;
}
↓↑ prgram to display int float using various format specifiers.
#include<stdio.h>
void showbit(int n);
int main()
{
    int i=65;
    float j=1.25f;
    printf("Intiger to decimal=%d ,octal=%o hexadecimal=%x ,scientific=%e character=%c and in binary= ",i,i,i,i,i,i);
    showbit(i);
    printf("\n");
    printf("float in scientific=%e\n",j); 
return 0;
}
void showbit(int n)
{
    int i,k,shift;
    for(i=15;i>=0;i-- )
    {
        shift=1<<i;
        k=n&shift;
        k==0?printf("0"):printf("1");
    }
}
↓↑ Programm to convert ip address to 32 bit unsigned integer.
#include<stdio.h>
#include<math.h>
int main()
{
    int a,b,c,d;
    unsigned int ip;
    printf("\nEnter IP Address...\n");
    scanf(" %d%d%d%d",&a,&b,&c,&d);
    if(a>255&&a<0 && b>255&&b<0 && c>255&&c<0 && d>255&&d<0)
    {
        printf("\nInvalid Ip...\n");
    }
    ip=a<<24|b<<16|c<<8|d;
    printf("\n\n%u",ip);
return 0;
}
↓↑ Program to converts a 32 bit unsigned integer to ip address.
#include<stdio.h>
#include<math.h>
int main()
{
    unsigned int num;
    int a,b,c,d;
    printf("\nEnter 32 bit unsigned number");
    scanf("%d",&num);
    a=num>>24; 
    b=(num<<8)>>24;
    c=(num<<16)>>24;
    d=(num<<24)>>24;
    printf("\n%d.%d.%d.%d",a,b,c,d);
return 0;
}
↓↑ Program to convert binary to decimal.
#include<stdio.h>
#include<math.h>
int main()
{
    int n,a[8],b[8],i,j;
    printf("enter binary no:");
    for(i=0;i<=7;i++)
    {
        scanf("%d",&a[i]);
    } 
    for(i=7,j=0;i>=0,j<=7;i--,j++)
    { 
        n+=a[j]*pow(2,i); 
    }
    printf("no in decimal is:%d\n",n);
return 0;
}
↓↑ Program to Evaluate various expressions with bitwise operatiors.
#include<stdio.h>
int main()
{
    int a=22,b=4;
    printf("result of bitwise or  is:%d\n",a|b);
    printf("result of bitwise and is:%d\n",a&b);
    printf("result of bitwise xor is:%d\n",a^b);
    printf("result of bitwise left shift is:%d\n",a<<b);
    printf("result of bitwise right shift is:%d\n",a>>b);
    printf("result of bitwise not is:%d\n",~a);
return 0;
}
output:
result of bitwise or  is:22
result of bitwise and is:4
result of bitwise xor is:18
result of bitwise left shift is:352
result of bitwise right shift is:1
result of bitwise not is:-23
↓↑ Program to Try various format specifiers in printf, scanf .
#include<stdio.h>
int main()
{
   int i=10;
   float j=20.56333;
   printf("%5d,%-5d,%05d,%8.2f,%-8.2f,%08.2f,%.2f,%.1f\n",i,i,i,j,j,j,j,j);
return 0;
}
output:
   10,10   ,00010,   20.56,20.56   ,00020.56,20.56,20.6
↓↑ expression using conditional operator to find biggest of 3 no.s.
biggest = a > b ? (a > c ? a : c) : (b > c ? b : c) ;
↓↑ Program In a given bit pattern(8,16 or 32 bit) write expressions for
a. set Kth bit
b. Reset Kth bit
c. Flip Kth bit
d. Query Kth bit1.
#include<stdio.h>
int main()
{
    int n,i=0,j=0,bin[32],b[32],count=0,choice,bit;
    printf("enter number:");
    scanf("%d",&n);
    while(n>=1)
    {
        bin[i]=n%2;
        n=n/2;
        i++;
        count++;
    }
    while(i>=0)
    {
        b[j]=bin[i-1];
        i--;
        j++;
    }
    printf("\nbinary no is:");
    for(i=0;i<count;i++ )
    {
        printf("%d ",b[i]);
    }
    printf("\nthere are : %d :bit in binary number\n",count);
    printf("**********************************************************\n");
    printf("Press 1,2,3,4,5 .............\n");
    printf("1.set Kth bit\n");
    printf("2.Reset Kth bit\n");
    printf("3.Flip Kth bit\n");
    printf("4.Query Kth bit\n");
    printf("5.Exit\n");
    printf("**********************************************************\n");
    printf("enter your choice:");
    scanf("%d",&choice);



    switch(choice)
    {
        case 1:
            printf("\nEnter which bit is to be set:");
            scanf("%d",&bit);
            if(b[count-bit]==1);
            else
                b[count-bit]=1;

            printf("\nBinary no after setting the bit:");
            for(i=0;i<count;i++ )
            {
                printf("%d ",b[i]);
            }
            printf("\n");
        break;
        case 2:
            printf("Enter which bit is to be Reset:\n");
            scanf("%d",&bit);
            (b[count-bit]==1)?(b[count-bit]=0):(b[count-bit]=0);

             printf("\nBinary no after setting the bit:");
             for(i=0;i<count;i++ )
             {
                 printf("%d ",b[i]);
             }
             printf("\n");

        break;
        case 3:
            printf("which bit is to be flip:\n");
            scanf("%d",&bit);
            if(b[count-bit]==0)
                b[count-bit]=1;
            else
                b[count-bit]=0;
            printf("\nBinary no after Flipping the bit:");
            for(i=0;i<count;i++ )
            {
                printf("%d ",b[i]);
            }
            printf("\n");
        break;
        case 4:
            printf("which bit is to be searched:\n");
            scanf("%d",&bit);
            printf(" %d bit is :%d\n",bit,b[count-bit]);
        break;
        case 5:


        break;
        default:
            printf("\ninvalid choice\n");
    }
return 0;
}

No comments:

Post a Comment