0% found this document useful (0 votes)
2 views

PSC_Exp 3-1

The document outlines the development of C programs utilizing decision-making constructs such as if, if-else, else-if, switch, and the conditional operator. It includes algorithms and example programs for checking if a number is positive, finding the maximum of three numbers, displaying the day of the week based on user input, and determining if a number is even or odd. Key points emphasize the importance of using curly braces for code blocks and the appropriate use of the switch statement and conditional operator.

Uploaded by

guruneshm1
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

PSC_Exp 3-1

The document outlines the development of C programs utilizing decision-making constructs such as if, if-else, else-if, switch, and the conditional operator. It includes algorithms and example programs for checking if a number is positive, finding the maximum of three numbers, displaying the day of the week based on user input, and determining if a number is even or odd. Key points emphasize the importance of using curly braces for code blocks and the appropriate use of the switch statement and conditional operator.

Uploaded by

guruneshm1
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Ex. No.

3
Develop C programs using Decision - making
constructs.
Date:

Aim: D e v e l o p C p r o g r a m s u s i n g D e c i s i o n - m a k i n g c o n s t r u c t s .
Study Material:
Decision-making constructs in C help control the flow of a program based on conditions. These
constructs include if, if-else, else-if, switch, and conditional (?:) statements.
1. Decision-Making Constructs in C
1.1. if Statement
The if statement allows you to execute a block of code only if a specified condition evaluates to
true.
Syntax:
if (condition) {
// Code to execute if condition is true
}
1.2. if-else Statement
The if-else statement allows you to define an alternate block of code to execute if the condition
evaluates to false.
Syntax:
if (condition) {
// Code to execute if condition is true
} else {
// Code to execute if condition is false
}
1.3. else if Ladder
When there are multiple conditions, you can use the else if ladder to test each condition
sequentially.
Syntax:
if (condition1) {
// Code to execute if condition1 is true
} else if (condition2) {
// Code to execute if condition2 is true
} else {
// Code to execute if none of the conditions are true
}
1.4. Nested if Statement
An if statement can also be nested inside another if or else statement.
Syntax:
if (condition1) {
// Code to execute if condition1 is true
if (condition2) {
// Code to execute if condition2 is true
}
}
1.5. switch Statement
The switch statement allows you to execute one block of code among many based on the value
of an expression.
Syntax:
switch (expression) {
case constant1:
// Code to execute if expression equals constant1
break;
case constant2:
// Code to execute if expression equals constant2
break;
default:
// Code to execute if expression doesn't match any constant
}
2. Using Conditional (?:) Operator
The conditional operator is a shorthand way of writing an if-else statement. It is also known as
the ternary operator.
Syntax:
condition ? expression1 : expression2;
3. Key Points and Best Practices
• Always use curly braces {} to define blocks of code for if, else, else if, and switch cases,
even if the block contains a single statement. This improves readability and reduces the
chance of errors.
• When using the switch statement, don’t forget to use the break statement to prevent fall-
through.
• Use the conditional (?:) operator for simple conditions, but avoid using it for complex
logic, as it can reduce readability.
Program 1: Write a C program that reads a number from the user and checks if it is
positive, negative, or zero using the if-else statement.
Algorithm:
Step 1: Start
Step 2: Input the number
a. Prompt the user to input a number.
b. Read the input and store it in a variable (e.g., num).
Step 3: Check if the number is positive
a. Use the if statement to check if the number is greater than zero (num > 0).
b. If true, print "The number is positive."
Step 4: Check if the number is zero
a. Use the else if statement to check if the number is equal to zero (num == 0).
b. If true, print "The number is zero."
Step 5: Check if the number is negative
a. Use the else statement to handle the case where the number is less than zero.
b. If the previous conditions were false, the number must be negative. Print "The
number is negative."
Step 6: End
Program:
Output:
Program 2: Write a C program to find maximum of three numbers.
Algorithm:
Step1: Start

Step2: Read three numbers A, B, C

Step3: Find the larger number between A and B and store it in MAX_AB

Step4: Find the larger number between MAX_AB and C and store it in MAX

Step 5: Display MAX

Step6: Stop

Program:
Output:
Program 3: Create a C program that takes a number from 1 to 7 as input and displays the
corresponding day of the week (1 for Monday, 2 for Tuesday, etc.) using a switch-case
statement.
Algorithm:
Step 1: Start
Step 2: Declare an integer variable to store the day number (e.g., day).
Step 3: Prompt the user to enter a number between 1 and 7.
Step 4: Read the input from the user and store it in the variable day.
Step 5: Use a switch-case statement to check the value of the day:
a. Case 1: If day is 1, display "Monday".
b. Case 2: If day is 2, display "Tuesday".
c. Case 3: If day is 3, display "Wednesday".
d. Case 4: If day is 4, display "Thursday".
e. Case 5: If day is 5, display "Friday".
f. Case 6: If day is 6, display "Saturday".
g. Case 7: If day is 7, display "Sunday".
Step 6: Default case: If the input is not between 1 and 7, display an error message like
"Invalid input, please enter a number between 1 and 7."
Step 7: End
Program:
Output:
Program 4: Implement a C program to check if a given number is even or odd using the
ternary operator.
Algorithm:
Step 1: Start
Step 2: Declare an integer variable (e.g., number) to store the number to be checked.
Step 3: Prompt the user to enter an integer.
Step 4: Read the input from the user and store it in the variable number.
Step 5: Use the ternary operator to check if the number is even or odd:
a. Use the modulus operator (%) to check if number % 2 == 0.
b. If true (number % 2 == 0), the number is even.
c. If false (number % 2 != 0), the number is odd.
Step 6: Display the result ("even" or "odd") based on the ternary operator's evaluation.
Step 7: End
Program:
Output:

Criteria Marks
Preparation /20
Program /25
Interpretation of Result /20
Viva /10
Total /75
Faculty Signature with Date

Result: Thus, C programs using conditional statements were successfully implemented.

You might also like