C as Challenge


Q.1: Print 2 to 10 number without using semicolon. (In GCC it will print form 1 to 10 and Turbo C it print 2 to 10 )

Every statement in C must end with a semicolon as per basics. However there are few scenarios when we can write & run a program without semicolon.

Ans.  
#include <stdio.h>
#define N 10
int main(int num, char *argv[])
{

    while (num <= N && printf(" %d", num) && num++)

    {
    }
}

Q.2: Write a program for print day No. without using switch case statement.

In general case for switch case we use switch()  but here  we  complete this program without using switch case statement, otherwise switch( ) is known as decision making control statement.  

Ans.

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

void main()

{
    int dayno;
    printf("Enter day number");
    scanf("%d", &dayno);

    if (dayno = = 1)

    {
        printf("Monday");
    }

    else if (dayno = = 2)

    {
        printf("Tuesday");
    }

    else if (dayno = = 3)

    {
        printf("Wednesday");
    }

    else if (dayno = = 4)

    {
        printf("Thursday");
    }

    else if (dayno = = 5)

    {
        printf("Friday");
    }

    else if (dayno = = 6)

    {
        printf("Saturday");
    }

    else if (dayno = = 7)

    {
        printf("sunday");
    }

    else

    {
        printf("Invalid input");
    }
}

Q.3: Write a program for Swap between two nos, without using any third variable or any add / subtraction method. 

In general method we use swap() or any math math function for swapping Two variables.
But here see the difference. We use the operator.

Ans.


#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
{
    int a = 3, b = 7;
    printf("Without swapping: %d %d \n", a, b);
    a ^= b ^= a ^= b;
    printf("After swapping: %d %d", a, b);
    getch();
}

// Output:
// Without swapping: 3 7 // After swapping: 7 3

Post a Comment

0 Comments