Sunday, 6 August 2017

WALK-IN-INTERVIEW




#1: Which is valid C expression?
a) int my_num = 100,000;
b) int my_num = 100000;
c) int my num = 1000;
d) int $my_num = 10000;
Answer: b
Explanation: space, comma and $ cannot be used in a variable name.
#2: What is the output of this C code?
#include
int main()
{
    printf("Hello World! %d \n", x);
    return 0;
}
a) Hello World! x;
b) Hello World! followed by a junk value
c) Compile time error
d) Hello World!
 Answer: c
Explanation: It results in an error since x is used without declaring the variable x.
#3: What will happen if the below program is executed?
#include
int main()
{
    int main = 3;
    printf("%d", main);
    return 0;
}
a) It will cause a compile-time error
b) It will cause a run-time error
c) It will run without any error and prints 3
d) It will experience infinite looping
Answer: c
Explanation: A C program can have same function name and same variable name.
#4: What is the output of this C code?
#include 
int main()
{
   char chr;
   chr = 128;
   printf("%d\n", chr);
   return 0;
}
a) 128
b) -128
c) Depends on the compiler
d) None of the mention

Answer: b
Explanation: signed char will be a negative number.
#5: What is the output of this C code?
#include
int main()
{
    char *p[1] = {"hello"};
    printf("%s", (p)[0]);
    return 0;
}
a) Compile time error
b) Undefined behaviour
c) hello
d) None of the mentioned
Answer: c
Explanation: None
#6: What is the output of this C code?
#include
int main()
{
    printf("crazyfor\code\n");
    return 0;
}
a) crazyforcode
b) crazyforcode
c) codeyfor
d) crazyfor
Answer: c
Explanation: r is carriage return and moves the cursor back. sanfo is replaced by class


Friday, 4 August 2017

CHALLENGE THIS!!!!!!!





Given three corner points of a triangle, and one more point P. Write a function to check whether P lies within the triangle or not.

   B(10,30)
           / \
          /   \
         /     \
        /   P   \      P'
       /         \
A(0,0) ----------- C(20,0)
The program needs to read the values of three coordinates

A(x1,y1)
B(x2,y2)
C(x3,y3)
as well as another coordinate P(x,y) and determine whether this point is inside a triangle formed from the 3 point above.

Solution:
Let the coordinates of three corners be (x1, y1), (x2, y2) and (x3, y3). And coordinates of the given point P be (x, y)

1) Calculate area of the given triangle, i.e., area of the triangle ABC in the above diagram. Area A = [ x1(y2 – y3) + x2(y3 – y1) + x3(y1-y2)]/2
2) Calculate area of the triangle PAB. We can use the same formula for this. Let this area be A1.
3) Calculate area of the triangle PBC. Let this area be A2.
4) Calculate area of the triangle PAC. Let this area be A3.
5) If P lies inside the triangle, then A1 + A2 + A3 must be equal to A.

Implementation

float area(int x1, int y1, int x2, int y2, int x3, int y3)
{
   return abs((x1*(y2-y3) + x2*(y3-y1)+ x3*(y1-y2))/2.0);
}
 
/* A function to check whether point P(x, y) lies inside the triangle  */
bool isInside(int x1, int y1, int x2, int y2, int x3, int y3, int x, int y)
   /* Calculate area of triangle ABC */
   float A = area (x1, y1, x2, y2, x3, y3);
 
   /* Calculate area of triangle PBC */
   float A1 = area (x, y, x2, y2, x3, y3);
 
   /* Calculate area of triangle PAC */
   float A2 = area (x1, y1, x, y, x3, y3);
 
   /* Calculate area of triangle PAB */ 
   float A3 = area (x1, y1, x2, y2, x, y);
   
   /* Check if sum of A1, A2 and A3 is same as A */
   return (A == A1 + A2 +A3);

}

Saturday, 15 July 2017

PRINT ASCII TABLE/ASCII CHART

#include
int main()
{
unsigned char count;
for(count=32;count<255 count="" p="">{
printf(" %3d - %c",count,count);
if(count % 6==0)
printf("\n");
}
return 0;

}
IT REALLY  WORKS....

Tuesday, 11 July 2017

INTERESTING FACT ON C


Below is one of the interesting facts about C programming:

1) The case labels of a switch statement can occur inside if-else statement

#include

int main()
{
    int a = 2, b = 2;
    switch(a)
    {
    case 1:
        ;

        if (b==5)
        {
        case 2:
            printf("GeeksforGeeks");
        }
    else case 3:
    {

    }
    }
}
Run on IDE
Output :GeeksforGeeks

Monday, 10 July 2017

FACTORIAL


#include
int main()
{
    int n, i;
    unsigned long long factorial = 1;

    printf("Enter an integer: ");
    scanf("%d",&n);

    // show error if the user enters a negative integer
    if (n < 0)
        printf("Error! Factorial of a negative number doesn't exist.");

    else
    {
        for(i=1; i<=n; ++i)
        {
            factorial *= i;              // factorial = factorial*i;
        }
        printf("Factorial of %d = %llu", n, factorial);
    }

    return 0;
}

Tuesday, 4 July 2017



               
1.In a file contains the line "I am a boy\r\n" then on reading this line into the array str using fgets(). What will str contain?
A.            "I am a boy\r\n\0"
B.            "I am a boy\r\0"
C.            "I am a boy\n\0"
D.            "I am a boy"
TRY  IT YOURSELF


Answer: Option C
Explanation:
Declaration: char *fgets(char *s, int n, FILE *stream);
fgets reads characters from stream into the string s. It stops when it reads either n - 1 characters or a newline character, whichever comes first.

Therefore the  string str  contain “ I am a boy\n\0”.

Monday, 3 July 2017

MAGIC SQUARE PUZZLE


/*
 * C Program to Solve the Magic Squares Puzzle without using 
 * Recursion
 */
#include

void magicsq(int, int [][10]);

int main( )
{
    int size;
    int a[10][10];

    printf("Enter the size: ");
    scanf("%d", &size);
    if (size % 2 == 0)
    {
        printf("Magic square works for an odd numbered size\n");
 A   }
    else
    {
        magicsq(size, a);
    }
    return 0;
}

void magicsq(int size, int a[][10])
{
    int sqr = size * size;
    int i = 0, j = size / 2, k;

    for (k = 1; k <= sqr; ++k) 
    {
        a[i][j] = k;
        i--;
        j++;

        if (k % size == 0) 
        { 
            i += 2; 
            --j; 
        }
        else 
        {
            if (j == size) 
            {
                j -= size;
            }
            else if (i < 0)
            {
                i += size;
            }
        }
    }
    for (i = 0; i < size; i++)
    {
        for (j = 0; j < size; j++)
        {
            printf("%d  ", a[i][j]);
        }
        printf("\n");
    }
}
RUN IT..............