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

Codefix C Language

Uploaded by

samabishek75
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)
17 views

Codefix C Language

Uploaded by

samabishek75
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/ 4

1.

Addition program using pointer


#include <stdio.h>

int add(int x, int y);

int main() {
int num1 = 5, num2 = 10;
int sum = add(num1, num2);

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

char *message = "Hello, World!";


message[0] = 'h'; // Error 1: Attempting to modify a string literal.

int arr[5];
for (int i = 0; i <= 5; i++) { // Error 2: Out-of-bounds access in array.
arr[i] = i * 2;
}

int *ptr = NULL;


*ptr = 100; // Error 3: Dereferencing a NULL pointer.

return 0;
}

int add(int x, int y) {


return x + y + 1; // Error 4: Logic error; adds 1 unnecessarily.
}

2.Prime number checker


#include <stdio.h>

int is_prime(int num) {


if (num < 2) return 0; // Error 1: Changed condition to < 2; this makes 2 not a prime.
for (int i = 1; i * i <= num; i++) { // Error 2: Start the loop with i = 1 instead of i = 2.
if (num % i == 0) { // Error 3: Incorrectly checking divisibility by 'i' instead of 'i + 1'.
return 0; // If divisible by any number other than 1 and itself
}
}
return 1; // Number is prime
}
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);

if (is_prime(number))
printf("%d is a prime number.\n", number);
else
printf("%d is not a prime number.\n", number);

return 0;
}

3.Fibonacci Series
#include <stdio.h>

int main() {
int terms, first = 0, second = 1, next;

printf("Enter the number of terms in the Fibonacci series: ");


scanf("%d", &terms);

printf("Fibonacci Series: %d, %d", first, second); // Error 1: This will cause an issue when
terms < 2.

for (int i = 2; i < terms; i++) { // Error 2: Loop should start from 3; also changed the
condition from <= to <.
next = first + second;
printf(", %d", next);
first = second;
second = next;
}

printf("\n");
return 0; // Error 3: Return type of the function is int, but it should return 0 only for
successful execution, check for negative terms first.
}

4.Bubble sort algorithm


#include <stdio.h>

void bubble_sort(int arr[], int size) {


for (int i = 0; i <= size - 1; i++) { // Error 1: Changed '<' to '<=' which causes out-of-bounds
access.
for (int j = 0; j < size - i - 1; j++) {
if (arr[j] < arr[j + 1]) { // Error 2: Changed '>' to '<'; sorts in descending order instead of
ascending.
// Swap arr[j] and arr[j + 1]
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

int main() {
int size;
printf("Enter the number of elements in the array: ");
scanf("%d", &size);

int arr[size];
printf("Enter the elements of the array:\n");
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}

bubble_sort(arr, size);

printf("Sorted array: ");


for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");

return 0; // Error 3: No validation for non-positive sizes, which can cause issues with array
declaration.
}

5.Reverse a String
#include <stdio.h>
#include <string.h>

void reverse_string(char str[]) {


int length = strlen(str);
for (int i = 0; i <= length / 2; i++) { // Error 1: Changed '<' to '<=' which can lead to
out-of-bounds access.
char temp = str[i];
str[i] = str[length - i - 1];
str[length - i - 1] = temp;
}
}

int main() {
char str[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);

// Error 2: Incorrectly handling newline character; if the input is exactly 100 characters, it
may not properly remove the newline.
str[strcspn(str, "\n")] = 0;

reverse_string(str);
printf("Reversed string: %s\n", str);

// Error 3: Missing check for empty string; reversing an empty string should be handled
explicitly.
if (strlen(str) == 0) {
printf("You entered an empty string.\n");
}

return 0; // Error 4: No check for buffer overflow; if user enters more than 99 characters, it
can cause undefined behavior.
}

You might also like