Tuesday, December 13, 2011

Frequently Asked C Programs

Write functions / code for the following C Programs

1.Write a program to count number of 1]characters,2]lines & words in a given sentence[may be taken from standard input]?Also count the number of digits, white spaces and others in an input

2.C Program to check whether stack grows up or down

3.Write a c program to check given year is leap year or not.

4.fast mutiply by 7

5.Function to compare floating point numbers in C

6.Code to round two numbers

7.random number generator in C

8.sqrt() function

13. Write a c program to print ASCII value of all characters.
14. C program to print hello world without using semicolon
15. Write a c program which produces its own source code as its output


Useful Links:--
http://www.contrib.andrew.cmu.edu/~rrv/updated%20cracktheinterview/www.cracktheinterview.com/adfaqpublish.html


3 comments:

  1. 7.Fibonacci series without recursion

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

    int main()
    {
    int initial_value=0; int final_value=1,temp_value,count=10;
    // count contains the number of elements to be generated.....
    for(count=1;count<=10;count++)
    {
    temp_value=initial_value+final_value;
    printf("\n%d",initial_value);
    initial_value=final_value;
    final_value=temp_value;
    }

    getch();
    return 0;
    }

    ReplyDelete
  2. 1.Word Count in a sentence

    Logic:-The logic behind this program is checking if the character in the sentence is a 'blank' and with the conditions that the next is not a blank character, or current character position is not equal to length of the sentence. If so, variable 'words' is incremented by one as one new word found.


    #include < stdio.h >
    #include < conio.h >
    int main()
    {
    char str[50];
    int words = 0, len = 0, i;
    printf("\n\n\t ENTER A STRING...: ");
    gets(str);

    while(str[len]!='\0')
    len++;

    len--;
    for(i=0;i <= len;i++)
    {
    if ( ( str[i] == ' ' && str[i+1] != ' ' ) || i == len )
    words++;
    }
    printf("\n\t NUMBER OF WORDS IN THE ABOVE SENTENCE IS...: %d", words);
    getch();
    return 0;
    }

    ReplyDelete
  3. 3.Stack Direction---------

    The following program creates an integer (a) on the stack, then passes a pointer to a down to the function sub. sub creates another integer on the stack (b), then compares the address of a to the address of b. If b’s address is greater then a’s, then the stack is growing up, if it’s less, then it’s growing down.

    #include

    void sub(int *a) {
    int b;

    if (&b > a) {
    printf("Stack grows up.");
    } else {
    printf("Stack grows down.");
    }
    }

    main () {
    int a;
    sub(&a);
    }


    NOTE-------------
    Why can’t we declare two integers in the main() function and check if the stack grows up or down? Any reason for creating a separate sub() function?

    Ans:Which integer of the two would we expect to be higher / lower in this case? I’m not sure there’s anything to say they would go into main’s stack frame in a particular order, especially if the compiler is trying to optimise (but I’m no expert).

    With the sub function, we know for sure that the second integer will be in a different stack frame, which must be either higher or lower (and hence everything in it will be higher or lower).

    ReplyDelete