C Paper Makaut
C Paper Makaut
TECH(N)/EVEN/SEM-2/2008/2024-2025/
MAULANA ABUL KALAM AZAD UNIVERSITY OF TECHNOLOGY, WEST BENGAL
Paper Code : ESCS201 Programming for Problem Solving
UPID : 002008
Used for selecting one block of code to execute from multiple choices based on the value of a
single variable or expression.
It checks for equality against constant values (cases).
Generally more efficient than nested-if when dealing with multiple discrete choices.
Uses break to prevent fall-through to the next case.
Has an optional default case for handling values not explicitly matched.
Nested-If Statement:
2/9
Example:
#include
int main() {
int choice = 2;
// Switch statement example
switch (choice) {
case 1:
printf("Choice 1 selected\n");
break;
case 2:
printf("Choice 2 selected\n");
break;
case 3:
printf("Choice 3 selected\n");
break;
default:
printf("Invalid choice\n");
}
int x = 15;
// Nested-if statement example
if (x > 10) {
if (x < 20) {
printf("x is between 10 and 20\n");
if(x%2 ==0){
printf("x is even\n");
} else {
printf("x is odd\n");
}
} else {
printf("x is greater than or equal to 20\n");
}
} else {
printf("x is less than or equal to 10\n");
}
return 0;
}
3. Write a C program to count the number of even and odd numbers from an 1-D array. [5]
Ans:- int main() {
int arr[100], n, i;
int even = 0, odd = 0;
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Enter the elements:");
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
for(i = 0; i < n; i++) {
if(arr[i] % 2 == 0)
even++;
else
odd++;
}
printf("Number of even numbers: %d\n", even);
printf("Number of odd numbers: %d\n", odd);
return 0;
}
Note: The necessary header files must be included.
3/9
Any other correct program should be given credit. Part marking to be done for partially correct
answer.
4. Write a program in C to find the area and circumference of a circle using function [5]
Ans:- #define PI 3.14159
float calcArea(float radius) {
return PI * radius * radius;
}
float calcCircum(float radius) {
return 2 * PI * radius;
}
int main() {
float radius, area, circum;
printf("Enter the radius of the circle: ");
scanf("%f", &radius);
area = calcArea(radius);
circum = calcCircum(radius);
printf("Area of the circle = %f\n", area);
printf("Circumference of the circle = %f\n", circum);
return 0;
}
Output
Enter an integer: 5
Enter number of positions to left shift: 2
Original number = 5
After left shifting by 2 positions = 20
4/9
Note: Necessary header files must be included.
Any other correct program should be given credit. Part marking to be done for partially correct
answer.
8. (a) Define a recursive function to calculate factorial of a number and call this function to calculate [8]
S = 1! + 2! + ...n! ,where n is user input.
Ans:- long factorial(int num) {
if (num == 0 || num == 1)
return 1;
else
return num * factorial(num - 1);
}
int main() {
int n, i;
long sum = 0;
printf("Enter a positive integer n: ");
scanf("%d", &n);
if(n < 1) {
printf("Please enter a positive integer greater than 0.\n");
return 0;
}
// Calculate S = 1! + 2! + ... + n!
for(i = 1; i <= n; i++) {
sum += factorial(i);
}
5/9
printf("Sum of factorials: %ld\n", sum);
return 0;
}
6/9
printf("\n");
return 0;
}
Example:
7/9
}
}
if (isPalindrome)
printf("The string is a palindrome.\n");
else
printf("The string is not a palindrome.\n");
return 0;
}
9/9