Friday, November 28, 2014

C using bitwise pro



C Program to find if a given Year is a Leap Year

#include
#include
void main()
{                  
    int year;
    clrscr();
    printf("Enter a year \n");
    scanf("%d", &year);
    if ((year % 400) == 0)
        printf("%d is a leap year \n", year);
    else if ((year % 100) == 0)
        printf("%d is a not leap year \n", year);
    else if ((year % 4) == 0)
        printf("%d is a leap year \n", year);
    else
        printf("%d is not a leap year \n", year);
    getch();
}
Output
 
Enter a year
2012
2012 is a leap year

C Program to Swap the Contents of two Numbers using Bitwise XOR Operation

#include 
    #include
void main()
{
long i, k;
clrscr();
printf("Enter two integers \n");
scanf("%ld %ld", &i, &k);
printf("\n Before swapping i= %ld and k = %ld", i, k);
i = i ^ k;
k = i ^ k;
i = i ^ k;
printf("\n After swapping i= %ld and k = %ld", i, k);
getch();
}
 
Output

Enter two integers
45
89
Before swapping i= 45 and k = 89
After swapping i= 89 and k = 45

C Program to Convert a Given Number of Days in terms of Years, Weeks & Days

#include 
#include
#define Daysinweek 7
void main()
{
    int ndays, year, week, days;
    printf("Enter the number of daysn");
    scanf("%d", &ndays);
    year = ndays / 365;
    week =(ndays % 365) / Daysinweek;
    days =(ndays % 365) % Daysinweek;
    printf ("%d is equivalent to %d years, %d weeks and %d daysn",
            ndays, year, week, days);
    getch();
}
 
Output

Enter the number of days
29
29 is equivalent to 0 years, 4 weeks and 1 days

C Program to Display the Inventory of Items in a Store

#include 
#include
void main()
{
    struct date
    {
        int day;
        int month;
        int year;
    };
    struct details
    {
        char name[20];
        int price;
        int code;
        int qty;
        struct date mfg;
    };
    struct details item[50];
    int n, i;
    printf("Enter number of items:");
    scanf("%d", &n);
    fflush(stdin);
    for (i = 0; i < n; i++)
    {
        fflush(stdin);
        printf("Item name: \n");
        scanf("%s", item[i].name);
        fflush(stdin);
        printf("Item code: \n");
        scanf("%d", &item[i].code);
        fflush(stdin);
        printf("Quantity: \n");
        scanf("%d", &item[i].qty);
        fflush(stdin);
        printf("price: \n");
        scanf("%d",  &item[i].price);
        fflush(stdin);
        printf("Manufacturing date(dd-mm-yyyy): \n");
        scanf("%d-%d-%d", &item[i].mfg.day,
        &item[i].mfg.month, &item[i].mfg.year);
    }
    printf("             *****  Inventory ***** \n");
    printf("---------------------------------------------------------
    ---------\n");
    printf("S.n.|    Name           |   Code   |  Quantity |  Price
    | Mfg.Date \n");
    printf("---------------------------------------------------------
    ---------\n");
    for (i = 0; i < n; i++)
        printf("%d     %-15s        %-d          %-5d     %-5d
        %d/%d/%d \n", i + 1, item[i].name, item[i].code, item[i].qty,
        item[i].price, item[i].mfg.day, item[i].mfg.month,
        item[i].mfg.year);
    printf("---------------------------------------------------------
    ---------\n");
    getch();
}
Output

Enter number of items:3
Item name:
pendrive
Item code:
123
Quantity:
6
price:
3000
Manufacturing date(dd-mm-yyyy):
30-9-2012
Item name:
computer
Item code:
124
Quantity:
10
price:
10000
Manufacturing date(dd-mm-yyyy):
30-7-2012
Item name:
optical mouse
Item code:
Quantity:
price:
Manufacturing date(dd-mm-yyyy):
             *****  Inventory *****
------------------------------------------------------------------
S.n.|    Name           |   Code   |  Quantity |  Price  | Mfg.Date
------------------------------------------------------------------
1     pendrive               123          6        3000     30/9/2012
2     computer               124          10       10000    30/7/2012
3     optical                0            0        0        0/0/0
------------------------------------------------------------------

No comments:

Post a Comment