Posts

Showing posts with the label C Programming

C Puzzle 05

Image
Multiply a given number by 2 without using the Multiplication Operator It is possible with the help of the operator <<  << is a shift left operator  << is a binary and bitwise operator The Shift Left operator moves every bit of the operand towards the left and the space created at the right side(LSB) will be filled with a zero. How the shift left operator works? LSB stands for Least Significant bit MSB stands for Most Significant bit Syntax:       Operand = Operand << number of bits to shifted left Examples:   x=x<<1   //1 bit of x to be shifted left   y=y<<2  // 2 bits of y to be shifted left Note:  Performing the shift left operation for the operand by one time is equal to multiplying the operand by two. Program

C Puzzle - 04

Image
 Swap two numbers using a logical operator It is possible to swap two numbers without using a temporary variable and it is possible XOR operator. The truth table of the XOR operator Program #include <stdio.h> int main() {     int a,b;     printf("Enter two numbers\n");     scanf("%d%d",&a,&b);     printf("Before Sawp: a=%d.... b=%d",a,b);     a=a^b;     b=a^b;     a=a^b;     printf("\nAfter Swap: a=%d.... b=%d",a,b);     return 0; } OUTPUT

C Puzzle -03

What is the return value of printf() and scanf() functions in C? scanf() : It returns the number of inputs scanned successfully from keyboard. Example: #include <stdio.h> int main() {    int a,b,c,n;    printf("Enter the values for a, b and c\n");    n=scanf("%d%d%d",&a,&b,&c);  // The variable n receive the return value of scanf    printf("%d",n);    return 0; } Output Enter the values for a, b and c 10 20 30 3 printf() : It returns the number of bytes/characters printed on the screen. Example #include <stdio.h> int main() {    int n,x;    n=10;    x=printf("%d",n);    printf("\nx=%d bytes",x);    return 0; } Output 10 x=2 bytes

C Puzzle - 02

 What is the output of the following C program? #include <stdio.h> void main() { int a=25,b=4; printf("%d",(a%b)); } OUTPUT: 1 % operator gives you the remainder of an integer division.

C Puzzle

What is the output of the following code snippet? #include <stdio.h> void main(){ printf("%d",(12%2)); } Output: 0 Explanation: % is the mod operator in c, which is used to find remainder of an integer division.