0% found this document useful (0 votes)
10 views2 pages

C Programming Control Structures Exercises

This document is a C programming practice sheet focused on control structures, including sequential statements, branching statements, and looping statements. It contains various exercises such as calculating sums, determining grades, implementing a calculator, and using loops for number printing and factorial calculations. Additionally, it includes tasks on nested loops and loop interruption techniques.

Uploaded by

biplaptripathi13
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

C Programming Control Structures Exercises

This document is a C programming practice sheet focused on control structures, including sequential statements, branching statements, and looping statements. It contains various exercises such as calculating sums, determining grades, implementing a calculator, and using loops for number printing and factorial calculations. Additionally, it includes tasks on nested loops and loop interruption techniques.

Uploaded by

biplaptripathi13
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

C Programming Practice Sheet - Chapter 5: Control Structures

Sequential Statement

1. Write a C program to input two numbers and display their sum, difference, and product.

Branching Statements

2. Write a program to input a number and print "Positive" if it is greater than zero.

3. Input a number and check whether it is even or odd.

4. Input marks of a student and print grade based on:

- 90 and above: A

- 80-89: B

- 70-79: C

- Below 70: Fail

5. Input a number and print whether it is Positive, Negative or Zero using else-if ladder.

6. Write a calculator program using switch that performs +, -, *, / based on user's choice.

7. Write a program that uses goto to jump to a label when divide by zero is detected.

Looping Statements - Types

8. Print numbers from 1 to 10 using for, while, and do...while loops.

Looping Statements - Categories

9. Use a while loop (entry-controlled) to print first 5 natural numbers.

10. Use a do...while loop (exit-controlled) to print numbers from 1 to 5.

11. Input a number n and calculate factorial using a for loop (counter-controlled).

12. Accept numbers until user enters -1, then print the sum (sentinel-controlled).

Loop Nesting

13. Print the following pattern using nested loops:

**
C Programming Practice Sheet - Chapter 5: Control Structures

***

****

Loop Interruption

14. Print numbers from 1 to 10, skip 5 using continue.

15. Print numbers from 1 to 10, stop when 7 is reached using break.

Common questions

Powered by AI

In C, a 'for' loop is often used when the number of iterations is known beforehand, as it initializes, checks the condition, and increments in a single line. A 'while' loop is preferred when the iterations depend on a condition evaluated prior to the loop body, while a 'do...while' loop guarantees the loop body executes at least once since the condition is checked after execution. This makes 'do...while' suitable for scenarios where the loop must run regardless of the initial condition .

A C program can determine whether an integer is positive, negative, or zero using an else-if ladder. The program should first check if the number is greater than zero, if so, it is positive. If not, it should check if it is less than zero, which would make it negative. If neither condition is true, the number is zero .

A C program can dynamically adjust arithmetic operations based on user input by using a switch statement. The user specifies an operation (e.g., '+', '-', '*', '/') through input, and the program uses the switch statement to select and execute the corresponding case block that performs the desired arithmetic operation .

Using 'goto' for handling errors like division by zero can simplify the control flow in some cases by allowing the program to jump directly to handling code, thereby avoiding deeply nested if-else statements. However, it can lead to less readable code and makes debugging more difficult, as the program's flow is non-linear and harder to follow .

A 'switch' statement simplifies the creation of command-driven C programs by allowing multiple branches of execution depending on user input. It is particularly useful for menu-driven applications where the action depends on a user's choice. However, its limitation lies in its restriction to discrete values and the absence of fall-through prevention, making it unsuitable for ranges or complex condition checks, which might better be handled with if-else chains .

Control flow in C loops can be efficiently managed using 'continue' and 'break'. 'Continue' skips the remainder of the loop body and continues with the next iteration, which is ideal for skipping certain loop cycles (e.g., skipping a number). 'Break' exits the loop completely when a condition is met; it is useful for stopping execution early when further iteration is unnecessary or undesired .

A C program ensures efficiency and correctness in calculations by using a combination of conditional and iterative statements. Conditional structures (if-else) decide which operations to perform based on inputs, while loops manage repetitive calculations, such as calculating a factorial with a for loop. This ensures precise control over logic flow and resource use, enhancing both performance and accuracy .

To assign grades based on numeric input in C, a program can use a series of if-else if statements. The program should first check if the marks are 90 or above for a grade 'A'. Then, if not, it checks if marks are between 80-89 for a 'B', 70-79 for a 'C', and assign 'Fail' for any value below 70. The else-if ladder ensures proper hierarchical evaluation .

Nested loops allow for iterating over multi-dimensional data and are effective for tasks like generating patterns or matrices, where operations need to be repeated within other operations. For example, printing a pattern with varying row decrements requires a loop for each row and an inner loop for the columns. This automation mimics natural incrementality and hierarchy, but it can increase complexity and reduce readability if overused or poorly structured .

Sentinel-controlled loops in C continue executing until a special value, known as a sentinel, is encountered. This special value signals the end of loop execution. They are particularly effective in scenarios where data is input until a user decides to stop, such as reading numbers until '-1' is entered to calculate their sum. This approach dynamically handles varying data sizes and conditions without predefined loop counts .

You might also like