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..............

Thursday, 29 June 2017

ALGEBRA MAGIC

#include
#include

int main()

 int x,y;
 printf("Think of a munber Between 60 and 100 .\nProvided The number should not consist of the values 0&1.\nPress Enter Key When Ready");
 getch();
 printf("\nNow add the number with its reverse.\neg.62+26\nif the number you thought was 62.\npress Enter when ready.");
 getch();
 printf("\nEnter the Middle value of your answer = ");
 scanf("%d",&x);
 y=100+(x*10)+(x-1);
 printf("The value you got is = %d",y);
 getch();
}

Run this and see the magic !!!!!!!

Tuesday, 27 June 2017

More on printf

What will be the output of following code 

#include

int main()
{

         printf("%d",printf("computer"));

         return (0);

}

Tuesday, 9 August 2016

Automorphic Numbers

Automorphic Numbers
In mathematics an automorphic number (sometimes referred to as a circular number) is a number whose square "ends" in the same digits as the number itself. For example, 52 = 25, 62 = 36, 762 = 5776, and 8906252 = 793212890625, so 5, 6, 76 and 890625 are all automorphic numbers. The only automorphic Kaprekar number is 1, because the square of a Kaprekar number cannot start with zero.
The sequence of automorphic numbers begins 1562576376625, 9376, ... (sequence A003226 in the OEIS).