C Programming Interview / Viva Q&A: 1

Guess the output of this simple C program. You can check these 2 video tutorials listed below so that you get to know todays interview / viva question.

Related Read:
if else statement in C
Relational Operators In C

Whats the output of this C Program?

 
#include < stdio.h >

int main()
{
    int num;

    printf("Enter a number\n");
    scanf("%d", &num);

    if(num = 5)
    {
        printf("You entered number 5\n");
    }
    else
    {
        printf("You entered %d \n", num);
    }

    return 0;
}

If user enters num value as 10. What does the above C Program output?

Answer: Did you answer – You entered 10 as output? If yes, then its wrong.

Note:
1. If at first glance of the source code you didn’t get the answer right, then go through the videos I’ve posted in ‘Related Read” section above.
2. After going through the 2 videos I’ve posted in ‘Related Read’ section above, if you still didn’t get the answer, then please open your editor and type the program and execute it one line at a time.

Hint: assignment operator(=) and equality operator(==) are different and work differently in C programming.

C Programming Interview / Viva Q&A: 1


[youtube https://www.youtube.com/watch?v=Zg04m3hF69E]

YouTube Link: https://www.youtube.com/watch?v=Zg04m3hF69E [Watch the Video In Full Screen.]


Here is the Answer:

As you can see in the condition of if, it’s written as num = 5. = is assignment operator. So here 5 is being assigned to variable num, and thus it returns 5. In C program, any non-zero number is considered as true. So every time the condition in if becomes true, so the block of code present in if gets executed. i.e., value of num is 5.

using equality operator for comparison

 
#include < stdio.h >

int main()
{
    int num;

    printf("Enter a number\n");
    scanf("%d", &num);

    if(num == 5)
    {
        printf("You entered number 5\n");
    }
    else
    {
        printf("You entered %d \n", num);
    }

    return 0;
}

Output 1:
Enter a number
10
You entered 10

Output 2:
Enter a number
5
You entered number 5

Output 3:
Enter a number
1000
You entered 1000

Note: Both programs are correct syntactically. That’s the reason compiler did not throw any error. It’s a logical error, so we need to be very careful while writing programs. If we do this simple logical error in a big program, it might take a lot of time to get to know the bug and fix it. So careful. Happy coding.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Employee Bonus Calculation: C

In this c program, we ask the user/employee to enter current year and his / her year of joining the organization/company. We check if the employee has served the company / organization for more than 3 years. If yes, then we give him/her a bonus of $2500 and if he or she has served for less than 3 years we’ll give $1000 as bonus.

Related Read:
if else statement in C
Relational Operators In C

Source Code: Employee Bonus Calculation: C

 
#include < stdio.h >

int main()
{
    int bonus, cy, yoj;

    printf("Enter current year and year of joining\n");
    scanf("%d %d", &cy, &yoj);

    if( (cy - yoj) > 3 )
    {
        bonus = 2500;
    }
    else
    {
        bonus = 1000;
    }

    printf("You get a bonus of %d \n", bonus);

    return 0;
}

Output 1:
Enter current year and year of joining
2019
2017
You get a bonus of 1000

Output 2:
Enter current year and year of joining
2019
2014
You get a bonus of 2500

C Program to Calculate Employee Bonus


[youtube https://www.youtube.com/watch?v=nB7ngERD3Tc]

YouTube Link: https://www.youtube.com/watch?v=nB7ngERD3Tc [Watch the Video In Full Screen.]


In this program we check if the employee has worked for 3 years in the organization or not. We subtract his year of joining from current year and then based on that we decide his or her bonus.

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Discount on Purchase: C Program

In this c program, we ask the user to input the price of the item and number of items(quantity) purchased. If the quantity is greater than 999 then we’ll give 10% discount orelse there will be no discount.

Related Read:
if else statement in C
Basic Arithmetic Operations In C

Source Code: To Calculate Total Expense

 
#include < stdio.h >

int main()
{
    int qty, dis;
    float rate, total;

    printf("Enter rate and quantity\n");
    scanf("%f %d", &rate, &qty);

    if(qty > 999)
    {
        dis = (qty * rate) * 10 / 100;
    }
    else
    {
        dis = 0;
    }

    total = (rate * qty) - dis;

    printf("Total Paid is Rs %f\n", total);

    return 0;
}

Output 1:
Enter rate and quantity
2
1000
Total Paid is Rs 1800.000000

Output 2:
Enter rate and quantity
2
500
Total Paid is Rs 1000.000000

Here is user purchases more than 999 items(quantity) then we provide a discount of 10%. Its calculated on the total purchase amount – which is calculated by multiplying rate and total quantity.

Discount on Purchase: C Program


[youtube https://www.youtube.com/watch?v=Af34KD12V_Y]

YouTube Link: https://www.youtube.com/watch?v=Af34KD12V_Y [Watch the Video In Full Screen.]


According to the program logic, if the user purchases quantity above 999, then he / she will get 10% discount orelse no discount on purchase.

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Even or Odd Number without using Modular Division: C Program

Today lets write a C program to check whether a user entered integer number is EVEN or ODD, without using modular division(%) operator.

Related Read:
Even or Odd Number: C Program (using Modular Division Operator)
Basic Arithmetic Operations In C
Relational Operators In C

An even number is an integer that is exactly divisible by 2. An odd number is an integer that is not exactly divisible by 2.

Note:
1. Even numbers are of the form 2 * n;
2. Odd numbers are of the form (2 * n + 1);

You can substitute n value in above forms and check the resulting numbers for even or odd.

Even or Odd Number: Source Code

 
#include < stdio.h >

int main()
{
    int n;

    printf("Enter an integer number\n");
    scanf("%d", &n);

    if( (n/2)*2 == n )
    {
        printf("%d is even number\n", n);
    }
    else
    {
        printf("%d is odd number\n", n);
    }

    return 0;
}

Output 1
Enter an integer number
2
2 is even number

Output 2
Enter an integer number
3
3 is odd number

Output 3
Enter an integer number
7
7 is odd number

Output 4
Enter an integer number
12
12 is even number

C Program To Check Even or Odd Number, without using Modular Division Operator


[youtube https://www.youtube.com/watch?v=qc-wK_amMzs]

YouTube Link: https://www.youtube.com/watch?v=qc-wK_amMzs [Watch the Video In Full Screen.]


Note: Division of an integer number by 2(which is also an integer number) always returns integer number.

In above c program, we ask the user to input an integer value and store it in variable n. Next using if else condition, we check if the user entered number is even or odd.

User entered integer number is divided by 2. And then it is multiplied by 2. If the final result is equal to the original user entered value, then the user entered value is even orelse its odd.

Example 1:
If user enters n = 2;
Applying n = 2 to ( (n/2)*2 == n ).
( (2/2)*2 == 2 )
( (1)*2 == 2 )
( 2 == 2 ) // true

So user entered value, that is, 2 is even number.

Example 2:
If user enters n = 3;
Applying n = 3 to ( (n/2)*2 == n ).
( (3/2)*2 == 3 )
( (1)*2 == 3 )
( 2 == 3 ) // false

2 is not equal to 3. So user entered value, that is, 3 is odd number.
In above example, 3/2 gives back 1 and not 1.5 as 3 is divided by integer which returns only the integer part and discards the decimal part.

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Even or Odd Number: C Program

Today lets write a C program to check whether a user entered integer number is EVEN or ODD.

Related Read:
Basic Arithmetic Operations In C
Relational Operators In C

Even or Odd Number without using Modular Division: C Program
Even or Odd Number using Ternary Operator: C Program

An even number is an integer that is exactly divisible by 2. An odd number is an integer that is not exactly divisible by 2.

C program To check Even or Odd Number: Source Code

 
#include < stdio.h >

int main()
{
    int n;

    printf("Enter an integer number\n");
    scanf("%d", &n);

    if(n % 2 == 0)
    {
        printf("%d is even number\n", n);
    }
    else
    {
        printf("%d is odd number\n", n);
    }

    return 0;
}

Output 1
Enter an integer number
2
2 is even number

Output 2
Enter an integer number
3
3 is odd number

Output 3
Enter an integer number
7
7 is odd number

Output 4
Enter an integer number
12
12 is even number

C program To check Even or Odd Number


[youtube https://www.youtube.com/watch?v=CAgJxUh8bEg]

YouTube Link: https://www.youtube.com/watch?v=CAgJxUh8bEg [Watch the Video In Full Screen.]


Modular division returns remainder of division. For example, 12 / 2 = 6. But 12 % 2 = 0.

In above c program, we ask the user to input an integer value and store it in variable n. Next using if else condition, we check if the user entered number is perfectly divisible by 2. If its perfectly divisible by 2, then it’e even number or else its odd number.

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert