Q.
1) In C programming, decision-making statements are used to execute specific
blocks of code based on certain conditions. These statements help the program
take different actions depending on the outcome of logical expressions.
1) if Statement
2) if-else Statement
3) else if Ladder
4) Nested if Statements
5)Conditional Operator (?:)
Q.2) #include <stdio.h>
int main() {
int number, absolute;
// Input from the user
printf("Enter an integer: ");
scanf("%d", &number);
// Calculate absolute value
if (number < 0) {
absolute = -number; // Convert negative number to positive
} else {
absolute = number; // If already positive or zero, no change
// Output the result
printf("The absolute value of %d is %d\n", number, absolute);
return 0;
}
Q.3 #include <stdio.h>
int main() {
int number;
// Input from the user
printf("Enter an integer: ");
scanf("%d", &number);
// Check if the number is positive, negative, or zero
if (number > 0) {
printf("The number %d is positive.\n", number);
} else if (number < 0) {
printf("The number %d is negative.\n", number);
} else {
printf("The number is zero.\n");
return 0;
Q.4 #include <stdio.h>
int main() {
int num1, num2, num3;
// Input three numbers
printf("Enter three integers: ");
scanf("%d %d %d", &num1, &num2, &num3);
// Using if-else ladder and && operator
if (num1 >= num2 && num1 >= num3) {
printf("The greatest number is: %d\n", num1);
} else if (num2 >= num1 && num2 >= num3) {
printf("The greatest number is: %d\n", num2);
} else {
printf("The greatest number is: %d\n", num3);
return 0;
Q.5) The switch statement in C is a decision-making statement used to execute
one block of code among multiple options based on the value of a single variable
or expression. It is an alternative to using a series of if-else statements when
working with a variable that can take multiple discrete values.
Q.6 #include <stdio.h>
int main() {
int day;
// Input from the user
printf("Enter a number (1-7): ");
scanf("%d", &day);
// Switch case to determine the day of the week
switch (day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
case 4:
printf("Thursday\n");
break;
case 5:
printf("Friday\n");
break;
case 6:
printf("Saturday\n");
break;
case 7:
printf("Sunday\n");
break;
default:
printf("Invalid input! Please enter a number between 1 and 7.\n");
return 0;
}
Q.7) 1) for loop: for (initialization; condition; increment/decrement) {
// Code to execute in the loop
2)while loop: while (condition) {
// Code to execute in the loop
3)do while loop:
do {
// Code to execute in the loop
} while (condition);
4)infinite loops: for (;;) {
// Infinite loop code
Q.8) #include <stdio.h>
int main() {
int num;
// Input from the user
printf("Enter a number to print its table: ");
scanf("%d", &num);
// Print the multiplication table using a for loop
printf("The table of %d is:\n", num);
for (int i = 1; i <= 10; i++) {
printf("%d ", num * i); // Print each multiple separated by space
printf("\n"); // Newline after printing the table
return 0;
}
Unit 4-3
Q.1) A function in C is a self-contained block of code that performs a specific task.
Functions are used to organize code, reduce redundancy, improve readability, and
facilitate debugging. C programs must have at least one function, which is the
main() function, but you can define additional user-defined functions as needed.
Syntax : return_type function_name(parameter_list) {
// Function body: code to perform a specific task
return value; // Optional: Only if the function has a return type
Q.2) #include <stdio.h>
#include <math.h> // Required for sqrt() function
// Function declaration
double calculateSquareRoot(double num);
int main() {
double number, result;
// Input from the user
printf("Enter a number to find its square root: ");
scanf("%lf", &number);
// Check if the number is non-negative
if (number < 0) {
printf("Error! Square root of a negative number is not real.\n");
} else {
// Function call to calculate square root
result = calculateSquareRoot(number);
// Output the result
printf("The square root of %.2lf is %.2lf\n", number, result);
return 0;
// Function definition to calculate square root
double calculateSquareRoot(double num) {
return sqrt(num); // Using the sqrt() function from math.h
Q.3) #include <stdio.h>
// Function declaration
void swap(int *a, int *b);
int main() {
int num1, num2;
// Input from the user
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
// Before swapping
printf("Before swapping: num1 = %d, num2 = %d\n", num1, num2);
// Function call to swap the numbers
swap(&num1, &num2);
// After swapping
printf("After swapping: num1 = %d, num2 = %d\n", num1, num2);
return 0;
// Function definition to swap two numbers using pointers
void swap(int *a, int *b) {
int temp;
temp = *a; // Store the value of a in temp
*a = *b; // Assign the value of b to a
*b = temp; // Assign the value of temp (original a) to b
Q.4) #include <stdio.h>
// Function declaration
int findMax(int a, int b);
int main() {
int num1, num2, maxValue;
// Input from the user
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
// Function call to find the maximum value
maxValue = findMax(num1, num2);
// Output the result
printf("The maximum value between %d and %d is: %d\n", num1, num2,
maxValue);
return 0;
// Function definition to find the maximum of two numbers
int findMax(int a, int b) {
if (a > b) {
return a; // If a is greater than b, return a
} else {
return b; // Otherwise, return b
Q.5) #include <stdio.h>
// Function declaration
int add(int a, int b);
int main() {
int num1, num2, sum;
// Input from the user
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
// Function call to add the two numbers
sum = add(num1, num2);
// Output the result
printf("The sum of %d and %d is: %d\n", num1, num2, sum);
return 0;
// Function definition to add two numbers
int add(int a, int b) {
return a + b; // Return the sum of a and b
Q.6) #include <stdio.h>
// Function declaration
int square(int n);
int main() {
int num, result;
// Input from the user
printf("Enter a number: ");
scanf("%d", &num);
// Function call to calculate the square of the number
result = square(num);
// Output the result
printf("The square of %d is: %d\n", num, result);
return 0;
// Function definition to calculate square using recursion
int square(int n) {
// Base case: when n is 0 or 1
if (n == 0) {
return 0;
} else if (n == 1) {
return 1;
} else {
// Recursive case: n * n is computed by repeated addition
return n + square(n - 1) + square(n - 1);
}\
Q.8)