0% found this document useful (0 votes)
16 views5 pages

Error Eliminator2nd Round C Answers

Uploaded by

sumanthshetty080
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)
16 views5 pages

Error Eliminator2nd Round C Answers

Uploaded by

sumanthshetty080
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/ 5

Error Eliminator

2nd Round

Answers

Answer

#include <stdio.h>

#include <string.h>

void reverse_string(char *str) {

int n = strlen(str);

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

char temp = str[i];

str[i] = str[n - i - 1]; // Corrected index

str[n - i - 1] = temp; // Corrected index

int main() {

char str[] = "hello";

reverse_string(str);

printf("%s\n", str); // Output: "olleh"

return 0;

Answer

#include <stdio.h>

#include <limits.h>

Int maxSubArraySum(int a[], int size) {

Int max_so_far = INT_MIN, max_ending_here = 0;


For (int I = 0; I < size; i++) {

Max_ending_here = max_ending_here + a[i];

If (max_so_far < max_ending_here)

Max_so_far = max_ending_here;

If (max_ending_here < 0)

Max_ending_here = 0; // Corrected to reset to 0 instead of max_so_far

Return max_so_far;

Int main() {

Int a[] = {-2, 1, -3, 4, -1, 2, 1, -5, 4};

Int n = sizeof(a) / sizeof(a[0]);

Int max_sum = maxSubArraySum(a, n);

Printf(“Maximum contiguous sum is %d\n”, max_sum); // Output: 6

Return 0;

Answer

#include <stdio.h>

Int factorial(int n) {

If (n < 0) return -1; // Handle negative input

If (n == 0) return 1;

Return n * factorial(n – 1);

}
Int main() {

Int n = 5;

Printf(“Factorial of %d is %d\n”, n, factorial(n)); // Output: 120

Return 0;

Answer

#include <stdio.h>

Int binarySearch(int arr[], int l, int r, int x) {

If (r >= l) {

Int mid = l + (r – l) / 2;

If (arr[mid] == x)

Return mid;

If (arr[mid] > x)

Return binarySearch(arr, l, mid – 1, x);

Return binarySearch(arr, mid + 1, r, x);

Return -1;

Int main() {

Int arr[] = {2, 3, 4, 10, 40};

Int n = sizeof(arr) / sizeof(arr[0]);

Int x = 10;
Int result = binarySearch(arr, 0, n-1, x);

(result == -1) ? printf(“Element is not present in array\n”)

: printf(“Element is present at index %d\n”, result); // Output: Element is present at index


3

Return 0;

Answer

#include <stdio.h>

#include<math.h> //Include math lib file

Int is_prime(int num) {

If (num <= 1) {

Return 0;

For (int i = 2; i<=sqrt( num ); i++) {//I to I change and num to sqrt(num)

If (num % i == 0) {

Return 0;

Return 1;

Void primes_in_range(int start_num, int end_num) {

For (int num = start_num; num <= end_num; num++) {//to include end num

If (is_prime(num)) {

Printf(“%d “, num);

Printf(“\n”);

}
Int main() {

Int start_num = 10;

Int end_num = 50;

Printf(“Prime numbers from %d to %d:\n”, start_num, end_num);

Primes_in_range(start_num, end_num); // Output should be: 11 13 17 19 23 29 31 37 41 43 47

Return 0;

You might also like