Programming in C 312303 Manual Practical No 6: Implement C program using relational Answer

Programming in C 312303 Manual Practical No 6 Answer

In this Programming in C 312303 Manual Practical No 6 Students will be able to use relational and conditional operators in the C program.

Programming in C 312303 Manual Practical No 6: Implement C program using relational Answer
Programming in C 312303 Manual Practical No 6 Answer

Conditional operator: The conditional operator helps us make decisions in our program. It checks if something is true or false and then does something based on that. It’s like when we decide to do something only if a certain condition is met.

OperatorMeaning
<Less than
>Greater than
<=Less than or equal to
y=Greater than or equal to
==Equal to
!=Not equal to

The Conditional Operator (?:) is like a shortcut for making decisions in programming. It has three parts: a comparison, a result for when the comparison is true, and a result for when the comparison is false. It’s like a quick way to say “If this is true, do this, otherwise do that.” It can make your code easier to understand and write in less space.

Exercise

1. Develop a program to print two numbers are equal or not.

Answer:
#include <stdio.h>
int main() {
    int num1, num2;
    // Prompt user for input
    printf(“Enter two integers: “);
    // Read the numbers using scanf
    scanf(“%d %d”, &num1, &num2);
    // Check if the numbers are equal using the relational operator ‘==’.
    // Store the result (true or false) in a boolean variable ‘isEqual’
    int isEqual = (num1 == num2);
    // Use a conditional statement (if-else) to print the result
    if (isEqual) {
        printf(“%d and %d are equaln”, num1, num2);
    } else {
        printf(“%d and %d are not equaln”, num1, num2);
    }
    return 0;
}
Output:
Enter two integers: 1 2
1 and 2 are not equal
=== Code Execution Successful ===
2. Develop a program to print the largest of two numbers using conditional operator.


Answer:
#include <stdio.h>

int main() {
    int num1, num2, largest;

    // Prompt user for input
    printf(“Enter two numbers: “);
    scanf(“%d %d”, &num1, &num2);

    // Use conditional operator to determine largest number
    largest = (num1 > num2) ? num1 : num2;

    // Print the largest number
    printf(“The largest number is: %dn”, largest);

    return 0;
}

Output:
Enter two numbers: 2 8
The largest number is: 8
=== Code Execution Successful ===

Algorithm

1. Start
2. Input:
Prompt the user to enter two numbers.
Read the entered numbers and store them in variables num1 and num2.
3. Compare and Determine Largest:
Use a conditional statement to compare num1 and num2.
If num1 is greater than num2:
Set largest to num1.
Otherwise (if num2 is greater than or equal to num1):
Set largest to num2.
4. Output:
Print the value stored in largest as the “The largest number is: “.
5. End

Flowchart

        +—————–+
          |       Start       |
          +—————–+
                   |
                   v
          +—————–+
          | Get num1 (Input) |
          +—————–+
                   |
                   v
          +—————–+
          | Get num2 (Input) |
          +—————–+
                   |
                   v
          +—————–+
          | Is num1 > num2?   | (Decision Diamond)
          +—————–+
                   |
                   v (Yes)
          +—————–+
          | largest = num1    | (Assignment)
          +—————–+
                   |
                   v (No)
          +—————–+
          | largest = num2    | (Assignment)
          +—————–+
                   |
                   v
          +—————–+
          | Print largest     | (Output)
          +—————–+
                   |
                   v
          +—————–+
          |       End        |
          +—————–+

Practical Related Questions

1. Write a program for the largest of three numbers.

Answer:
#include <stdio.h>
int main() {
    int num1, num2, num3, largest;
    // Prompt user for input
    printf(“Enter three numbers: “);
    scanf(“%d %d %d”, &num1, &num2, &num3);
    // Use nested conditional operator to determine largest number
    largest = (num1 > num2) ? (num1 > num3 ? num1 : num3) : (num2 > num3 ? num2 : num3);
    // Print the largest number
    printf(“The largest number is: %dn”, largest);
    return 0;
}
Output:
Enter three numbers: 3 8 9
The largest number is: 9
=== Code Execution Successful ===
2. Write a program to demonstrate the working of relational operator.
Answer:
#include <stdio.h>
int main() {
    int num1, num2;
    // Prompt user for input
    printf(“Enter two numbers: “);
    scanf(“%d %d”, &num1, &num2);
    // Relational Operator Demonstrations
    printf(“nRelational Operator Results:n”);
    // Equal To (==)
    if (num1 == num2) {
        printf(“%d is equal to %d.n”, num1, num2);
    } else {
        printf(“%d is not equal to %d.n”, num1, num2);
    }
    // Not Equal To (!=)
    if (num1 != num2) {
        printf(“%d is not equal to %d.n”, num1, num2);
    } else {
        printf(“%d is equal to %d (This shouldn’t print if the first condition is true).n”, num1, num2);
    }
    // Greater Than (>)
    if (num1 > num2) {
        printf(“%d is greater than %d.n”, num1, num2);
    } else {
        printf(“%d is not greater than %d.n”, num1, num2);
    }
    // Less Than (<)
    if (num1 < num2) {
        printf(“%d is less than %d.n”, num1, num2);
    } else {
        printf(“%d is not less than %d.n”, num1, num2);
    }
    // Greater Than or Equal To (>=)
    if (num1 >= num2) {
        printf(“%d is greater than or equal to %d.n”, num1, num2);
    } else {
        printf(“%d is not greater than or equal to %d.n”, num1, num2);
    }
    // Less Than or Equal To (<=)
    if (num1 <= num2) {
        printf(“%d is less than or equal to %d.n”, num1, num2);
    } else {
        printf(“%d is not less than or equal to %d.n”, num1, num2);
    }
    return 0;
}
Output:
Enter two numbers: 5 6
Relational Operator Results:
5 is not equal to 6.
5 is not equal to 6.
5 is not greater than 6.
5 is less than 6.
5 is not greater than or equal to 6.
5 is less than or equal to 6.
=== Code Execution Successful ===
3. Write a program using a conditional operator to check whether a user can vote or not based on their age.
Answer:
#include <stdio.h>
int main() {
    int age;
    // Prompt user for input
    printf(“Enter your age: “);
    scanf(“%d”, &age);
    // Check eligibility using conditional operator
    (age >= 18) ? printf(“You are eligible to vote!n”) : printf(“You are not eligible to vote yet.n”);
    return 0;
}
Output:
Enter your age: 20
You are eligible to vote!
=== Code Execution Successful ===

Conclusion

We successfully completed practical no.6 of Programming in C 312303 Manual in which we Implemented a C program using relational and conditional Operators.

Suraj Diware
Suraj Diware

Hi everyone, I'm Suraj Diware, and I'm passionate about helping students succeed in MSBTE programs. This blog is dedicated to providing clear and concise explanations of MSBTE curriculum topics, along with practical tips and resources to support your learning journey.

Articles: 701

Leave a Reply

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