0% found this document useful (0 votes)
15 views7 pages

Answer To The Question No 1

Uploaded by

istiakpranto00
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views7 pages

Answer To The Question No 1

Uploaded by

istiakpranto00
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Tasnuva ferdous

Id . 0112330821

Section .E

Cse 1111

Answer to the question no 1

a. #include <stdio.h>
int factorial(int n)
{ int result = 1; for (int i = 1; i <= n; i++)
{ result *= i; } return result; }
int sum(int a, int b)
{ return a + b; }
int main()
{ int num1, num2, num3;
printf("Enter three integers: ");
scanf("%d %d %d", &num1, &num2, &num3);
int factorialSum = sum(factorial(num1), sum(factorial(num2), factorial(num3)));
printf("Sum of factorials: %d\n", factorialSum);
return o;
}

Answer to the question no 2

I a B

0 10 30
1 30 19
2 49 18

b#include <stdio.h>

int main() {
char str[55] = "I love spl. Uiu has some good labs for spl.";
char newStr[55];
int i, j, k;

i = 0;
j = 0;
while (str[i] != '\0') {
if (str[i] == 's' && str[i+1] == 'p' && str[i+2] == 'l') {
for (k = 0; k < 3; k++) {
i++;
}
newStr[j++] = 'd';
newStr[j++] = 's';
newStr[j++] = 'a';
} else {
newStr[j++] = str[i++];
}
}
newStr[j] = '\0';

printf("%s\n", newStr);

return 0;
}

Answer to the question no 3

a.
#include <stdio.h>
#include <string.h> // For safer string manipulation

struct student {
char name[50]; // Allocate enough space for common names and a null terminator
int ID;
};

int main() {
struct student s1, s2;

// Assign values directly to structure members


strcpy(s1.name, "Rahim"); // Use strcpy for safer string assignment
s1.ID = 101;

// Create a pointer to a student structure (dynamic allocation not needed here)


struct student *s_ptr = &s2;

// Use scanf with correct format specifiers and error handling


printf("Enter name for student s2: ");
if (scanf("%49s", s_ptr->name) != 1) { // Check for successful read
fprintf(stderr, "Error reading name. Please enter a string.\n");
}

printf("Enter ID for student s2: ");


if (scanf("%d", &s_ptr->ID) != 1) { // Check for successful read
fprintf(stderr, "Error reading ID. Please enter an integer.\n");
}

printf("\nStudent Details:\n");
printf("Student 1:\n");
printf(" Name: %s\n", s1.name);
printf(" ID: %d\n", s1.ID);

printf("Student 2:\n");
printf(" Name: %s\n", s_ptr->name);
printf(" ID: %d\n", s_ptr->ID);

return 0;
}

b. #include <stdio.h>
#include <string.h>

// Structure to store patient information


struct Patient {
char name[50];
int age;
float height;
float weight;
float BMI;
};

int main() {
struct Patient patients[100];
int n, i, youngestPatientIndex;
float lowestBMI = 9999.0; // Initialize with a high value

printf("Enter the number of patients: ");


scanf("%d", &n);

// Take input for each patient


for (i = 0; i < n; i++) {
printf("\nEnter information for patient %d:\n", i + 1);
printf("Name: ");
scanf("%s", patients[i].name);
printf("Age: ");
scanf("%d", &patients[i].age);
printf("Height (in meters): ");
scanf("%f", &patients[i].height);
printf("Weight (in kilograms): ");
scanf("%f", &patients[i].weight);

// Calculate BMI
patients[i].BMI = patients[i].weight / (patients[i].height * patients[i].height);
}

// Find the youngest patient with lowest BMI


youngestPatientIndex = 0;
for (i = 1; i < n; i++) {
if (patients[i].age < patients[youngestPatientIndex].age ||
(patients[i].age == patients[youngestPatientIndex].age && patients[i].BMI < lowestBMI)) {
youngestPatientIndex = i;
lowestBMI = patients[i].BMI;
}
}

// Display information of the youngest patient with lowest BMI


printf("\nYoungest patient with lowest BMI:\n");
printf("Name: %s\n", patients[youngestPatientIndex].name);
printf("Age: %d\n", patients[youngestPatientIndex].age);
printf("Height: %.2f meters\n", patients[youngestPatientIndex].height);
printf("Weight: %.2f kilograms\n", patients[youngestPatientIndex].weight);
printf("BMI: %.2f\n", patients[youngestPatientIndex].BMI);

return 0;
}

Answer to the question no 4

a.
Inside power_of_2(16)
Inside power_of_2(8)
Inside power_of_2(4)
Inside power_of_2(2)
Inside power_of_2(1)

b. 1 2 4 5 6 7 8 9

Answer to the question no 5

a. #include <stdio.h>

int main() {

int arr[100];

int *arrPtr = arr;

int i, largest;

// Scan elements of the array using pointer with offset

printf("Enter 100 integers:\n");


for (i = 0; i < 100; i++) {

scanf("%d", arrPtr + i);

// Find the largest element using pointer

largest = *arrPtr;

for (i = 1; i < 100; i++) {

if (*(arrPtr + i) > largest) {

largest = *(arrPtr + i);

// Print the largest element

printf("The largest element is: %d\n", largest);

return 0;

b#include <stdio.h>

int main() {

// Re-open the file in append mode

FILE *fp = fopen("string.txt", "a");

if (fp == NULL) {

printf("Error opening file for appending.\n");

return 1;
}

// Append the new string

fprintf(fp, "This is another string\n");

// Close the file

fclose(fp);

return 0;

You might also like