79680913

Date: 2025-06-26 17:14:13
Score: 2.5
Natty:
Report link

Here's how to solve each of the 20 C programming problems step-by-step. I’ll give brief logic for each and sample function headers. Let me know which full programs you want:


1. Sum of divisors

int sum_of_divisors(int n) {
    int sum = 0;
    for (int i = 1; i <= n; i++)
        if (n % i == 0) sum += i;
    return sum;
}

2. Merge sort

void mergeSort(int arr[], int left, int right);
void merge(int arr[], int left, int mid, int right);

Use recursive divide-and-conquer + merge logic.


3. Check if string contains digits

int count_digits(char str[]) {
    int count = 0;
    for (int i = 0; str[i] != '\0'; i++)
        if (isdigit(str[i])) count++;
    return count;
}

4. GCD using recursion

int gcd(int a, int b) {
    if (b == 0) return a;
    return gcd(b, a % b);
}

5. Insertion Sort

void insertionSort(int arr[], int n);

Loop from i=1 to n, insert arr[i] in the sorted left part.


6. Reverse words in a sentence (in-place)

Reverse the full string, then reverse each word:

void reverseWords(char* str);

7. Count vowels and consonants

void count_vowels_consonants(char str[], int *vowels, int *consonants);

Check with isalpha() and vowel comparison.


8. Check Armstrong number

int isArmstrong(int n) {
    int sum = 0, temp = n;
    while (temp) {
        int d = temp % 10;
        sum += d * d * d;
        temp /= 10;
    }
    return sum == n;
}

9. Sum of squares in array

int sum_of_squares(int arr[], int n) {
    int sum = 0;
    for (int i = 0; i < n; i++)
        sum += arr[i] * arr[i];
    return sum;
}

10. Merge two sorted arrays (in-place)

If extra space is not allowed, use in-place merge like:

void mergeSortedArrays(int a[], int b[], int m, int n);

11. Perfect square check

int isPerfectSquare(int num) {
    int root = sqrt(num);
    return root * root == num;
}

12. Rotate matrix 90 degrees

void rotateMatrix(int matrix[N][N]);

Transpose + reverse rows or columns.


13. Power of number using recursion

int power(int x, int n) {
    if (n == 0) return 1;
    return x * power(x, n - 1);
}

14. Middle element of a linked list

Use slow and fast pointers:

struct Node* findMiddle(struct Node* head);

15. Remove duplicates from array

Sort array, then shift unique values:

int removeDuplicates(int arr[], int n);

16. Longest Common Subsequence

Use 2D dynamic programming:

int LCS(char* X, char* Y, int m, int n);

17. Pascal's Triangle

void printPascalsTriangle(int n);

Use combinatorics: nCr = n! / (r!(n-r)!).


18. Sum of odd and even elements

void sumOddEven(int arr[], int n, int *oddSum, int *evenSum);

19. Reverse array in groups

void reverseInGroups(int arr[], int n, int k);

20. Valid parentheses expression

Use a stack to match ( and ):

int isValidParentheses(char* str);

Would you like me to provide full C code for all, or start with a few specific ones (e.g. 1–5)?

Reasons:
  • Blacklisted phrase (1): how to solve
  • Long answer (-1):
  • Has code block (-0.5):
  • Ends in question mark (2):
  • Low reputation (1):
Posted by: Marium Sheikh