Determine the cyclomatic complexity for the following code: Accept year
if(year mod 4=0 and year mod 100!=0) or(year mod 400 =0)
print year is leap
else
print year is not leap
end if.
Select one:
a. 5
b. 4
c. 3
d. 6
Answers
Answer:
d. 6
Explanation:
Cyclomatic complexity :
In a strongly connected directed graph G, its cyclomatic complexity, denoted by V(G), is given by V(G) = e – n + p,
where,
• e is the number of edges in G
• n is the number of nodes in G
• p is the number of connected regions in G
In code that conforms to structured programming
(single entry, single exit), p = 1.
• In a directed graph that is not strongly connected,
V(G) = e – n + 2p.
Now, Let's consider given code :
1 If (year divisible by 4)
2 Then
3 If (year is NOT divisible by 100)
4 Then isLeap = True
5 Else
6 If (year is divisible by 400)
7 Then isLeap = True
8 Else isLeap = False
9 EndIf
V(G) = e – n + p
= 9 - 5 + 2
= 6
Answer:
answer: c.3
Explanation:
p=1
no of edges=2
no of nodes=3
cyclomatic comlexity= e+n-2p
=2+3-2(1)
=3