Programming in C 312303 Practical No.7 Implement minimum two C programs using Logical Operators

Programming in C 312303 Manual Practical No.7 Answers

Programming in C 312303 Manual Practical No.7 Answers
Programming in C 312303 Manual Practical No.7 Answers
In this Programming in C 312303 Practical No.7, we are going to learn about Implementing a minimum of two C programs using Logical Operators. Students will be able to understand logical operators. After completion of the practical students will be able to use logical operators in the C program.

Logical Operators

  • Logical operators in C are used to combine multiple conditions/constraints.
  • Logical Operators return either 0 or 1, depending on whether the expression
  • result is true or false. In C programming for decision-making, we use logical operators.
We have 3 logical operators in the C language:
  1. Logical AND: &&
  2. Logical OR: ||
  3. Logical NOT:!

Exercise

Write a C program to:
1. Identify whether given number is Positive even, Positive odd, Negative even or
Negative odd.
#include <stdio.h>
int main() {
    int num;
    // Prompt user for input
    printf(“Enter an integer: “);
    scanf(“%d”, &num);
    // Combine logical operators to determine the category
    if (num > 0) {
        if (num % 2 == 0) {
            printf(“%d is Positive Evenn”, num);
        } else {
            printf(“%d is Positive Oddn”, num);
        }
    } else if (num < 0) {
        if (num % 2 == 0) {
            printf(“%d is Negative Evenn”, num);
        } else {
            printf(“%d is Negative Oddn”, num);
        }
    } else {
        printf(“%d is Zeron”, num); // Handle the case of zero
    }
    return 0;
}
Output:

Enter an integer: 2
2 is Positive Even
=== Code Execution Successful ===
2. Find the greatest/smallest number between the given 3 numbers.

Answer :
#include <stdio.h>
int main() {
    int num1, num2, num3, largest, smallest;
    // Prompt user for input
    printf(“Enter three numbers: “);
    scanf(“%d %d %d”, &num1, &num2, &num3);
    // Find the largest number
    largest = (num1 > num2) ? (num1 > num3 ? num1 : num3) : (num2 > num3 ? num2 : num3);
    // Find the smallest number (using multiple conditions)
    smallest = (num1 < num2 && num1 < num3) ? num1 :  // Check if num1 is smallest
              ((num2 < num3) ? num2 : num3);   // Otherwise, check num2 or num3
    // Print the results
    printf(“The largest number is: %dn”, largest);
    printf(“The smallest number is: %dn”, smallest);
    return 0;
}
Output:

Enter three numbers: 3 7 4
The largest number is: 7
The smallest number is: 3
=== Code Execution Successful ===

Algorithm

1. Start

2. Input:
Declare three integer variables num1, num2, and num3 to store the input numbers. Prompt the user to enter three numbers using printf(“Enter three numbers: “). Read the user’s input using scanf(“%d %d %d”, &num1, &num2, &num3).
3. Find Largest Number:
Use a nested conditional operator to determine the largest number:
Check if num1 is greater than num2:
If yes, compare num1 with num3:
If num1 is also greater than num3, then num1 is the largest.
If num3 is greater than or equal to num1, then num3 is the largest.
If num2 is greater than or equal to num1, compare num2 with num3:
If num2 is greater than num3, then num2 is the largest.
If num3 is greater than or equal to num2, then num3 is the largest.
Assign the largest number found to the variable largest.
4. Find Smallest Number:
Use a combination of logical operators and a conditional operator:
Check if num1 is less than both num2 and num3 using the expression (num1 < num2 && num1 < num3).
If true, num1 is the smallest and assign it to smallest.
If the first condition is false, use a ternary operator to check num2 and num3:
Assign the smaller value between num2 and num3 to the variable smallest.
5. Output:
Print the largest number stored in largest using printf(“The largest number is: %dn”, largest).
Print the smallest number stored in smallest using printf(“The smallest number is: %dn”, smallest).

Flow Chart

         +—————–+
          |       Start       |
          +—————–+
                   |
                   v
          +—————–+
          | Get num1 (Input) |
          +—————–+
                   |
                   v
          +—————–+
          | Get num2 (Input) |
          +—————–+
                   |
                   v
          +—————–+
          | Get num3 (Input) |
          +—————–+
                   |
                   v
          +—————–+
          |  num1 > num2?     | (Decision Diamond)
          +—————–+
                   |
                   v (Yes)
          +—————–+
          |  num1 > num3?     | (Decision Diamond)
          +—————–+
                   |
                   v (Yes)
          +—————–+
          |  largest = num1   | (Assignment)
          +—————–+
                   |
                   v (No)
          +—————–+
          |  largest = num3   | (Assignment)
          +—————–+
                   |
                   v (No) – Implicit “else” for outer diamond
          +—————–+
          |  num2 > num3?     | (Decision Diamond)
          +—————–+
                   |
                   v (Yes)
          +—————–+
          |  largest = num2   | (Assignment)
          +—————–+
                   |
                   v (No)
          +—————–+
          |  largest = num3   | (Assignment)
          +—————–+
                   |
                   v
          +—————–+
          | Check smallest     |
          +—————–+
          |  (num1<num2 &&     |
          |   num1<num3)?       | (Multiple Conditions)
          +—————–+
                   |
                   v (Yes)
          +—————–+
          |  smallest = num1  | (Assignment)
          +—————–+
                   |
                   v (No)
          +—————–+
          |  smallest =        | (Ternary Operator)
          |  (num2<num3)?num2:   |
          |   num3              |
          +—————–+
                   |
                   v
          +—————–+
          | Print largest     | (Output)
          +—————–+
                   |
                   v
          +—————–+
          | Print smallest     | (Output)
          +—————–+
                   |
                   v
          +—————–+
          |       End        |
          +—————–+

Practical Related Questions

1. What are different logical operators?
Answer:

Operator Description Example Result
&& (AND) Performs logical conjunction. Both operands must be non-zero (true) for the result to be non-zero (true). (5 > 3) && (2 != 4) 1 (true) – Both conditions are true
` ` (OR) Performs logical disjunction. At least one operand must be non-zero (true) for the result to be non-zero (true). `(1 < 0)
! (NOT) Performs logical negation. Inverts the truth value of the operand. !(!0) 0 (false) – Negates the negation (true becomes false)
2. Write a program that accepts marks of 5 subjects calculate the percentage and display grade of student from percentage. (Grade Distinction, first class, second class, pass class, fail).
Answer:
#include <stdio.h>
int main() {
    int subject1, subject2, subject3, subject4, subject5, totalMarks, percentage;
    char grade;
    // Prompt user for input
    printf(“Enter marks for 5 subjects (out of 100):n”);
    scanf(“%d %d %d %d %d”, &subject1, &subject2, &subject3, &subject4, &subject5);
    // Calculate total marks
    totalMarks = subject1 + subject2 + subject3 + subject4 + subject5;
    // Calculate percentage
    percentage = (totalMarks * 100) / 500; // Assuming maximum marks per subject is 100
    // Determine grade based on percentage
    if (percentage >= 90) {
        grade = ‘A’; // Distinction
    } else if (percentage >= 80) {
        grade = ‘B’; // First Class
    } else if (percentage >= 70) {
        grade = ‘C’; // Second Class
    } else if (percentage >= 40) {
        grade = ‘D’; // Pass Class
    } else {
        grade = ‘F’; // Fail
    }
    // Print the results
    printf(“Total marks: %dn”, totalMarks);
    printf(“Percentage: %d%%n”, percentage);
    printf(“Grade: %cn”, grade);
    return 0;
}
Output:

Enter marks for 5 subjects (out of 100):
80 80 80 80 80
Total marks: 400
Percentage: 80%
Grade: B
=== Code Execution Successful ===
4. Write a program to find whether the given number is even and divisible by 5 or not.

Answer:
#include <stdio.h>
int main() {
    int number;
    // Prompt user for input
    printf(“Enter a number: “);
    scanf(“%d”, &number);
    // Check for even and divisibility by 5 (combined condition)
    if (number % 2 == 0 && number % 5 == 0) {
        printf(“%d is even and divisible by 5.n”, number);
    } else {
        // If not both even and divisible by 5, provide separate checks
        printf(“%d is “, number);
        if (number % 2 == 0) {
            printf(“even”);
        } else {
            printf(“odd”);
        }
        printf(” but not divisible by 5.n”);
    }
    return 0;
}
Output:
Enter a number: 7
7 is odd but not divisible by 5.
=== Code Execution Successful ===

Conclusion 

We successfully completed practical no.7 of Programming in C 312303 manual and answered all practical related questions given in this practical.

Leave a Reply

Your email address will not be published. Required fields are marked *