c programmimg Answer
c programmimg Answer
7. Type Conversion in C:
Type conversion refers to converting a value from one data type to another.
o Implicit Type Conversion: Automatically done by the compiler
when data is converted from a smaller to a larger data type.
Example:
c
Copy code
int x = 5;
double y = x; // Implicit conversion from int to double
o Explicit Type Casting: Done manually by the programmer using a
cast operator. Example:
c
Copy code
double x = 5.7;
int y = (int)x; // Explicit conversion from double to int
int main() {
char *arr[] = {"Hello", "World", "C"};
for (int i = 0; i < 3; i++) {
printf("%s ", arr[i]);
}
return 0;
}
Output:
mathematica
Copy code
Hello World C
// Call by value
void swapByValue(int a, int b) {
int temp = a;
a = b;
b = temp;
}
// Call by reference
void swapByReference(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int x = 5, y = 10;
swapByValue(x, y);
printf("Call by Value: x = %d, y = %d\n", x, y); // x and y remain unchanged
swapByReference(&x, &y);
printf("Call by Reference: x = %d, y = %d\n", x, y); // x and y are swapped
return 0;
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
sumArray(arr, 5);
return 0;
}
int main() {
enum Weekday today = Monday;
if (today == Monday) {
printf("It's Monday!\n");
}
return 0;
}
20. Program to Use Switch Statement to Print the Season Based on User Input
c
Copy code
#include <stdio.h>
int main() {
int month;
printf("Enter month number (1-12): ");
scanf("%d", &month);
switch (month) {
case 1:
case 2:
case 12:
printf("Winter\n");
break;
case 3:
case 4:
case 5:
printf("Spring\n");
break;
case 6:
case 7:
case 8:
printf("Summer\n");
break;
case 9:
case 10:
case 11:
printf("Fall\n");
break;
default:
printf("Invalid month\n");
}
return 0;
}
21. Differences Between Structures and Unions in C
Structures: A structure is a collection of variables of different data types
grouped together. Each member of a structure has its own memory
location.
Example:
c
Copy code
struct Employee {
int id;
char name[50];
};
Unions: A union is similar to a structure, but all members share the same
memory location. Only one member can hold a value at a time.
Example:
c
Copy code
union Data {
int i;
float f;
};
Memory Allocation:
Structures: The total memory size is the sum of the sizes of all members.
Unions: The total memory size is the size of the largest member because
all members share the same memory.
Program Example (Structure and Union):
c
Copy code
#include <stdio.h>
// Structure definition
struct Employee {
int id;
char name[50];
};
// Union definition
union Data {
int i;
float f;
};
int main() {
struct Employee emp = {1, "John Doe"};
printf("Employee ID: %d, Name: %s\n", emp.id, emp.name);
data.f = 3.14;
printf("Union Data (float): %.2f\n", data.f); // Union stores the float now
return 0;
}
#define ROWS 2
#define COLS 2
int main() {
int A[ROWS][COLS] = {{1, 2}, {3, 4}};
int B[ROWS][COLS] = {{5, 6}, {7, 8}};
int result[ROWS][COLS];
printf("Matrix A:\n");
printMatrix(A);
printf("Matrix B:\n");
printMatrix(B);
matrixAdd(A, B, result);
printf("Matrix A + B:\n");
printMatrix(result);
matrixSubtract(A, B, result);
printf("Matrix A - B:\n");
printMatrix(result);
matrixMultiply(A, B, result);
printf("Matrix A * B:\n");
printMatrix(result);
return 0;
}
int main() {
char str[] = "Hello, World!";
int vowelCount = 0;
int main() {
int a = 5, b = 10, c = 2;
int result = a + b * c; // Multiplication has higher precedence than addition
printf("Result: %d\n", result); // Output will be 25, not 34
int d = 5;
d = 10 + 2 * 3; // Multiplication first, then addition
printf("d = %d\n", d); // Output will be 16
return 0;
}
int main() {
float radius, area;
printf("Enter radius: ");
scanf("%f", &radius);
area = PI * radius * radius;
printf("Area of the circle: %.2f\n", area);
return 0;
}
Triangle:
Formula: Area = (base * height) / 2
Algorithm:
1. Input base and height.
2. Calculate area using the formula.
3. Print the result.
Pseudocode:
arduino
Copy code
BEGIN
READ base, height
area = (base * height) / 2
PRINT area
END
C Code:
c
Copy code
#include <stdio.h>
int main() {
float base, height, area;
printf("Enter base and height: ");
scanf("%f %f", &base, &height);
area = (base * height) / 2;
printf("Area of the triangle: %.2f\n", area);
return 0;
}
Rectangle:
Formula: Area = length * width
Algorithm:
1. Input length and width.
2. Calculate area using the formula.
3. Print the result.
Pseudocode:
arduino
Copy code
BEGIN
READ length, width
area = length * width
PRINT area
END
C Code:
c
Copy code
#include <stdio.h>
int main() {
float length, width, area;
printf("Enter length and width: ");
scanf("%f %f", &length, &width);
area = length * width;
printf("Area of the rectangle: %.2f\n", area);
return 0;
}
int main() {
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
continue; // Skip even numbers
}
printf("%d ", i);
}
return 0;
}
Output: 1 3 5 7 9
int fibonacci(int n) {
if (n <= 1) {
return n;
}
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
printf("Fibonacci number at position %d: %d\n", n, fibonacci(n));
return 0;
}
int main() {
int arr[2][2] = {{1, 2}, {3, 4}};
int *ptr = &arr[0][0]; // Pointer to the first element
return 0;
}
37. Pointer Arithmetic with Multi-Dimensional Arrays
void greet() {
printf("Hello, World!\n");
}
int main() {
greet(); // Calling the function
return 0;
}
2. Functions with Arguments and No Return Value: These functions accept
parameters but do not return any value. The return type is void.
Example:
c
Copy code
#include <stdio.h>
int main() {
printSum(5, 3); // Calling the function with arguments
return 0;
}
3. Functions with No Arguments but with Return Value: These functions
do not take any parameters but return a value.
Example:
c
Copy code
#include <stdio.h>
int getFive() {
return 5;
}
int main() {
int result = getFive(); // Calling the function
printf("Returned value: %d\n", result);
return 0;
}
4. Functions with Arguments and Return Value: These functions take
arguments and return a value.
Example:
c
Copy code
#include <stdio.h>
int main() {
int result = multiply(5, 3); // Calling the function with arguments
printf("Product: %d\n", result);
return 0;
}
39. Difference Between an Array and a Structure in C
int main() {
// Initializing an array of structures
struct Employee emp[2] = {{1, "Alice"}, {2, "Bob"}};
return 0;
}
struct Employee {
int id;
char name[50];
};
int main() {
struct Employee emp[2] = {{1, "Alice"}, {2, "Bob"}};
return 0;
}