0% found this document useful (0 votes)
5 views4 pages

Example Exp 4

The document outlines the steps to create a Control Flow Graph (CFG) and calculate Cyclomatic Complexity for a sample program that checks if a number is positive, even, or odd. It identifies 8 nodes and 9 edges, leading to a Cyclomatic Complexity of 3, indicating that a minimum of 3 test cases are required for thorough testing. The document also provides specific test cases and their expected outputs.

Uploaded by

8318413827ps
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Example Exp 4

The document outlines the steps to create a Control Flow Graph (CFG) and calculate Cyclomatic Complexity for a sample program that checks if a number is positive, even, or odd. It identifies 8 nodes and 9 edges, leading to a Cyclomatic Complexity of 3, indicating that a minimum of 3 test cases are required for thorough testing. The document also provides specific test cases and their expected outputs.

Uploaded by

8318413827ps
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

EXPERIMENT–4: CONTROL FLOW

GRAPH (CFG) & CYCLOMATIC


COMPLEXITY
Sample Program
#include <stdio.h>

int main()
{
int num;

printf("Enter a number: ");


scanf("%d", &num);

if (num > 0)
{
if (num % 2 == 0)
{
printf("Positive Even Number");
}
else
{
printf("Positive Odd Number");
}
}
else
{
printf("Negative or Zero Number");
}

return 0;
}

Step 1: Identify Nodes


Each statement or block becomes a node.

Node No Statement
1 Start
2 Input number
3 Check num > 0
4 Check num % 2 == 0
5 Print Positive Even
6 Print Positive Odd
Node No Statement
7 Print Negative or Zero
8 End

Total Nodes (N) = 8

Step 2: Identify Edges (Flow of Control)


Edges represent control flow between nodes.

1→2
2→3
3 → 4 (True)
3 → 7 (False)
4 → 5 (True)
4 → 6 (False)
5→8
6→8
7→8

Total Edges (E) = 9

Step 3: CFG Diagram (Text Representation)


You can draw this in your record.

(1) Start
|
v
(2) Input num
|
v
(3) num > 0 ?
/ \
Yes No
v v
(4) num%2==0? (7) Print Negative/Zero
/ \
Yes No
v v
(5) Print Even (6) Print Odd
\ /
\ /
v v
(8) End

Draw circles for nodes and arrows for edges in your practical file.

Step 4: Cyclomatic Complexity Calculation


Formula:

V(G) = E − N + 2

Substitute values:

E=9
N=8

V(G) = 9 − 8 + 2
V(G) = 3

Cyclomatic Complexity = 3

Step 5: Independent Paths


Number of independent paths = Cyclomatic Complexity = 3

Paths are:

Path 1
Start → Input → num > 0 (True) → num % 2 == 0 (True) → Print Even → End

Path 2
Start → Input → num > 0 (True) → num % 2 == 0 (False) → Print Odd → End

Path 3
Start → Input → num > 0 (False) → Print Negative or Zero → End

Step 6: Observation Table


Program Name Nodes (N) Edges (E) Cyclomatic Complexity
Number Check Program 8 9 3

Step 7: Interpretation
Cyclomatic Complexity = 3

This means:

• Minimum 3 test cases required


• Program is simple
• Easy to test and maintain

Step 8: Test Cases Example


Test Case Input Expected Output Path
TC1 8 Positive Even Number Path 1
TC2 5 Positive Odd Number Path 2
TC3 -2 Negative or Zero Number Path 3

Step 9: Final Result


Thus, the Control Flow Graph was drawn successfully for the given program. The number of
nodes is 8 and edges is 9. The Cyclomatic Complexity calculated using the formula V(G) = E −
N + 2 is 3. Hence, minimum 3 independent test cases are required to test the program
completely.

You might also like