0% found this document useful (0 votes)
19 views

Final Assignment C Programming

Djekxijn

Uploaded by

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

Final Assignment C Programming

Djekxijn

Uploaded by

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

Q1)

#include <stdio.h>

int main() {

int int1, int2;

float float_num;

scanf("%d", &int1);

scanf("%d", &int2);

scanf("%f", &float_num);

int bitwise_result = (int1 & int2) | (int1 ^ int2);

printf("%d\n", bitwise_result);

float arithmetic_result = (int1 * float_num) + ((float)int2 / float_num);

printf("%.4f\n", arithmetic_result);

return 0;

Q2)

#include <stdio.h>

int main() {

int n;

scanf("%d", &n);

if (n > 6) n = 6; // Limit array size to avoid out-of-bounds

int x[6] = {0};

for (int i = 0; i < n; i++) {

scanf("%d", &x[i]);
}

if (x[4] == 0 || x[5] == 0) {

printf("0\n");

return 0;

int result = (((x[0] + x[1]) - x[2]) * x[3] / x[4]) % x[5];

printf("%d\n", result);

return 0;

Q3)

#include <stdio.h>

#include <string.h>

#include <ctype.h>

void swapCharacters(char *str, char mostFreq, char leastFreq) {

for (int i = 0; str[i] != '\0'; i++) {

if (tolower(str[i]) == tolower(mostFreq)) {

str[i] = isupper(str[i]) ? toupper(leastFreq) : tolower(leastFreq);

} else if (tolower(str[i]) == tolower(leastFreq)) {

str[i] = isupper(str[i]) ? toupper(mostFreq) : tolower(mostFreq);

int main() {

char str[100];

int freq[256] = {0};


scanf("%s", str);

for (int i = 0; str[i] != '\0'; i++) {

freq[tolower(str[i])]++;

char mostFreq = str[0], leastFreq = str[0];

for (int i = 0; str[i] != '\0'; i++) {

if (freq[tolower(str[i])] > freq[tolower(mostFreq)]) {

mostFreq = str[i];

if (freq[tolower(str[i])] < freq[tolower(leastFreq)]) {

leastFreq = str[i];

swapCharacters(str, mostFreq, leastFreq);

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

return 0;

Q4)

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <ctype.h>

typedef struct {

char title[100];

char author[100];
int year;

} Book;

Book *library = NULL;

int bookCount = 0;

void toLowerCase(char *str) {

for (int i = 0; str[i]; i++) {

str[i] = tolower(str[i]);

void addBook() {

bookCount++;

library = realloc(library, bookCount * sizeof(Book));

getchar();

fgets(library[bookCount - 1].title, 100, stdin);

library[bookCount - 1].title[strcspn(library[bookCount - 1].title, "\n")] = '\0';

fgets(library[bookCount - 1].author, 100, stdin);

library[bookCount - 1].author[strcspn(library[bookCount - 1].author, "\n")] = '\0';

scanf("%d", &library[bookCount - 1].year);

void displayBooks() {

if (bookCount == 0) {

printf("No books in the library.\n");

} else {

for (int i = 0; i < bookCount; i++) {


printf("Title: %s, Author: %s, Year: %d\n", library[i].title, library[i].author, library[i].year);

void searchBook() {

char searchTitle[100];

getchar();

fgets(searchTitle, 100, stdin);

searchTitle[strcspn(searchTitle, "\n")] = '\0';

int found = 0;

for (int i = 0; i < bookCount; i++) {

char titleLower[100];

strcpy(titleLower, library[i].title);

toLowerCase(titleLower);

char searchLower[100];

strcpy(searchLower, searchTitle);

toLowerCase(searchLower);

if (strstr(titleLower, searchLower)) {

printf("Title: %s, Author: %s, Year: %d\n", library[i].title, library[i].author, library[i].year);

found = 1;

if (!found) {

printf("No book found with the given title.\n");

}
int main() {

int choice;

do {

scanf("%d", &choice);

switch (choice) {

case 1:

addBook();

printf("Book added successfully.\n");

break;

case 2:

displayBooks();

break;

case 3:

searchBook();

break;

case 4:

printf("Exiting...\n");

break;

default:

break;

} while (choice != 4);

free(library);

return 0;

Q5)

#include <stdio.h>
int main() {

int n, factorial = 1;

// Input the number

scanf("%d", &n);

// Error handling for negative input

if (n < 0) {

printf("Error! Factorial of a negative number doesn't exist.\n");

return 0;

// Factorial calculation using a loop

for (int i = 1; i <= n; i++) {

factorial *= i;

// Print the result

printf("Factorial of %d is %d\n", n, factorial);

return 0;

Q6)

#include <stdio.h>

void modifyArray(int *arr, int n) {

for (int i = 0; i < n; i++) {

arr[i] *= 2;

}
int main() {

int n;

scanf("%d", &n);

if (n <= 0) {

printf("Array size must be positive.\n");

return 1;

int arr[n];

for (int i = 0; i < n; i++) {

scanf("%d", &arr[i]);

printf("Original array:");

for (int i = 0; i < n; i++) {

printf(" %d", arr[i]);

modifyArray(arr, n);

printf("\nModified array:");

for (int i = 0; i < n; i++) {

printf(" %d", arr[i]);

return 0;

Q7
#include <stdio.h>

struct Student {

char name[50];

int marks;

};

int main() {

int n;

scanf("%d", &n);

struct Student students[n];

int totalMarks = 0;

for (int i = 0; i < n; i++) {

scanf("%s %d", students[i].name, &students[i].marks);

totalMarks += students[i].marks;

float average = (float)totalMarks / n;

printf("Average Marks: %.2f\n", average);

return 0;

Q8)

#include <stdio.h>

int main() {
char *days[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

int n;

scanf("%d", &n);

int arr[n];

for (int i = 0; i < n; i++) {

scanf("%d", &arr[i]);

printf("Days in reverse order:\n");

for (int i = n - 1; i >= 0; i--) {

printf("%s%s", days[arr[i]], i > 0 ? " " : "");

printf("\n");

return 0;

Q9)

#include <stdio.h>

typedef struct {

float real;

float imag;

} Complex;

Complex add(Complex a, Complex b) {

Complex result;

result.real = a.real + b.real;

result.imag = a.imag + b.imag;


return result;

Complex subtract(Complex a, Complex b) {

Complex result;

result.real = a.real - b.real;

result.imag = a.imag - b.imag;

return result;

int main() {

Complex num1, num2, sum, difference;

scanf("%f %f", &num1.real, &num1.imag);

scanf("%f %f", &num2.real, &num2.imag);

sum = add(num1, num2);

difference = subtract(num1, num2);

printf("Addition: %.1f + %.1fi\n", sum.real, sum.imag);

printf("Subtraction: %.1f - %.1fi\n", difference.real, difference.imag);

return 0;

Q10)

#include <stdio.h>

struct Student {

char name[50];

int age;
float gpa;

};

int main() {

int n;

scanf("%d", &n);

struct Student students[n];

for (int i = 0; i < n; i++) {

scanf("%s %d %f", students[i].name, &students[i].age, &students[i].gpa);

printf("All Students:\n");

int highest = 0;

for (int i = 0; i < n; i++) {

printf("Name: %s, Age: %d, GPA: %.1f\n", students[i].name, students[i].age, students[i].gpa);

if (students[i].gpa > students[highest].gpa) {

highest = i;

printf("Student with the highest GPA:\n");

printf("Name: %s, Age: %d, GPA: %.1f\n", students[highest].name, students[highest].age,


students[highest].gpa);

return 0;

You might also like