Monday 1 October 2012

Some Good Question Of C Language



  COLORS (text mode)
 ====================|Back-|Fore-
  Constant     |Value|grnd?|grnd?
 --------------+-----+-----+-----
  BLACK        |  0  | Yes | Yes
  BLUE         |  1  | Yes | Yes
  GREEN        |  2  | Yes | Yes
  CYAN         |  3  | Yes | Yes
  RED          |  4  | Yes | Yes
  MAGENTA      |  5  | Yes | Yes
  BROWN        |  6  | Yes | Yes
  LIGHTGRAY    |  7  | Yes | Yes
  DARKGRAY     |  8  | No  | Yes
  LIGHTBLUE    |  9  | No  | Yes
  LIGHTGREEN   | 10  | No  | Yes
  LIGHTCYAN    | 11  | No  | Yes
  LIGHTRED     | 12  | No  | Yes
  LIGHTMAGENTA | 13  | No  | Yes
  YELLOW       | 14  | No  | Yes
  WHITE        | 15  | No  | Yes
-----

#include <conio.h>

 int main(void)
 {
    clrscr();
    cprintf("INSLINE inserts an empty line in the text window\r\n");
    cprintf("at the cursor position using the current text\r\n");
    cprintf("background color.  All lines below the empty one\r\n");
    cprintf("move down one line and the bottom line scrolls\r\n");
    cprintf("off the bottom of the window.\r\n");
    cprintf("\r\nPress any key to continue:");
    gotoxy(1, 3);
    getch();
    insline();
    getch();
    return 0;
 }


------

 #include <conio.h>

 int main(void)
 {
    cprintf("Press any key to continue:");
    while (!kbhit()) /* do nothing */ ;
    cprintf("\r\nA key was pressed...\r\n");
    return 0;
 }

--------

#include <conio.h>
 #include <string.h>

 int main(void)
 {
    char *str = "This is a test string";

    clrscr();
    cputs(str);
    getch();

    movetext(1, 1, strlen(str), 2, 10, 10);
    getch();

    return 0;
 }


-----
 #include <stdio.h>
 #include <conio.h>

 int main(void)
 {
    char ch = 0;

    printf("Input a string:");
    while ((ch != '\r'))
    {
       ch = getch();
       putch(ch);
    }
    return 0;
 }

----------
 #include <conio.h>

 int main(void)
 {
    /* Display the normal cursor */
    cprintf("\n\rNormal Cursor: "); getch();

    /* Turn off the cursor */
    _setcursortype(_NOCURSOR);
    cprintf("\n\rNo Cursor    : "); getch();

    /* Switch to a solid cursor */
    _setcursortype(_SOLIDCURSOR);
    cprintf("\n\rSolid Cursor : "); getch();

    /* Switch back to the normal cursor */
    _setcursortype(_NORMALCURSOR);
    cprintf("\n\rNormal Cursor: "); getch();

    return 0;
 }
------------
 #include <conio.h>

 int main(void)
 {
    int i;

    clrscr();
    for (i=0; i<9; i++)
    {
        textattr(i + ((i+1) << 4));
        cprintf("This is a test\r\n");
    }

    return 0;
 }
---------------
 #include <conio.h>

 int main(void)
 {
    textmode(BW40);
    cprintf("ABC");
    getch();

    textmode(C40);
    cprintf("ABC");
    getch();

    textmode(BW80);
    cprintf("ABC");
    getch();

    textmode(C80);
    cprintf("ABC");
    getch();

    textmode(MONO);
    cprintf("ABC");
    getch();

    return 0;
 }

-------------

 #include <conio.h>

 int main(void)
 {
    clrscr();
    gotoxy(10,10);
    cprintf("Current location is X: %d  Y: %d\r\n", wherex(), wherey());
    getch();

    return 0;
 }

----------The bitwise operators
Operator
Name
Description
a&b
and
1 if both bits are 1. 3 & 5 is 1.
a|b
or
1 if either bit is 1. 3 | 5 is 7.
a^b
xor
1 if both bits are different. 3 ^ 5 is 6.
~a
not
This unary operator inverts the bits. If ints are stored as 32-bit integers, ~3 is 11111111111111111111111111111100.
n<<p
left shift
shifts the bits of n left p positions. Zero bits are shifted into the low-order positions. 3 << 2 is 12.
n>>p
right shift
shifts the bits of n right p positions. If n is a 2's complement signed number, the sign bit is shifted into the high-order positions. 5 >> 2 is 1.
----------

------------- Add two number without using arthi. op

#include <stdio.h>
      int add(int a, int b){
            if (!a) return b;
            else
                 return add((a & b) << 1, a ^ b);
      }

      int main(){
      int a,b;
          printf("Enter the two numbers: \n");

            scanf("%d",&a);
            scanf("%d",&b);
            printf("Sum is: %d",add(a,b));
      }

--------------------
#include <iostream.h>
#include <conio.h>

int main()
{
cout << "Bit-operators example\nBy Sean Patrick Kane\nhttp://celtickane.com\n\n";

//AND operator
cout << "****AND(&)****\n";
cout << "*..A..B..A&B.*\n";
cout << "* 1 0 " << (1 & 0) << " *\n";
cout << "* 0 1 " << (0 & 1) << " *\n";
cout << "* 0 0 " << (0 & 0) << " *\n";
cout << "* 1 1 " << (1 & 1) << " *\n";
cout << "**************\n";
cout << "Hit any key to continue...\n";

getch();

//OR operator
cout << "\n****OR (|)****\n";
cout << "*..A..B..A|B.*\n";
cout << "* 1 0 " << (1 | 0) << " *\n";
cout << "* 0 1 " << (0 | 1) << " *\n";
cout << "* 0 0 " << (0 | 0) << " *\n";
cout << "* 1 1 " << (1 | 1) << " *\n";
cout << "**************\n";
cout << "Hit any key to continue...\n";

getch();

//XOR operator
cout << "\n****XOR(^)****\n";
cout << "*..A..B..A^B.*\n";
cout << "* 1 0 " << (1 ^ 0) << " *\n";
cout << "* 0 1 " << (0 ^ 1) << " *\n";
cout << "* 0 0 " << (0 ^ 0) << " *\n";
cout << "* 1 1 " << (1 ^ 1) << " *\n";
cout << "**************\n";

cout << "Hit any key to exit...\n";

getch();
return 0;
}

No comments:

Post a Comment