Cse Question Ans
Cse Question Ans
Part A
1. a) Is it possible to convert all if else code into switch code? Give an example?
Ans :No, it is not possible to convert all if-else code into switch code in C
programming .Here are some reasons why not all if-else code can be converted into switch
code:
Complex conditions: if-else statements can handle complex conditions involving
logical operators (&&, ||), comparisons, and expressions. On the other hand, switch
statements only support simple equality checks on a single integral type. If your
conditions involve complex logic or non-constant expressions, it cannot be directly
converted into a switch statement.
Non-integral types: switch statements in C are designed to work with integral types
(such as int or char). They cannot be used with other types, such as floating-point
numbers or strings. if-else statements provide more flexibility in handling conditions
with different data types.
While switch statements can offer a more concise and readable approach in certain cases,
they have limitations compared to the flexibility of if-else statements.
while (binaryNum != 0) {
remainder = binaryNum % 10;
decimalNum += remainder * base;
base *= 2;
binaryNum /= 10;
}
printf("Decimal equivalent: %d\n", decimalNum);
return 0;
}
Main function : The main function in a C program serves as the entry point for
program execution and is required in every C program. It contains the program's
executable statements and can optionally return a value to indicate the program's
status upon completion.
Comment : Comments in a C program are used to provide explanations or notes
within the code that are ignored by the compiler during the compilation process.
Part B
5. a) Explain the function general form?
Ans : The general form of a function in C programming includes the function's return type,
name, optional parameters enclosed in parentheses, and the function body enclosed in curly
braces.
return_type: Specifies the data type of the value that the function returns to the caller.
Use void if the function does not return a value.
function_name: Name of the function, which is used to call the function from other
parts of the program.
parameters: Optional input parameters enclosed in parentheses, which are variables or
values passed to the function for processing.
function_body: The block of code enclosed in curly braces, containing the statements
and operations to be executed when the function is called.
b) Find the output of following c programming.
For(i=0;i<10;i++)
{
For(j=0;j<=I;j++)//nested
Printf(“*”);
Printf(“\n”);
}
Output:
Inside the function: 6
Outside the function: 5
Call by reference :
1. While calling a function, in programming language instead of copying the values of
variables, the address of the variables is used it is known as “Call By References.
2. In call by reference, the memory address (reference) of the actual argument is passed
to the function.
3. The function can directly access and modify the value at that memory address, which
affects the original argument outside the function.
4. Call by reference is typically used when we want to modify the original value of the
argument or pass large data structures efficiently.
5. It requires explicit use of pointers or references in the function parameters.
For example:
Output:
Inside the function: 6
Outside the function: 6
continue ; }
Printf(“%d”,i);
i++ ; }
output :
0
1
2
7. a) Explain the operation of *ptr++ and ++*ptr with example ?
Ans :
*ptr++:
In this expression, ptr is a pointer variable.
The * operator dereferences the pointer, giving access to the value it points to.
The ++ operator increments the pointer to point to the next memory location.
The order of operations is postfix increment (++) first, followed by dereference (*).
After the increment, ptr points to the next memory location, and the value at the original
memory location is accessed.
Output:
Output:
Value: 2
Value: 3
Value: 4
8. a) Give an example of a function with no arguments and return values.
Ans :
#include <stdio.h>
void greet() {
printf ("Hello! Welcome to the program.\n");
}
int main() {
greet(); // Calling the function
return 0;
}
In this example, we have a function named greet() that doesn't have any parameters and has a
return type of void, indicating that it doesn't return any value. Inside the function, it prints the
greeting message "Hello! Welcome to the program." to the console.
int main() {
int numbers[] = {10, 20, 30, 40, 50};
return 0;
}
In this example, we have an integer array numbers initialized with values. To access a
specific element, we use the array name followed by the index enclosed within square
brackets, such as numbers[0], numbers[2], and numbers[4].You can also modify the value at
a specific index by assigning a new value to it, as shown with numbers[1] = 25;. This changes
the value at index 1 from 20 to 25.