Unit 2.1 C Notes by Prof. Shahid Masood
Unit 2.1 C Notes by Prof. Shahid Masood
CABSMJ-1001
Problem Solving Using C
Unit - II
CABSMJ-1001 [2024-25]
2. Relational Operators
Used for comparing the two values, such as price of two items.
The result of relational operation is a boolean value 0 or 1
which indicates whether the operation is false or true.
C supports following SIX relational operators:
Operator Description Example:
int a=10, b=20;
== is equal to: checks if the values (a == b)
of two operands are equal /* 0 (false) */
!= is not equal to: checks if values (a != b)
of two operands are not equal /* 1 (true) */
> is greater than: checks if the (a > b)
value of left operand is greater /* 0 (false) */
than the value of right operand
Contd...
CABSMJ-1001 [2024-25]
3. Logical Operators
Used to combine two/more relational expressions together, so
that we can test two/more conditions.
The result of logical operators is a boolean value 0 or 1.
C supports following 3 logical operators:
Operator Description Example:
int a=10, b=20, c=30;
&& Logical AND: if both the operands (a>b && c==30)
are true (non-zero) then condition /* ( 0 && 1 ) is
becomes true (i.e. 1) evaluated to 0 */
|| Logical OR: if any one or both (a>b || c==30)
operands are true (non zero) then /* ( 0 || 1 ) is
condition becomes true (i.e. 1) evaluated to 1 */
! Logical NOT: used to reverse the !(a>b && c==30)
logical state of its operand /* !( 0 && 1 ) is
evaluated to 1Contd...
*/
CABSMJ-1001 [2024-25]
4. Assignment Operators
Used to assign the value of expression on its right to the
variable on its left.
C supports following SIX assignment operators:
Operator Description Example
= Simple Assignment: c = a + b;
Assigns the value of expression on its
right to the variable on its left.
3. More efficient.
For example:
a[i+2*x] = a[i+2*x] + y; /* add y to the array element */
can be written using shorthand assignment operator as:
a[i+2*x] += y; /* a[i+2*x] is evaluated only once. */
CABSMJ-1001 [2024-25]
Output
a=12, c=22
b=8, d=18
CABSMJ-1001 [2024-25]
General form:
value = exp1 ? exp2 : exp3;
e.g. bigger = x>y ? x : y;
Prob
8. Special Operators
C supports following TWO special operators:
(i). Comma operator
(ii). sizeof operator
C Expressions
and
Precedence of
Operators
CABSMJ-1001 [2024-25]
C Expressions
A C expression:
is a combination of operators and their operands
which results in a single value of type char/ int/ float/ double
etc.
Types of C Expressions
Example
1. Arithmetic expressions 4 - 2 + 12/8
Consist of operands and operators. =4–2+1
=3
2. Relational or Conditional expressions ( x%2 == 0 )
• Used to compare two operands or the
conditional operator is used. ( a<b )
• The result can be either zero (false) or non- true/false
zero value (true). exp1 ? exp2 : exp3
3. Logical expressions ( x>10 || y<11)
Consist of logical operators (&&, ||, ! ) that
result in either a zero (false) or non-zero ( a>b && b>c)
value (true).
CABSMJ-1001 [2024-25]
Precedence of C Operators
A C expression (containing different types of operators) is evaluated
based upon their operator precedence & associativity.
Operator precedence determines the order:
in which operators are performed in an expression containing
more than one operators with different precedence.
Operators Associativity
It is used when two operators of same precedence appear in
an expression.
It can be from left-to-right or right-to-left.
e.g. statement x = 5 + 6 * 2 / 3; assigns 9 to x (and not 22/3=7).
x=5+4
x=9
CABSMJ-1001 [2024-25]
Type Casting
CABSMJ-1001 [2024-25]
Implicit
Type Casting
There is a
chance of
No Loss in some Loss
data in data
Promotion or Narrowing
Widening conversion
conversion
CABSMJ-1001 [2024-25]
long float
long float
float
float
double
Data loss may happen double
int
CABSMJ-1001 [2024-25]
Mathematical
Library
Functions
CABSMJ-1001 [2024-25]
Contd...
CABSMJ-1001 [2024-25]
Hyperbolic
sinh(x) Returns hyperbolic sine of x (x in radians)
cosh(x) Returns hyperbolic cosine of x (x in radians)
tanh(x) Returns hyperbolic tangent of x (x in radians)
Other functions Raises value of arg x to the next int value and
ceil(x) returns it.
ceil(4.3) gives 5.0
floor(x) Decreases value of arg x to the previous int value
and returns it.
floor(4.8) gives 4.0
exp(x) Returns value of ex (e=2.71828 is base of natural
logarithm) e.g.
exp(2.0) gives 7.38905609893065
abs(x) Returns absolute value (+ve quantity) of integer
argument x. abs(-45) gives 45
Contd...
CABSMJ-1001 [2024-25]
Prob
Write a C program that reads principle, time, rate and number of
times interest is compounded per year (P, T, R, N) from the user
and finds compound interest. Compound interest formula is:
R N*T
CI = P * 1 + -P
N
Where,
P = initial principal amount
R = interest rate (if R = 5% then R = 5/100 = 0.05)
N = number of times interest is compounded per year
T = number of years
Output
Enter principal amount (p): 5000
Enter time in year (t): 2
Enter interest rate in percent (r): 5
Enter no. of times interest is compounded per year (n): 4
Compound Interest = 522.43
CABSMJ-1001 [2024-25]
Decision Making
and
Branching
CABSMJ-1001 [2024-25]
Statements Statement-2
if (expression)
F statement block
{
statement block;
} Statement following
Statement following if; if statement
Examples
Prob:
/* trianglearea.c */
#include<stdio.h> output:
#include<math.h> Enter 3 sides of triangle: 4 6 8
main() Area of Triangle = 11.618950
{ float a, b, c, s, area;
printf("Enter 3 sides of triangle: ");
scanf("%f %f %f",&a,&b,&c);
if ( (a+b)>c && (b+c)>a && (a+c)>b )
{ s = (a + b + c) / 2;
area = sqrt( s*(s - a) * (s - b) * (s - c) );
printf("Area of Triangle = %f\n",area);
}
else
printf("Three sides do not form a triangle\n");
}
CABSMJ-1001 [2024-25]
Prob:
/* smallestin4.c */
#include<stdio.h>
main()
{ int a, b, c, d, small;
printf("Enter four integers\n");
scanf("%d %d %d %d",&a,&b,&c,&d);
if (a==b && b==c && c==d)
printf("Numbers are equal\n");
else
{ small=a;
if (b<small) small=b;
if (c<small) small=c;
if (d<small) small=d; Output
Enter four integers
printf("Smallest = %d\n",small);
40 30 10 20
}
Smallest = 10
}
CABSMJ-1001 [2024-25]
Prob
/* chartest.c */
#include<stdio.h>
#include<ctype.h>
main()
{ char ch;
printf("Enter any character: ");
ch = getchar();
if ( isalpha(ch) )
printf("The character is an alphabet");
else if ( isdigit(ch) )
printf("The character is a digit");
else
printf("the character is other than alphabet and digit");
}
CABSMJ-1001 [2024-25]
Prob:
/* divby5and8.c */
Output
#include<stdio.h>
Enter an integer number: 120
main( )
Divisible by both 5 and 8
{ int a;
printf("Enter an integer number: ");
scanf("%d",&a);
if (a%5 == 0 && a%8 == 0)
printf("Divisible by both 5 and 8");
else if (a%8 == 0)
printf("Divisible by 8 only"); Output
else if (a%5 == 0) Enter an integer number: 100
Divisible by 5 only
printf("Divisible by 5 only");
else
printf("Divisible by none"); Output
} Enter an integer number: 98
Divisible by none
CABSMJ-1001 [2024-25]
Prob:
Write a C program that reads the annual income as input and
calculates the Income tax.
Use below given rules for Income Tax calculation:
T
income < 0
F
write "Invalid input"
income<= T
250000 itax = 0
F
itax=
income>=250001 and T
0.10*(income-
income<=500000
250000)
F
income>=500001 and T itax=
income<=1000000 25000 +
F
0.20*(income-
itax = 125000 + 0.30*(income-1000000) 500000)
stop
CABSMJ-1001 [2024-25]
Optional
break; set of stat-d
default:
set of statements-d; statement following
} switch
statement following switch;
CABSMJ-1001 [2024-25]
Prob:
T
n=1?
F write “Sunday”
n=2? T
F write “Monday”
T
n=3? T
F write “Tuesday”
T
n=4?
F write “Wednesday”
T
n=5?
F write “Thursday”
n=6? T
F write “Friday”
T
n=7?
F write “Saturday”
Stop
CABSMJ-1001 [2024-25]
Prob:
Note:
Flowchart
CABSMJ-1001 [2024-25]
Prob:
Write a C porgram that reads three integer numbers and finds
biggest using nested if-else statement.
How to
F (a>b) ? T
find the
biggest F T
F (b>c) ? T (a>c) ?
using
nested if-
else? bigc bigb bigc biga
write “Biggest=“,big
/* biggest.c illustrates the use of nested if-elseCABSMJ-1001
statement[2024-25]
*/
#include<stdio.h>
main()
{ int a, b, c, big;
printf("Enter 3 integer numbers\n");
scanf("%d %d %d",&a,&b,&c);
if ( a>b )
{ if ( a>c ) Inner if-else statement
big=a;
else
big=c;
}
else
{ if ( b>c )
big=b; Output:
else
Enter 3 integer numbers
big=c;
10 30 20
}
printf("biggest = %d",big);
biggest = 30
}