C interview programs: even or odd, prime or not, greatest number

by Madhulika Reddy on October 23, 2008


c interview questions Here are few of the frequently asked programs in C language interviews. Each of the questions have been answered with code and , output and explanation.Question.27(even/odd) is a good starter in an C interview for selecting entry level resources. Question.28(greatest of three) tests whether interviewee candidate is able to write a simple program using control structures. Question.29(prime or not) tests whether the candidate is able to write an algorithm for recognizing a prime number. It gives a fair idea of candidate’s programming ability at entry level. Question.30 tests the analytical and programming ability of the candidate. It is also a fair way to know about C programming knowledge of the candidate depending upon his/her choice of storing the variables. See all C interview questions in this blog in one page.

  1. Write a program to check whether a given number is even or odd.
  2. Program:

    #include <stdio.h>
    int main() {
    int a;
    printf("Enter a: \n");
    scanf("%d", &a);
    /* logic */
    if (a % 2 == 0) {
     printf("The given number is EVEN\n");
    }
    else {
     printf("The given number is ODD\n");
    }
    return 0;
    }

    Download Code

    Output:

    Enter a: 2
    The given number is EVEN
    Explanation with examples: 
    Example 1: If entered number is an even number
              Let value of 'a' entered is 4
              if(a%2==0) then a is an even number, else odd.
      i.e.    if(4%2==0) then 4 is an even number, else odd.
    
    To check whether 4 is even or odd, we need to calculate (4%2).
    /* % (modulus) implies remainder value. */
    /* Therefore if the remainder obtained when 4 is divided by 2
    is 0, then 4 is even. */
        4%2==0 is true
        Thus 4 is an even number.
    
    Example 2: If entered number is an odd number.
              Let value of 'a' entered is 7
              if(a%2==0) then a is an even number, else odd.
      i.e.    if(7%2==0) then 4 is an even number, else odd.
    
       To check whether 7 is even or odd, we need to calculate (7%2).
       7%2==0 is false  /* 7%2==1 condition fails and else part is
     executed */
       Thus 7 is an odd number.

    Back to top

  3. Write a program to find the greatest of three numbers.
  4. Program:

    #include <stdio.h>
    int main(){
    int a, b, c;
    printf("Enter a,b,c: \n");
    scanf("%d %d %d", &a, &b, &c);
    if (a > b && a > c) {
    printf("a is Greater than b and c");
    }
    else if (b > a && b > c) {
    printf("b is Greater than a and c");
    }
    else if (c > a && c > b) {
    printf("c is Greater than a and b");
    }
    else {
    printf("all are equal or any two values are equal");
    }
    return 0;
    }

    Download Code

    Output:

    Enter a,b,c: 3 5 8
     c is Greater than a and b
    Explanation with examples: 
    consider three numbers a=5,b=4,c=8
    
    if(a>b && a>c) then a is greater than b and c
    now check this condition for the three numbers 5,4,8 i.e.
    if(5>4 && 5>8) /* 5>4 is true but 5>8 fails */
    
    so the control shifts to else if condition
    else if(b>a && b>c) then b is greater than a and c
    now checking this condition for 5,4,8 i.e.
    else if(4>5 && 4>8) /* both the conditions fail */
    
    now the control shifts to the next else if condition
    else if(c>a && c>b) then c is greater than a and b
    now checking this condition for 5,4,8 i.e.
    else if(8>5 && 8>4)  /* both conditions are satisfied */
    
    Thus c is greater than a and b.

    Back to top

  5. Write a program to check whether a given number is a prime.
  6. A prime number is a natural number that has only one and itself as factors. Examples: 2, 3, 5, 7,… are prime numbers. Program:

    #include <stdio.h>
    main() {
    int n, i, c = 0;
    printf("Enter any number n: \n");
    scanf("%d", &n);
    /*logic*/
    for (i = 1; i <= n; i++) {
     if (n % i == 0) {
      c++;
     }
    }
    if (c == 2) {
     printf("n is a Prime number");
    }
    else {
     printf("n is not a Prime number");
    }
    return 0;
    }

    Download Code

    Output:

    Enter any number n: 7
    n is Prime
    Explanation with examples:

    consider a number n=5
    for(i=0;i<=n;i++) /* for loop is executed untill the n value equals i */
    i.e. for(i=0;i<=5;i++) /* here the for loop is executed untill i is equal to n */
    
    1st iteration: i=1;i<=5;i++
    here i is incremented i.e. i value for next iteration is 2
    now if(n%i==0) then c is incremented
    i.e.if(5%1==0)then c is incremented, here 5%1=0 thus c is incremented.
    now c=1;
    
    2nd iteration: i=2;i<=5;i++
    here i is incremented i.e. i value for next iteration is 3
    now if(n%i==0) then c is incremented
    i.e.if(5%2==0) then c is incremented, but 5%2!=0 and so c is not incremented, c remains 1
    c=1;
    
    3rd iteration: i=3;i<=5;i++
    here i is incremented i.e. i value for next iteration is 4
    now if(n%i==0) then c is incremented
    i.e.if(5%3==0) then c ic incremented, but 5%3!=0 and so c is not incremented, c remains 1
    c=1;
    
    4th iteration: i=4;i<=5;i++
    here i is incremented i.e. i value for next iteration is 5
    now if(n%i==0) then c is incremented
    i.e. if(5%4==0) then c is incremented, but 5%4!=0 and so c is not incremented, c remains 1
    c=1;
    
    5th iteration: i=5;i<=5;i++
    here i is incremented i.e. i value for next iteration is 6
    now if(n%i==0) then c is incremented
    i.e. if(5%5==0) then c is incremented, 5%5=0 and so c is incremented.
    i.e. c=2
    
    6th iteration: i=6;i<=5;i++
    here i value is 6 and 6<=5 is false thus the condition fails and control leaves the for loop.
    
    now if(c==2) then n is a prime number
    we have c=2 from the 5th iteration and thus n=5 is a Prime number.

    Back to top

  7. Write a program to find the greatest among ten numbers.
  8. Program:

    #include <stdio.h>
    int main() {
     int a[10];
     int i;
     int greatest;
     int position =0;
     printf("Enter ten values:");
     //Store 10 numbers in an array
     for (i = 0; i < 10; i++) {
      scanf("%d", &a[i]);
     }
     //Assume that a[0] is greatest
     greatest = a[0];
     for (i = 0; i < 10; i++) {
      if (a[i] > greatest) {
       greatest = a[i];
       position = i+1;
      }
     }
     printf("\n Greatest of ten numbers is %d at position %d \n", greatest, position);
     return 0;
    }

    Download Code

    Output:

    Enter ten values: 2 53 65 3 88 8 14 5 77 64
    Greatest of ten numbers is 88 at position 5

    Explanation with example: Entered values are 2, 53, 65, 3, 88, 8, 14, 5, 77, 64 They are stored in an array of size 10. let a[] be an array holding these values. /* how the greatest among ten numbers is found */ Let us consider a variable ‘greatest’. At the beginning of the loop, variable ‘greatest’ is assinged with the value of first element in the array greatest=a[0]. Here variable ‘greatest’ is assigned 2 as a[0]=2.

    Below loop is executed until end of the array 'a[]';.
    for(i=0; i<10;>greatest)
             {
                greatest= a[i];
             }
         }

    For each value of ‘i’, value of a[i] is compared with value of variable ‘greatest’. If any value greater than the value of ‘greatest’ is encountered, it would be replaced by a[i]. After completion of ‘for’ loop, the value of variable ‘greatest’ holds the greatest number in the array. In this case 88 is the greatest of all the numbers.


About the Author:  This post was written by Madhulika Reddy. You can connect with Madhulika on Orkut.


  • Ashik

    well,Ur Blog is so Nice ! but I was asked this question in C-DAC,Hyd

    “Write a program in C to find maximum of 2 Integers without using operators like ==,< =,>=,< ,>,!= anywhere in the Program”??
    I had been trying to implement using Bitwise but still I need to use “==” in last?? so anyway to write this program??

  • Anonymous

    In the above comment i forgot to ask about my other doubt.Can you make a small change in the program number 30 so that we can even print the position of the element??????

  • Anonymous

    you have really provided good examples.I have cleared my doubts with the help of your examples.
    It would be much better if you write all the programs without using return values.It would be more simple and easy for beginners to understand

  • Sridhar

    @Anonymous
    You have a point, but I wanted to emphasize the importance of return value of main function. Even if the programs were for the beginners, I wanted them to learn it correctly. I invite you to read about http://www.interviewmantra.net/2009/04/main-getchar-scanf-break-continue.html#ques1 the importance of return value of main function

  • Sridhar

    @Anonymous,
    I have modified program 30 to print the position of the element as you requested. Check it out.

  • Saurabh Manchanda

    @Ashik
    The code you need is here:
    http://pastebin.com/f3524e82a
    The explanation is as following:
    In a 2′s complement system, the negative numbers have the MSB set to 1.
    Now, if a number, say A, is subtracted from another number, say B, ,i.e.,(B – A), then
    if A>B
    the MSB of the resulting number will be 1 because (B – A) would be negative

    else if A<=B
    the MSB of the resulting number will be 0 because (B – A) would be positive/zero

    Here, the resulting number is the value (B – A)

    the expression
    ((1<< (sizeof(int)*8 – 1))&(b-a))?a:b
    is responsible for finding out the larger of the 2 numbers.

    (1< Doing a bitwise AND of this value with (B – A) would either result into a zero or a non-zero value, based on the previous explanation, thereby concluding the maximum number!

    PS : The above solution is implementation defined because it depends on how the implementation treats negative numbers.

    Regards,
    Saurabh Manchanda

  • sridhar

    a clickable link of the program written by saurabh manchanda above http://pastebin.com/f3524e82a

  • http://www.blogger.com/profile/16140009842587009950 Arya

    you hav provided really useful programs (for beginners)and a good explanation also….u can also include some more programs in recursive functions….

  • http://www.interviewmantra.net Sridhar

    Thanks for feedback Arya. We will surely add more programs in recursive functions

    - Editor of Interview Mantra

  • suresh

    i want program to find largest of givens numbers with out using array

  • Ganesh

    The Site is a wonderfull site. It had helped me a lot in refreshing my mind about C language. I’m tyankfull to the publisher of the site.
    Thankssss a lottt….

  • ASMAA

    Its nice.Above all the programs are very useful to recollect ‘c’ program and the explanation also we can easily understandable.

  • snoby

    at question no. 30, you are writing the statement of scanf as follows:
    scanf(“%d”, &a[i]);
    I think it should be without the & symbol as it is an array:
    scanf(“%d”, a[i]);

    correct me if i’m wrong please, thanks for that wonderful blog

  • Lokesh

    Really useful for computer students.hats of to u….

  • mikhal gayari

    .

  • Zeppelin Lad

    Thank you so very much for the help really !

  • http://www.orkut.com pankaj somde

    hi,,,,,,,,,,,,

  • un sok oeun

    It’s really helpful for us. Thanks so much!

  • Sridhar Jammalamadaka

    Hi Ashik, here is the answer.
    if(num1-num2)
        printf(“num1 is greater than num2″);
    else
       printf(“num1 is lesser than equal to num2″);

  • Narmadha

    explanation is very  nice  sir. i  can  undrestand  easily.  please  give  more  difficult  programs in c  with  explanation.

  • Rajput Mihir

    i m study comp. engg. in diploma this is very useful 4 me.

  • shankar

    eslly under stand and help of every one

  • Tl Vasavi

    can i have the code for second greatest prime number…plzzzz……
    example if  we had given a number say 10 then the output must be 13…

  • Bhushan

    very goooooooooooooood site to learn about c programs.
    thanks guyes …………. :-)

  • priyanka kumari

    tnx for this. i think its one of the site which help a lot to learn c program

Previous post:

Next post: