Docsity

Python Notes: Clear Concepts + Practical Examples, Study notes of Web Application Development

Toggle sidebar
bg1
Module 3
PROGRAMMING IN C
Q1. 2 MARKS QUESTIONS.
1.
What is the purpose of if statement in C.
➢
Purpose of if statement
is
as
follows:
• The if statement is used for decision-making in a program.
• It checks a condition, and if that condition is true, a specific block of code
is executed.
• If the condition is false, the program skips the block and continues
normally.
• It helps the program control the flow of execution based on logic.
Example: if (marks >= 40) printf("Pass"); - executes only if marks are 40 or
more.
2.
What does the switch statement do in C?
➢ Need for Algorithm is as follows:
• The switch statement is used when a variable can take multiple
constant values.
• It compares the value of an expression against a list of case labels.
• The program executes the statements under the matching case.
• It makes multi-way decision-making simpler and cleaner than many if-
else statements.
Example: used to print the day name based on day number (1 = Monday, 2
= Tuesday, etc.).
3.
What is the purpose of break statement in a switch case?
➢ Purpose of break statement is as follows:
• The break statement is used to stop execution of a particular case inside a switch.
• Without break, all cases after the matching one would also execute (called fall-
through).
• It helps to exit the switch block immediately after a case is executed.
• It ensures that only one case runs and prevents unnecessary outputs.
Example: switch(num) { case 1: printf("One"); break; }
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19

Partial preview of the text

Download Python Notes: Clear Concepts + Practical Examples and more Study notes Web Application Development in PDF only on Docsity!

PROGRAMMING IN C

Q1. 2 MARKS QUESTIONS.

  1. What is the purpose of if statement in C. ➢ Purpose of if statement is as follows:
    • The if statement is used for decision-making in a program.
    • It checks a condition , and if that condition is true, a specific block of code is executed.
    • If the condition is false, the program skips the block and continues normally.
    • It helps the program control the flow of execution based on logic. Example: if (marks >= 40) printf("Pass"); - executes only if marks are 40 or more.
  2. What does the switch statement do in C? ➢ Need for Algorithm is as follows:
    • The switch statement is used when a variable can take multiple constant values.
    • It compares the value of an expression against a list of case labels.
    • The program executes the statements under the matching case.
    • It makes multi-way decision-making simpler and cleaner than many if- else statements. Example: used to print the day name based on day number (1 = Monday, 2 = Tuesday, etc.).
  3. What is the purpose of break statement in a switch case? ➢ Purpose of break statement is as follows:
    • The break statement is used to stop execution of a particular case inside a switch.
    • Without break, all cases after the matching one would also execute (called fall- through).
    • It helps to exit the switch block immediately after a case is executed.
    • It ensures that only one case runs and prevents unnecessary outputs. Example: switch(num) { case 1: printf("One"); break; }

PROGRAMMING IN C

  1. What is the difference between switch and if-else - else-if statement in C.
  2. Differentiate between if and if-else statement.
  3. What does the default case in a switch case statement do? ➢ The default case in a switch case statement is used for the following:
    • The default case is used when none of the case labels match the given value.
    • It acts as a fallback option , just like an else in an if-else structure.
    • It is optional but recommended to handle unexpected inputs.
  • The default case can appear anywhere in the switch block but is usually placed last. Example: switch(day) { default: printf("Invalid day"); }

PROGRAMMING IN C

Q2. 5 MARKS QUESTIONS.

  1. Explain how the switch statement works in C. Provide an example of using a switch statement to find the grade based on a score. ➢ The switch statement works in the following ways: The switch statement in C is a multi-way decision-making control structure. It allows the program to select one block of code from multiple options based on the value of an expression. The expression inside the switch() is evaluated once, and its result is compared with each case label. When a match is found, the corresponding statements are executed until a break statement is encountered. If no case matches, the default case (if provided) gets executed. The switch is often used when a variable can take a limited number of known constant values — for example, to create menus, grading systems, or day-name programs. Algorithm to find the grade based on a score: Step 1: Start Step 2: Declare a variable grade or score. Step 3: Input a grade value (A, B, C, D). Step 4 : Use the switch statement to check each grade. Step 5 : Display the message according to the grade. Step 6: Use default to handle invalid grades. Step 7: Stop.

PROGRAMMING IN C

Code:- #include #include int main() { char grade; clrscr(); printf("Enter your grade (A/B/C/D): "); scanf("%c", &grade); switch(grade) { case 'A': printf("Excellent!"); break; case 'B': printf("Good!"); break; case 'C': printf("Average!"); break; case 'D': printf("Needs Improvement!"); break; default: printf("Invalid Grade!"); } getch(); return 0; } Output:- Enter your grade (A/B/C/D): B Good!

PROGRAMMING IN C

In conclusion, the else-if statement is one of the most useful control structures in C programming. It provides a clean and efficient way to perform multi-way decision- making in a program. By checking conditions in sequence and executing only the matching block, it makes the program logical, simple, and easy to debug. The else-if ladder therefore plays a key role in writing programs that can choose between several options based on specific criteria.

PROGRAMMING IN C

  1. What is a switch block, explain in detail with one simple example. ➢ In C programming, a switch block is a type of multi-way decision control structure that allows the programmer to select one block of code to execute from multiple available options. It is used when we need to perform different actions based on the value of a single variable or expression. The switch block provides a cleaner and more readable alternative to using multiple if-else-if statements when there are several constant conditions to check. The switch statement starts with the keyword switch, followed by an expression inside parentheses. The expression inside the switch must result in an integer or character value. The switch block then contains several case labels , each representing a possible value of that expression. When the program runs, the value of the expression is compared with each case label one by one. If a match is found, the statements under that case are executed. Each case in the switch block usually ends with a break statement , which tells the compiler to exit the switch after executing that particular block. Without the break statement, the program will continue to execute all the cases below the matched one — a situation known as fall-through. Therefore, using break is important to prevent unwanted execution of other cases. If none of the case labels match the value of the expression, the default case is executed. The default case acts as a catch-all or backup option and is similar to the else part in an if-else statement. The default case is optional, but it is generally recommended to handle unexpected or invalid input values. The general structure of a switch block is as follows: switch(expression){ case value1: statements; break; case value2: statements; break; default: statement; } Here, the expression is evaluated first. Then, depending on its result, the matching case executes. The program then exits the switch when a break statement is encountered.

PROGRAMMING IN C

  1. Write a program using if-else statement to check if number is divisible by 2. ➢ Algorithm to Check if a Number is divisible by 2: Step 1: Start Step 2: Declare Variable Num. Step 3: Input a number from the user. Step 4: Use an if-else statement to check if the number is divisible by 2. - If num % 2 == 0, display “Number is divisible by 2”. - Else, display “Number is not divisible by 2”. Step 5 : Stop C Program: #include #include int main() { int num; clrscr(); printf("Enter a number: "); scanf("%d", &num); if (num % 2 == 0) printf("The number is divisible by 2"); else printf("The number is not divisible by 2"); getch(); return 0; } Output Example: Enter a number: 8 The number is divisible by 2 Explanation: The program checks if the remainder of the number divided by 2 is zero. If the remainder is 0, it prints that the number is divisible by 2; otherwise, it is not.

PROGRAMMING IN C

  1. Write a program to check if number is positive, negative or zero using if-else statement. ➢ Algorithm to Check if a Number is positive, negative or zero: Step 1: Start Step 2: Declare Variable Num. Step 3: Input a number from the user. Step 4: Use an if-else structure to test the number:
  • If num > 0, display “Number is Positive”.
  • Else if num < 0, display “Number is Negative”.
  • Else, display “Number is Zero”. Step 5 : Stop C Program: #include #include int main() { int num; clrscr(); printf("Enter a number: "); scanf("%d", &num); if (num > 0) printf("The number is Positive"); else if (num < 0) printf("The number is Negative"); else printf("The number is Zero"); getch(); return 0; } Output Example: Enter a number: 25 The number is Positive Explanation: The program uses if-else statements to check the sign of the entered number. Depending on whether the number is greater than, less than, or equal to zero, it displays the corresponding message.

PROGRAMMING IN C

  1. Write a program to determine whether a number is divisible by both 2 and 5. ➢ Algorithm to determine whether the number is divisible by 2 and 5 both: Step 1: Start Step 2: Declare variables a, b, and c. Step 3: Input three number from the user. Step 4: Use an if-else statement to check divisibility:
  • If num % 2 == 0 and num % 5 == 0, display “Number is divisible by both 2 and 5”.
  • Else, display “Number is not divisible by both 2 and 5”. Step 5 : Stop C Program: #include #include int main() { int num; clrscr(); printf("Enter a number: "); scanf("%d", &num); if (num % 2 == 0 && num % 5 == 0) printf("The number is divisible by both 2 and 5"); else printf("The number is not divisible by both 2 and 5"); getch(); return 0; } Output Example: Enter a number: 20 The number is divisible by both 2 and 5 Explanation: The program uses the modulus operator (%) to check divisibility. If the number gives a remainder 0 when divided by both 2 and 5, it prints that it is divisible by both; otherwise, it is not.

PROGRAMMING IN C

Q3. 10 MARKS QUESTIONS.

  1. Explain the differences between switch and if-else statements in terms of performance, readability and use-cases. Provide an example for each. ➢ An Introduction: In C programming, both the switch and if-else statements are used for decision-making and controlling the flow of a program. They allow the program to choose between different blocks of code based on certain conditions. However, they differ in their structure, performance, readability, and best use-cases. The if-else statement is used for testing multiple conditions or ranges, while the switch statement is used when comparing a single variable to multiple constant values.
    1. if-else Statement: The if-else structure evaluates logical or relational expressions. It checks each condition in order from top to bottom and executes the block where the condition is true. If no condition is true, the else block (if present) executes as a default case. It is ideal when conditions involve comparisons, ranges, or complex logic such as (a > b && a < c) or (x % 2 == 0). Syntax of if-else: if (condition1) statement1; else if (condition2) statement2; else statement3;

PROGRAMMING IN C

Example of switch Statement: #include #include int main() { int day; clrscr(); printf("Enter day number (1–7): "); scanf("%d", &day); switch(day) { case 1: printf("Monday"); break; case 2: printf("Tuesday"); break; case 3: printf("Wednesday"); break; case 4: printf("Thursday"); break; case 5: printf("Friday"); break; case 6: printf("Saturday"); break; case 7: printf("Sunday"); break; default: printf("Invalid day"); } getch(); return 0; } Output Example: Enter day number (1–7): 4 Thursday Explanation: The value of day is compared with each case label, and only the matching case executes. This structure is cleaner than multiple if-else statements when checking fixed values.

PROGRAMMING IN C

  1. Create a program that uses a switch case statement to display the corresponding season based in the month number (1-3: Winter, 4-6: Spring, etc.). ➢ In C programming, a switch case statement is a multi-way decision making structure used to execute one block of code among many based on the value of an expression. It compares the value of a variable (like month number) with different case labels. When a match is found, the statements under that case are executed until a break statement is reached. If no case matches, the default case executes. It is most useful when there are multiple fixed choices for a single variable — such as menu options, days, or months. Algorithm: Step 1: Start Step 2: Declare variable month. Step 3: Input month number (1–12) from the user. Step 4: Use a switch statement to determine the season:
  • For 1–3 → Display “Winter”
  • For 4–6 → Display “Spring”
  • For 7–9 → Display “Summer”
  • For 10–12 → Display “Autumn” Step 5: If none of these match, display “Invalid Month”. Step 6: Stop

PROGRAMMING IN C

  1. Write a program that takes a year as input and uses nested if-else statement to determine if it’s a leap year or not. ➢ A nested if-else statement means placing one if-else structure inside another. It is used when a decision depends on multiple conditions that must be tested one after another. In this program, the nested if-else structure is used to check whether a given year is a leap year or not. A leap year occurs:
  • If the year is divisible by 400 , or
  • If the year is divisible by 4 but not divisible by 100. Using nested if-else allows us to check these conditions step by step in a logical order. Algorithm: Step 1: Start Step 2: Declare variable year. Step 3: Input the year from the user. Step 4: Check if the year is divisible by 4.
  • If true, check if it is divisible by 100.
  • If true, check if it is divisible by 400.
  • If true → It is a Leap Year.
  • Else → Not a Leap Year.
  • Else → It is a Leap Year.
  • Else → Not a Leap Year. Step 5: Stop C Program: #include #include int main() { int year; clrscr(); printf("Enter a year: "); scanf("%d", &year); if (year % 4 == 0) { if (year % 100 == 0) { if (year % 400 == 0) printf("%d is a Leap Year", year); else printf("%d is Not a Leap Year", year); } else { printf("%d is a Leap Year", year); } } else { printf("%d is Not a Leap Year", year); } getch(); return 0; }

PROGRAMMING IN C

Output Example: Enter a year: 2024 2024 is a Leap Year Explanation: The program checks the leap year condition using nested if-else statements.

  • If the year is divisible by 400, it is a leap year.
  • If divisible by 100 but not 400, it is not a leap year.
  • If divisible by 4 but not 100, it is a leap year.
  • Otherwise, it is not a leap year. The nested structure ensures conditions are tested in proper order to get an accurate result.