0% found this document useful (0 votes)
2 views6 pages

Module03 Control Structures Solutions

The document contains a question bank for C programming focusing on control structures, specifically switch statements and if-else ladders. It includes theoretical questions, practical programming exercises, and example code snippets for various tasks such as checking Armstrong numbers, calculating factorials, and displaying patterns. The content is structured into questions Q21 to Q37, covering essential programming concepts and their implementations.

Uploaded by

riyubale
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)
2 views6 pages

Module03 Control Structures Solutions

The document contains a question bank for C programming focusing on control structures, specifically switch statements and if-else ladders. It includes theoretical questions, practical programming exercises, and example code snippets for various tasks such as checking Armstrong numbers, calculating factorials, and displaying patterns. The content is structured into questions Q21 to Q37, covering essential programming concepts and their implementations.

Uploaded by

riyubale
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 – Question Bank Solutions

MODULE 03 – Control Structures

■ Theory Questions

Q21. What is the purpose of a switch statement? How does it differ from if-else?
Purpose of switch statement:
A switch statement is a multi-way branch control structure that allows a variable to be tested for
equality against a list of values (called cases). It makes code cleaner and more readable when a single
variable must be compared against several constant values.

Differences between switch and if-else:


| Feature | switch | if-else |
|------------------|-------------------------------------|----------------------------------|
| Expression type | Integer or character only | Any boolean expression |
| Comparison | Only equality (==) | Any relational / logical expr |
| Ranges | Cannot check ranges | Can check ranges (e.g. x > 10) |
| Speed | Faster (jump table used by compiler)| Slightly slower for many checks |
| Default action | Uses 'default' label | Uses final 'else' block |
| Fall-through | Yes (without break) | No fall-through |
Syntax of switch:
switch (expression) {
case value1:
// statements
break;
case value2:
// statements
break;
default:
// default statements
}

Q22. Explain the need for the break keyword in a switch case.
The break keyword is essential in a switch statement to exit the switch block after a matching case is
executed. Without break, execution 'falls through' to the next case and continues running all subsequent
cases until the end of the switch block, regardless of whether they match.
Example demonstrating fall-through (without break):
int x = 2;
switch (x) {
case 1: printf("One\n");
case 2: printf("Two\n"); // matches here
case 3: printf("Three\n"); // also executes (fall-through)
default: printf("Default\n"); // also executes
}
// Output: Two Three Default (all 3 print due to missing break)
Correct usage (with break):
switch (x) {
case 1: printf("One\n"); break;
case 2: printf("Two\n"); break; // stops here
case 3: printf("Three\n"); break;
default: printf("Default\n");
}
// Output: Two (correct)

■ Program / Practical Questions

Q23. WAP to display the class of students according to range given (use if-else ladder).
#include <stdio.h>
int main() {
int marks;
printf("Enter marks (0-100): ");
scanf("%d", &marks);

if (marks >= 75)


printf("Distinction\n");
else if (marks >= 60)
printf("First Class\n");
else if (marks >= 50)
printf("Second Class\n");
else if (marks >= 40)
printf("Pass Class\n");
else
printf("Fail\n");

return 0;
}
/*
Output (marks = 72): First Class
Output (marks = 38): Fail
*/

Q24. WAP to check whether the entered number is an Armstrong number.


An Armstrong number (narcissistic number) is a number equal to the sum of its own digits each raised
to the power of the number of digits (e.g. 153 = 1³+5³+3³).
#include <stdio.h>
#include <math.h>
int main() {
int num, original, remainder, n = 0;
double result = 0;

printf("Enter an integer: ");


scanf("%d", &num);
original = num;

// Count digits
int temp = num;
while (temp != 0) { n++; temp /= 10; }

temp = num;
while (temp != 0) {
remainder = temp % 10;
result += pow(remainder, n);
temp /= 10;
}

if ((int)result == original)
printf("%d is an Armstrong number.\n", original);
else
printf("%d is NOT an Armstrong number.\n", original);

return 0;
}
/* Output: 153 is an Armstrong number. */

Q25. WAP to check whether the number entered is divisible by 10 or not.


#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);

if (num % 10 == 0)
printf("%d is divisible by 10.\n", num);
else
printf("%d is NOT divisible by 10.\n", num);

return 0;
}
/* Output: 50 is divisible by 10. */

Q26. WAP to find the number of days in a month using switch case.
#include <stdio.h>
int main() {
int month, year, days;
printf("Enter month (1-12): ");
scanf("%d", &month);
printf("Enter year: ");
scanf("%d", &year);

switch (month) {
case 1: case 3: case 5: case 7:
case 8: case 10: case 12:
days = 31; break;
case 4: case 6: case 9: case 11:
days = 30; break;
case 2:
// Leap year check
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
days = 29;
else
days = 28;
break;
default:
printf("Invalid month!\n");
return 1;
}
printf("Number of days in month %d of year %d = %d\n", month, year, days);
return 0;
}
/* Output (month=2, year=2024): Number of days = 29 */

Q27. Write a C program to print numbers from 1 to 10 using a for loop.


#include <stdio.h>
int main() {
int i;
printf("Numbers from 1 to 10:\n");
for (i = 1; i <= 10; i++) {
printf("%d ", i);
}
printf("\n");
return 0;
}
/* Output: 1 2 3 4 5 6 7 8 9 10 */

Q28. WAP to compute: (i) Square root (ii) Square (iii) Cube of a number.
#include <stdio.h>
#include <math.h>
int main() {
double num;
printf("Enter a number: ");
scanf("%lf", &num);

printf("Square Root : %.2f\n", sqrt(num));


printf("Square : %.2f\n", num * num);
printf("Cube : %.2f\n", num * num * num);

return 0;
}
/* Output (num=4): Square Root=2.00 Square=16.00 Cube=64.00 */

Q29. WAP to compute the factorial of a number.


#include <stdio.h>
int main() {
int n, i;
long long fact = 1;
printf("Enter a positive integer: ");
scanf("%d", &n);

if (n < 0)
printf("Factorial not defined for negative numbers.\n");
else {
for (i = 1; i <= n; i++)
fact *= i;
printf("Factorial of %d = %lld\n", n, fact);
}
return 0;
}
/* Output: Factorial of 5 = 120 */

Q30. WAP to compute prime factors of a given number.


#include <stdio.h>
int main() {
int n, i;
printf("Enter a positive integer: ");
scanf("%d", &n);

printf("Prime factors of %d: ", n);


// Divide out all 2s first
while (n % 2 == 0) {
printf("2 ");
n /= 2;
}
// Now check odd factors from 3
for (i = 3; i * i <= n; i += 2) {
while (n % i == 0) {
printf("%d ", i);
n /= i;
}
}
if (n > 2)
printf("%d", n);
printf("\n");
return 0;
}
/* Output (n=60): Prime factors of 60: 2 2 3 5 */

Q31. WAP to check whether the entered number is a palindrome or not.


A palindrome number reads the same forwards and backwards (e.g. 121, 1331).
#include <stdio.h>
int main() {
int num, original, reversed = 0, remainder;
printf("Enter an integer: ");
scanf("%d", &num);
original = num;

while (num != 0) {
remainder = num % 10;
reversed = reversed * 10 + remainder;
num /= 10;
}

if (original == reversed)
printf("%d is a Palindrome.\n", original);
else
printf("%d is NOT a Palindrome.\n", original);

return 0;
}
/* Output: 121 is a Palindrome. */

Q32. WAP to find the LCM and GCD of given numbers.


#include <stdio.h>
int main() {
int a, b, x, y, gcd, lcm;
printf("Enter two integers: ");
scanf("%d %d", &a, &b);
x = a; y = b;

// Euclidean algorithm for GCD


while (y != 0) {
int temp = y;
y = x % y;
x = temp;
}
gcd = x;
lcm = (a / gcd) * b; // avoids overflow

printf("GCD of %d and %d = %d\n", a, b, gcd);


printf("LCM of %d and %d = %d\n", a, b, lcm);
return 0;
}
/* Output (12, 18): GCD = 6 LCM = 36 */

Q33. Write a program to perform addition, subtraction, and modulus using switch case.
#include <stdio.h>
int main() {
int a, b, choice;
printf("Enter two integers: ");
scanf("%d %d", &a, &b);
printf("1. Addition\n2. Subtraction\n3. Modulus\n");
printf("Enter choice (1-3): ");
scanf("%d", &choice);

switch (choice) {
case 1: printf("Addition = %d\n", a + b); break;
case 2: printf("Subtraction = %d\n", a - b); break;
case 3:
if (b != 0)
printf("Modulus = %d\n", a % b);
else
printf("Modulus by zero is undefined.\n");
break;
default: printf("Invalid choice!\n");
}
return 0;
}
/* Output (a=10, b=3, choice=3): Modulus = 1 */

Q34. Write a program to display the pattern: 12345 / 1234 / 123 / 12 / 1


#include <stdio.h>
int main() {
int i, j;
for (i = 5; i >= 1; i--) {
for (j = 1; j <= i; j++)
printf("%d", j);
printf("\n");
}
return 0;
}
/*
Output:
12345
1234
123
12
1
*/

Q35. Write a program to print the star triangle pattern.


#include <stdio.h>
int main() {
int i, j;
for (i = 1; i <= 5; i++) {
for (j = 1; j <= i; j++)
printf("* ");
printf("\n");
}
return 0;
}
/*
Output:
*
* *
* * *
* * * *
* * * * *
*/

Q36. Write a C program to print the pattern: 5 4 3 2 1 / 5 4 3 2 / ...


#include <stdio.h>
int main() {
int i, j;
for (i = 5; i >= 1; i--) {
for (j = 5; j >= (5 - i + 1); j--)
printf("%d ", j);
printf("\n");
}
return 0;
}
/*
Output:
5 4 3 2 1
5 4 3 2
5 4 3
5 4
5
*/
Q37. Write a program to print the pattern: 1 / 1 2 / 1 2 3
#include <stdio.h>
int main() {
int i, j;
for (i = 1; i <= 5; i++) {
for (j = 1; j <= i; j++)
printf("%d ", j);
printf("\n");
}
return 0;
}
/*
Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
*/

CP Question Bank – Module 03 Solutions | Questions Q21–Q37 | Total: 17 Questions

You might also like