5 Control Statement

C Programming · Unit 5 · 4 hrs

Control Statement

Exam-focused notes for Control Statement (C Programming, CSC115): what the TU syllabus asks and how it has actually been tested, with 15 solved past questions from this unit.

What this unit covers

  • Conditional Statements
  • Decision Making and Branching
  • Decision Making and Looping
  • Exit function
  • Break and Continue

Decision Making and Looping

20815 marks

Write a program to display first 50 prime numbers. [5]

- Required output: first 50 prime numbers - Marks: 5 A prime number is an integer greater than 1 whose only positive divisors are 1 and itself. To generate the first 50 primes, we test successive integers starting from 2, counting each prime until 50 are fo...

Full solved answer →
20815 marks

Write a program to display the following series up to 25 terms but do not print the 7th term. 2×3, 3×5, 4×7, 5×9... [5]

- Series: $2\times3,\; 3\times5,\; 4\times7,\; 5\times9,\ldots$ - Number of terms: 25 - Condition: Do not print the 7th term Term ($i$) Expression First Factor Second Factor ---------------------------------------------------- 1 $2\times3$ 2 3 2 $3\times5$ ...

Full solved answer →
20805 marks

Trace the output

#include <conio.h>
#include <stdio.h>
void main() {
int i = 0, k;
for (k = 5; k >= 0; k--) {
i = i + k;
}
printf("%d\t", i);
getch();
}

[5]

Initial value: $i = 0$ --- The loop starts at $k = 5$, decrements by 1 each pass, and continues while $k \geq 0$. Iteration $k$ Condition $k \geq 0$ $i = i + k$ New $i$ ------------------------------------------------------------ 1 5 True $0 + 5$ 5 2 4 True...

Full solved answer →
207910 marks

Write a program to demonstrate the following menu-driven program. The user will provide an integer and alphabet for making choice and the corresponding task has to be performed according as follow:A. Find Odd or Even B. Find Positive or Negative C. Find the Factorial value D. Exit The choice will be displayed until the user will give 'D' as a choice.[10]

--- --- --- The menu is placed inside a do-while loop so that: - The menu is displayed at least once. - The loop continues repeating until the user enters 'D' (Exit). Each menu option is handled by a separate case block: Choice Task Performed --------------...

Full solved answer →
207710 marks

What do you mean by looping?Explain while loop with suitable example.Compare while loop with do while loop.Write a program to find sum and average of first n natural numbers.[1+3+2+4]

--- Looping (also called iteration) is a programming concept that allows a set of statements to be executed repeatedly as long as a given condition remains true. Instead of writing the same code multiple times, a loop executes it automatically for the requi...

Full solved answer →
20775 marks

Write a program to display first n prime numbers. [5]

A prime number is a number greater than 1 that has no divisors other than 1 and itself. To display the first N prime numbers, we use a nested loop: - The outer loop counts how many primes have been found so far. - The inner loop checks whether each candidat...

Full solved answer →
207510 marks

What is looping statement?Discuss different looping statements with suitable example of each.[2+8]

A looping statement (also called an iterative statement) is a control structure that allows a block of statements to be executed repeatedly as long as a given condition remains true. Looping avoids writing the same code multiple times and is used when a tas...

Full solved answer →
20755 marks

Write a program to calculate sum of first 10 odd numbers. [5]

The first 10 odd numbers are: 1, 3, 5, 7, 9, 11, 13, 15, 17, 19 We use a for loop to iterate through numbers and check whether each number is odd using the condition num % 2 != 0. We keep a count of how many odd numbers have been found and add them to the s...

Full solved answer →
20745 marks

Write a program that computes the sum of digits of a given integer number. [5]

To find the sum of digits of an integer, we repeatedly extract the last digit using the modulus operator (% 10), add it to a sum variable, and then remove the last digit using integer division (/ 10). This continues until the number becomes 0. Example: For ...

Full solved answer →

Exit function

207910 marks

What is the difference between exit() and exit(1)? Discuss the need of nested structure with an example. Write a program to find the value of x^y without using POW code.[10]

--- The exit() function is defined in <stdlib.h and is used to terminate a program immediately from any point in the code, including from inside functions or loops. Feature exit(0) exit(1) --------- Meaning Normal / successful termination Abnormal / error t...

Full solved answer →

Break and Continue

207810 marks

What do you mean by jump statement?Explain each jump statement with example.Write a program to check whether a number entered is prime or not.[1+5+4]

A jump statement is a control statement that transfers the flow of program execution unconditionally from one part of the program to another. Jump statements interrupt the normal sequential or loop-based execution and cause the program to "jump" to a differ...

Full solved answer →
20755 marks

What is break statement ? Discuss with example. How the break statement is different from continue statement? [1+0.5+3.5]

--- The break statement is a jump statement used inside loops (for, while, do...while) and switch statements. When a break statement is encountered, the execution of the entire loop or switch case is immediately terminated, and the control is transferred to...

Full solved answer →

Decision Making and Branching

20785 marks

Write a program to print largest among three number entered by the user. [5]

The logic uses nested if-else statements to compare three numbers entered by the user and determine the largest one. --- --- Condition Result ------------------- a b and a c a is the largest a b but a <= c c is the largest a <= b and b c b is the largest a ...

Full solved answer →

Conditional Statements

20755 marks

Write a program to check whether a number entered is even or odd. [5]

When a number is divided by 2: - If the remainder is 0, the number is even - If the remainder is not 0, the number is odd The modulus operator % is used to find the remainder. --- --- Line Description ------------------- include<stdio.h Header file for inpu...

Full solved answer →
207410 marks

Discuss different types of if statements with suitable example of each.Different if statement with switch statement.[8+2]

(a) Types of if Statements Selection statements (if, if...else, switch) are a category of statements in C that control the flow of execution based on conditions. --- Syntax: Description: If the condition evaluates to true (non-zero), the block of statements...

Full solved answer →