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.
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.
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.
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.
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.
&& – 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 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.
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