Lab 3: Branching in Control Structure in C

Lab Report Information
Lab No.: 3
Title: Branching in Control Structure in C
Course: Computer Programming (CT 101), I Year I Part
Credit: Important Notes
C Programming Lab Report: Theory for Lab 3
Branching in Control Structure in C
This C Programming Lab Report explores how control statements alter the normal sequential flow of a program. They are essential for implementing logic through decision-making (branching) and repetition (looping).
Conditional (Branching) Statements
if
Statement: Executes code if a condition is true.if-else
Statement: Chooses between two blocks of code based on a condition.else-if
Ladder: Selects one path from multiple conditions.switch
Statement: A multi-way branch based on the value of an integer or character expression.
Example Flowchart for `if-else` Logic
C Programming Lab Report: Programs for Lab 3
1. Find the Greatest Among Three Numbers
Question:
Write a C program to find the greatest among three numbers entered by the user.
Program Code:
#include <stdio.h> #include <conio.h> void main() { int a, b, c; clrscr(); printf("Enter num1, num2, and num3: "); scanf("%d %d %d", &a, &b, &c); if (a > b) { if (a > c) { printf("The greatest number is %d", a); } else { printf("The greatest number is %d", c); } } else { if (b > c) { printf("The greatest number is %d", b); } else { printf("The greatest number is %d", c); } } getch(); }
Output:
The greatest number is 15
Algorithm:
- Start
- Declare integer variables a, b, c.
- Read three numbers from the user.
- If a > b, then check if a > c. If yes, a is greatest. Else, c is greatest.
- If a is not > b, then check if b > c. If yes, b is greatest. Else, c is greatest.
- Display the greatest number.
- Stop
Flowchart:
2. Find the Roots of a Quadratic Equation
Question:
Write a C program to find the roots of a quadratic equation ax^2 + bx + c = 0.
Program Code:
#include <stdio.h> #include <conio.h> #include <math.h> void main() { float a, b, c, r1, r2, d; printf("Enter a, b, c of equation ax^2 + bx + c = 0: "); scanf("%f %f %f", &a, &b, &c); d = (b*b) - (4*a*c); if (d < 0) { printf("The given equation has imaginary roots."); } else { r1 = (-b + sqrt(d)) / (2.0*a); r2 = (-b - sqrt(d)) / (2.0*a); printf("The roots of the given equation are %.1f and %.1f", r1, r2); } getch(); }
Output:
The roots of the given equation are 1.0 and 0.5
Algorithm:
- Start
- Declare variables a, b, c, d, r1, r2.
- Read coefficients a, b, c.
- Calculate discriminant d = b*b - 4*a*c.
- If d < 0, print "Imaginary roots".
- Else, calculate r1 = (-b + sqrt(d))/(2a) and r2 = (-b - sqrt(d))/(2a).
- Display the roots r1 and r2.
- Stop
Flowchart:
3. Display a Multiplication Table
Question:
Write a C program to display the multiplication table of an integer entered by the user using a for loop.
Program Code:
#include <stdio.h> #include <conio.h> void main() { int n, i; clrscr(); printf("Enter Number: "); scanf("%d", &n); for (i = 1; i <= 10; i++) { printf("%d x %d = %d\n", n, i, n * i); } getch(); }
Output:
7 x 1 = 7
7 x 2 = 14
... (lines for 3 through 9) ...
7 x 10 = 70
Algorithm:
- Start
- Declare variables n and i.
- Read a number n from the user.
- Start a for loop with i from 1 to 10.
- Inside the loop, print the multiplication result `n x i = n*i`.
- End loop.
- Stop
Flowchart:
4. Determine if a Number is Prime or Composite
Question:
Write a C program to determine whether a number entered by the user is prime or composite.
Program Code:
#include <stdio.h> #include <conio.h> void main() { int n, i, c = 0; printf("Enter a number: "); scanf("%d", &n); if (n <= 1) { printf("The number is neither prime nor composite."); } else { for (i = 1; i <= n; i++) { if (n % i == 0) { c++; } } if (c == 2) { printf("%d is a prime number.\n", n); } else { printf("%d is a composite number.\n", n); } } getch(); }
Output:
11 is a prime number.
Algorithm:
- Start
- Declare variables n, i, and c=0.
- Read number n.
- If n <= 1, print "Neither prime nor composite" and go to Stop.
- Start a for loop from i=1 to n.
- Inside the loop, if n is divisible by i, increment c.
- End loop.
- If c is equal to 2, print "Prime".
- Else, print "Composite".
- Stop
Flowchart:
5. Determine if a Year is a Leap Year
Question:
Write a C program to determine whether the year is a leap year or not.
Program Code:
#include <stdio.h> #include <conio.h> void main() { int n; clrscr(); printf("Enter a Year: "); scanf("%d", &n); if ((n % 400 == 0) || (n % 4 == 0 && n % 100 != 0)) { printf("The year is a leap year."); } else { printf("The year is not a leap year."); } getch(); }
Output:
The year is a leap year.
Algorithm:
- Start
- Declare integer n.
- Read year n.
- If (n is divisible by 400) OR (n is divisible by 4 AND not divisible by 100), it is a leap year.
- Else, it is not a leap year.
- Display the result.
- Stop
Flowchart:
(n%4==0 && n%100!=0)?
Discussion and Conclusion
This C Programming Lab Report on Branching in Control Structure in C has provided practical experience with decision-making and looping constructs. Through exercises like finding the greatest number, solving quadratic equations, and determining leap years, we have effectively applied `if`, `else-if`, and nested `if` statements. The use of `for` loops for generating multiplication tables and checking for prime numbers demonstrated their power in handling repetitive tasks. This lab solidifies the understanding that control structures are the essential building blocks for creating programs with dynamic behavior and complex logic.