Lab 3: Branching in Control Structure in C – C Programming Lab Report

Lab 3: Branching in Control Structure in C

A C Programming Lab Report for 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

Condition?
True
Execute ‘if’ Block
False
Execute ‘else’ Block

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:

Enter num1, num2 and num3: 5 10 15
The greatest number is 15

Algorithm:

  1. Start
  2. Declare integer variables a, b, c.
  3. Read three numbers from the user.
  4. If a > b, then check if a > c. If yes, a is greatest. Else, c is greatest.
  5. If a is not > b, then check if b > c. If yes, b is greatest. Else, c is greatest.
  6. Display the greatest number.
  7. Stop

Flowchart:

Start
Input a, b, c
a > b?
True
a > c?
True
Print a
False
Print c
False
b > c?
True
Print b
False
Print c
Stop

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:

Enter a, b, c of equation ax^2 + bx + c = 0: 2 -3 1
The roots of the given equation are 1.0 and 0.5

Algorithm:

  1. Start
  2. Declare variables a, b, c, d, r1, r2.
  3. Read coefficients a, b, c.
  4. Calculate discriminant d = b*b - 4*a*c.
  5. If d < 0, print "Imaginary roots".
  6. Else, calculate r1 = (-b + sqrt(d))/(2a) and r2 = (-b - sqrt(d))/(2a).
  7. Display the roots r1 and r2.
  8. Stop

Flowchart:

Start
Input a, b, c
d = b*b - 4*a*c
d < 0?
True
Print "Imaginary Roots"
False
Calculate r1, r2
Print r1, r2
Stop

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:

Enter Number: 7
7 x 1 = 7
7 x 2 = 14
... (lines for 3 through 9) ...
7 x 10 = 70

Algorithm:

  1. Start
  2. Declare variables n and i.
  3. Read a number n from the user.
  4. Start a for loop with i from 1 to 10.
  5. Inside the loop, print the multiplication result `n x i = n*i`.
  6. End loop.
  7. Stop

Flowchart:

Start
Input n
i = 1
i <= 10?
True
Print n*i
i++
False
Stop

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:

Enter a number: 11
11 is a prime number.

Algorithm:

  1. Start
  2. Declare variables n, i, and c=0.
  3. Read number n.
  4. If n <= 1, print "Neither prime nor composite" and go to Stop.
  5. Start a for loop from i=1 to n.
  6. Inside the loop, if n is divisible by i, increment c.
  7. End loop.
  8. If c is equal to 2, print "Prime".
  9. Else, print "Composite".
  10. Stop

Flowchart:

Start
Input n
n <= 1?
Print result
Stop

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:

Enter a Year: 2024
The year is a leap year.

Algorithm:

  1. Start
  2. Declare integer n.
  3. Read year n.
  4. If (n is divisible by 400) OR (n is divisible by 4 AND not divisible by 100), it is a leap year.
  5. Else, it is not a leap year.
  6. Display the result.
  7. Stop

Flowchart:

Start
Input year (n)
(n%400==0) ||
(n%4==0 && n%100!=0)?
True
Print "Leap Year"
False
Print "Not a Leap Year"
Stop

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.

Scroll to Top