Be - First Year Engineering - Semester 2 - 2023 - May - C Programming Rev 2019c Scheme
Be - First Year Engineering - Semester 2 - 2023 - May - C Programming Rev 2019c Scheme
int main() {
int result;
MUQuestionPapers.com
// Calling the function before its definition
result = add(3, 5);
printf("Result: %d\n", result);
return 0;
}
// Function definition
int add(int num1, int num2) {
return num1 + num2;
}
In this example:
• The function add() is declared with a function prototype int add(int num1, int
num2); before the main() function.
• The prototype specifies that add() is a function that returns an integer (int) and
takes two integer parameters (int num1 and int num2).
• The add() function is defined after the main() function, which provides its
implementation.
• Even though add() is called in main() before its definition, the program compiles
successfully because the compiler knows about the function's signature through
the prototype.
Function prototypes are especially useful when functions are defined in separate source
files or when functions call each other. They allow the compiler to perform type checking
and ensure that functions are used correctly throughout the program.
MUQuestionPapers.com
return 0;
}
Output: Length of the string: 13
b) strcmp() Function:
The strcmp() function is used to compare two strings lexicographically. It returns an integer
value:
• 0 if the strings are equal,
• a negative value if the first string is less than the second string, and
• a positive value if the first string is greater than the second string.
Example: #include <stdio.h>
#include <string.h>
int main() {
char str1[] = "apple";
char str2[] = "banana";
int result = strcmp(str1, str2);
printf("Comparison result: %d\n", result);
return 0;
}
c) strcat() Function:
The strcat() function is used to concatenate (append) one string to the end of another. It
appends a copy of the source string to the destination string, overwriting the null terminator
of the destination string, and then adds a new null terminator. It is declared in the <string.h>
header file.
Example: #include <stdio.h>
#include <string.h>
int main() {
char dest[20] = "Hello, ";
char src[] = "World!";
strcat(dest, src);
printf("Concatenated string: %s\n", dest);
return 0;
}
Output:- Concatenated string: Hello, World!
MUQuestionPapers.com
E. Define Structure and explain the syntax of declaration of Structure
with example [5]
Ans:- A structure in C is a user-defined data type that allows you to group together
different variables under a single name. Each variable within a structure is called a member
or field. Structures provide a way to represent complex data in a more organized and logical
manner.
Example:
#include <stdio.h>
// Define a structure called "Person"
struct Person {
char name[50];
int age;
};
int main() {
struct Person person1;
strcpy(person1.name, "John");
person1.age = 30;
printf("Name: %s\n", person1.name);
printf("Age: %d\n", person1.age);
return 0;
}
In this example:
MUQuestionPapers.com
• We define a structure called "Person" with two members: name and age.
• Inside the main() function, we declare a variable person1 of type struct Person.
• We assign values to the members of person1 using dot (.) notation.
• Finally, we print the details of person1 using printf() statements.
To draw a flowchart to find the sum of digits of an accepted number, we can follow these
steps:
Initialize sum = 0
MUQuestionPapers.com
Display sum of digits
End
In this flowchart:
• "Start" and "End" are terminal points indicating the beginning and end of the
flowchart, respectively.
• "Accept the number from user" is a process for input.
• "Initialize sum = 0" initializes the variable to store the sum.
• The loop labeled "Repeat until num == 0" extracts digits, adds them to the sum, and
removes them from the number until the number becomes zero.
• "Display sum of digits" is a process for output.
B.Write a program to sort the elements of one dimensional array in
ascending order. [5]
Ans:- Here's a simple C program to sort the elements of a one-dimensional array in
ascending order using the bubble sort algorithm:
#include <stdio.h>
void bubbleSort(int arr[], int n) {
int i, j, temp;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap arr[j] and arr[j+1]
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
MUQuestionPapers.com
int arr[10], i, n;
The conditional operator is often used as a replacement for simple if-else statements when
assigning values based on a condition.
Example:
#include <stdio.h>
int main() {
int num = 10;
int result;
// Equivalent to:
// if (num % 2 == 0)
// result = 1;
// else
// result = 0;
return 0;
}
In this example:
• We have a variable num with a value of 10.
• We use the conditional operator (num % 2 == 0) ? 1 : 0; to check if num is even.
• If num is even, the value 1 is assigned to result.
• If num is odd, the value 0 is assigned to result.
• Finally, we print the value of result, which will be 1 since num is even.
The conditional operator is useful for writing concise and readable code, especially when
performing simple conditional assignments.
Q.3 A. Explain control breaking statements available in C language [5]
Ans:-
In C language, control breaking statements are used to alter the flow of control in a program
by breaking out of loops or skipping certain iterations. There are three main control
breaking statements available in C:
1. break:
• The break statement is used to terminate the execution of a loop immediately
when a certain condition is met.
• It is commonly used in switch statements and loop constructs such as for,
while, and do-while.
• When a break statement is encountered inside a loop, the loop is terminated,
and the program continues with the statement immediately following the loop.
Example:
for (int i = 0; i < 10; i++) {
if (i == 5) {
break; // Terminate the loop when i equals 5
}
printf("%d ", i);
MUQuestionPapers.com
Output:- 0 1 2 3 4
2. continue:
The continue statement is used to skip the rest of the code inside a loop for the current
iteration and proceed to the next iteration.
When a continue statement is encountered inside a loop, the remaining code inside the
loop for that iteration is skipped, and the loop proceeds with the next iteration.
Example:
for (int i = 0; i < 5; i++) {
if (i == 2) {
continue; // Skip iteration when i equals 2
}
printf("%d ", i);
}
Output:- 0 1 3 4
3. return:
The return statement is used to terminate the execution of a function and return a value (if
the function has a return type other than void) to the caller.
When a return statement is encountered inside a function, the function immediately exits,
and control is returned to the calling function along with the specified return value (if any).
Example:-
int sum(int a, int b) {
return a + b; // Return the sum of a and b
}
Usage:-
int result = sum(3, 5);
printf("Sum: %d\n", result);
These control breaking statements provide programmers with flexibility in controlling the
flow of their programs, making it possible to write more efficient and structured code.
MUQuestionPapers.com
=x3+2x2 20<x<=30
=0 x>30
Ans:-
You can implement the program in C to calculate the value of f(x) based on different ranges
of x as follows:
#include <stdio.h>
double calculate_fx(double x) {
double result;
if (x >= 0 && x <= 10) {
result = x * x + 2;
} else if (x > 10 && x <= 20) {
result = x * x + 2 * x;
} else if (x > 20 && x <= 30) {
result = x * x * x + 2 * x * x;
} else {
result = 0;
}
return result;
}
int main() {
double x, result;
printf("Enter the value of x: ");
scanf("%lf", &x);
result = calculate_fx(x);
printf("f(x) = %.2lf\n", result);
return 0;
}
In this program:
The calculate_fx function takes the value of x as input and calculates the corresponding
value of f(x) based on the given ranges using if-else statements.
Ans:-
#include <stdio.h>
#include <string.h>
#include <ctype.h>
return 1; // Palindrome
MUQuestionPapers.com
int main() {
char str[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
return 0;
}
This program:
• Accepts a string from the user using fgets.
• Defines a function isPalindrome to check whether the given string is a palindrome.
• Converts the string to lowercase to make the comparison case-insensitive.
• Checks each character from the beginning and end of the string to determine
whether it is a palindrome or not.
• Prints the appropriate message based on the result.
Q.4 A. Explain different data type modifiers available in C language. [5]
Ans:- In C language, data type modifiers are keywords used to modify the properties of
data types, such as the storage size, range, and sign. These modifiers allow programmers
to control how variables are stored in memory and how they behave during operations.
Here are the different data type modifiers available in C:
1.signed and unsigned:
• These modifiers are used with integer data types (char, int, short, long) to
specify whether the variable can represent both positive and negative values
(signed) or only non-negative values (unsigned).
• By default, integer types are signed.
Example:
• unsigned int x; // Only non-negative values
• signed char c; // Both positive and negative values
MUQuestionPapers.com
2.short and long:
• These modifiers are used with integer data types to specify the storage size of
variables.
• short typically represents a smaller range of values compared to int, while long
typically represents a larger range of values.
Example:
3. long long:
• This modifier is used with integer data types to specify an extended storage size,
allowing for an even larger range of values.
• Introduced in C99 standard.
Example:
• float f; // Single-precision floating-point number
• double d; // Double-precision floating-point number
• long double ld; // Extended-precision floating-point number
5. const:
• The const modifier is used to declare constants, which are variables whose values
cannot be modified once initialized.
• Attempting to modify a const variable results in a compiler error.
MUQuestionPapers.com
int squareRoot(int num) {
int low = 0, high = num, mid, result = -1;
if (square == num) {
result = mid;
break;
} else if (square < num) {
low = mid + 1;
result = mid; // Store potential square root
} else {
high = mid - 1;
}
}
return result;
}
int main() {
int num, result;
result = squareRoot(num);
if (result != -1) {
printf("Square root of %d is: %d\n", num, result);
} else {
printf("%d is not a perfect square integer.\n", num);
}
return 0;
}
In this program:
• The squareRoot() function takes an integer num as input and performs a binary
search to find the square root of the given number.
• The main() function prompts the user to enter a perfect square integer, calls the
squareRoot() function to compute its square root, and then prints the result.
• If the input number is not a perfect square, the function returns -1, and the program
indicates that the input is not a perfect square integer.
MUQuestionPapers.com
D.Explain the multi-way branching statement available in C language
with example. [5]
Ans:- In C language, the multi-way branching statement available is the switch statement.
The switch statement allows you to test the value of an expression against multiple
possible constant values and execute different blocks of code based on the matching value.
This provides a more efficient alternative to a series of nested if-else statements when you
need to perform multiple comparisons.
Inside the switch block, you list multiple case labels, each followed by a constant value.
The break statement is used to terminate the switch block and exit to the end of the
switch statement. Without break, execution will continue to the next case label regardless
of whether its condition is met.
The default label is optional and is executed if no match is found for the expression.
Here's an example to demonstrate the switch statement:
#include <stdio.h>
int main() {
int choice;
switch (choice) {
case 1:
MUQuestionPapers.com
printf("You entered 1.\n");
break;
case 2:
printf("You entered 2.\n");
break;
case 3:
printf("You entered 3.\n");
break;
default:
printf("Invalid choice.\n");
}
return 0;
}
In this example:
The user is prompted to enter a number (1-3).
The value entered by the user is compared to each case label inside the switch statement.
MUQuestionPapers.com
int main() {
int num;
return 0;
}
In this program:
The isPrime function takes an integer num as input and returns 1 if the number is prime
and 0 otherwise.
The function checks whether the given number is less than or equal to 1 (not a prime
number), and then iterates from 2 to the square root of the number to check for factors.
Now, let's write a program to calculate the power of a given number using a recursive
function:
#include <stdio.h>
else {
}
}
int main() {
double base;
int exponent; // Input base and exponent from user
printf("Enter base: ");
scanf("%lf", &base);
printf("Enter exponent: ");
scanf("%d", &exponent);
We define a recursive function power that calculates the power of a given base raised to a
given exponent.
The base case is when the exponent is 0, in which case the function returns 1.
For positive exponents, the function recursively calls itself with the exponent decremented
by 1.
MUQuestionPapers.com
• Each pattern iterates from the current row number down to 1, printing the numbers in
ascending order on each row.
#include <stdio.h>
int main() {
int rows, i, j;
// Input the number of rows from the user
printf("Enter the number of rows: ");
scanf("%d", &rows);
// Display the patterns
printf("Pattern 1:\n");
for (i = rows; i >= 1; i--) {
for (j = 1; j <= i; j++) {
printf("%d ", j);
}
printf("\n");
}
printf("\nPattern 2:\n");
for (i = rows; i >= 1; i--) {
for (j = 1; j <= i; j++) {
printf("%d ", j);
}
printf("\n");
}
printf("\nPattern 3:\n");
for (i = rows; i >= 1; i--) {
for (j = 1; j <= i; j++) {
printf("%d ", j);
}
printf("\n");
}
printf("\nPattern 4:\n");
for (i = rows; i >= 1; i--) {
for (j = 1; j <= i; j++) {
printf("%d ", j);
MUQuestionPapers.com
}
printf("\n");
}
return 0;
}
Q.5.A Write a program to find transpose of a square matrix using only one matrix [5]
Ans:- #include <stdio.h>
In this program:
struct Employee {
int empNo;
MUQuestionPapers.com
char empName[50];
int experience;
float salary;
};
int main() {
int numEmployees, i;
scanf("%d", &numEmployees);
scanf("%d", &employees[i].empNo);
scanf("%d", &employees[i].experience);
printf("Salary: ");
scanf("%f", &employees[i].salary);
// Display employees with 5 or more years of experience and salary less than Rs. 10,000
MUQuestionPapers.com
printf("\nEmployees with 5 or more years of experience and salary less than Rs.
10,000:\n");
for (i = 0; i < numEmployees; i++) {
printf("\n");
return 0;
MUQuestionPapers.com