The greatest mistake you can make in life is to be continually fearing you will make one

Friday 12 March 2010

C Interview Questions

1.How to write a c program without using semicolon?
Solution

1.#include<stdio.h>
main()
{
if(printf("Welcome")){}
}
output
Welcome
2.#include<stdio.h>
main()
{
while(printf("Welcome"),0){}
}
output
Welcome
The while loop will execute until the condition is true. so the statement will print continuously to avoid that comma operator is used. The comma operator ignores the 1st argument and return the 2nd one. so the value o is given to while loop and the while loop is executed only once

2.Write a c program to print the number of digits in a decimal number?
#include<stdio.h>
int main()
{
    int n,count=0,x;
    printf("ENter the decimal number\n");
    scanf("%d",&n);
    x=n;
    while(n>0)
    {
        count++;
        n=n/10;
    }
    printf("Number %d has %d digits\n",x,count);
}
output
ENter the decimal number
2345
Number 2345 has 4 digits

3.Write a C program to print the two's complement of a decimal number?
 Solution
#include<stdio.h>
int main()
{
    int n,x;
    printf("Enter a number\n");
    scanf("%d",&n);
    x=n;
    n=(~n)+1;
    printf("Two's complement of %d=%d",x,n);
    return 0;
}
Output
Enter a number 81
Two's complement of 81=-81

1 comment: