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

Logical Operators In C

Logical Operators in C programming language return true(non-zero number) or false(0) value. Logical AND(&&) and logical OR(||) works on 2 operands. But logical NOT(!) works on single operand.

Related Read:
Relational Operators In C

Logical Operators

&& – Logical AND Operator.
|| – Logical OR Operator.
! – Logical NOT Operator.

&& – Logical AND Operator.

 
#include < stdio.h >

int main()
{
    int a;

    a = ( 1 && 1 );

    printf("value of a is %d\n", a);

    return 0;
}

Output:
value of a is 1

 
#include < stdio.h >

int main()
{
    int a;

    a = ( 0 && 1 );

    printf("value of a is %d\n", a);

    return 0;
}

Output:
value of a is 0

For logical AND(&&) both operands or expressions must yield to true. If any one condition is false(0), then it’ll return false(0).

|| – Logical OR Operator.

 
#include < stdio.h >
int main()
{
    int a;

    a = ( 1 || 1 );

    printf("value of a is %d\n", a);

    return 0;
}

Output:
value of a is 1

 
#include < stdio.h >
int main()
{
    int a;

    a = ( 1 || 0 );

    printf("value of a is %d\n", a);

    return 0;
}

Output:
value of a is 1

 
#include < stdio.h >
int main()
{
    int a;

    a = ( 0 || 0 );

    printf("value of a is %d\n", a);

    return 0;
}

Output:
value of a is 0

Logical OR(||) returns true(any non-zero number) if either one condition/operand is true. It returns false(0) only when both the conditions / operands are false(0).

! – Logical NOT Operator.

 
#include < stdio.h >
int main()
{
    int a;

    a = ( !1 );

    printf("value of a is %d\n", a);

    return 0;
}

Output:
value of a is 0

 
#include < stdio.h >
int main()
{
    int a;

    a = ( !0 );

    printf("value of a is %d\n", a);

    return 0;
}

Output:
value of a is 1

Logical NOT(!) returns true if the condition is false. It returns false if the condition is true. It just negates the Boolean value given to it.

Logical Operators In C


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

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


Logical Operators combined with Relational Operators

&& – Logical AND Operator.

 
#include < stdio.h >

int main()
{
    int a, b = 100;

    a = ( (b == 0) && (b > 50) );

    printf("Value of a is %d\n", a);

    return 0;
}

Output:
value of a is 1

a is true because both b is equal to 100 is true and b is greater than 50 is true.

 
#include < stdio.h >

int main()
{
    int a, b = 100;

    a = ( (b == 0) && (b > 150) );

    printf("Value of a is %d\n", a);

    return 0;
}

Output:
value of a is 0

a is false(0) because b is equal to 100 is true but b is greater than 150 is false.

|| – Logical OR Operator.

 
#include < stdio.h >

int main()
{
    int a, b = 100;

    a = ( (b == 0) || (b > 50) );

    printf("%d\n", a);

    return 0;
}

Output:
value of a is 1

Value of a is true, because b is equal to true. In logical OR(||) if one condition is true, then it returns true. It returns false(0) only when both the conditions / operands are false(0).

! – Logical NOT Operator.

 
#include < stdio.h >

int main()
{
    int a, b = 100;

    a = ( !(b == 0) );

    printf("%d\n", a);

    return 0;
}

Output:
value of a is 0

Value of a is false(0). Because b is equal to 100 is true. When true value is given to NOT(!) it’ll return false(0). When false value is supplied to NOT it’ll return true.

Note: = is assignment operator. == is equality operator.

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

C Program to Calculate Gross Salary of an Employee

In a company an employee is paid as under: If his basic salary is less than 5000 then he’ll get 10% of his base salary as HRA and 90% of his base salary as DA. If his basic salary is above 5000, then he’ll get 600 HRA and 95% of his base salary as DA.

HRA – House Rent Allowance.
DA – Dearness Allowance.

C Program to Calculate Gross Salary of an Employee


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

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


Source Code: Calculate Gross Salary of an employee

#include < stdio.h >

int main()
{
    float bs, gs, da, hra;

    printf("Enter basic salary\n");
    scanf("%f", &bs);

    if( bs < 5000 )
    {
        hra = bs * 10 / 100;
        da  = bs * 90 / 100;
    }
    else
    {
        hra = 600;
        da  = bs * 95 / 100;
    }

    gs = bs + da + hra;

    printf("Gross Salary is Rs %f\n", gs);

    return 0;
}

Output 1:
Enter basic salary
5500
Gross Salary is Rs 11325.000000

Output 2:
Enter basic salary
4000
Gross Salary is Rs 8000.000000

Formula to calculate Gross Salary is:
Gross Salary = Basic Salary + Dearness allowance + House Rent Allowance.

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