"Level Up Your Coding Skills: Conquer 10 C Programming Challenges and Keep Going"

  C Programming Question For Practices 

 


Q1. How to print "HELLO WORLD " In C Programming ?

solution:     #include <stdio.h>

int main() {
    printf("Hello, World!\n");
    return 0;
}


Q2. Write a C program to find the factorial of a number.

Solution:     #include <stdio.h>

int factorial(int n) {
    if (n == 0)
        return 1;
    else
        return n * factorial(n - 1);
}

int main() {
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);
    printf("Factorial of %d is %d\n", num, factorial(num));
    return 0;
}

Q3. Write a C program to check whether a number is prime or not.

Solution:

 #include <stdio.h>

int isPrime(int n) {
    if (n <= 1)
        return 0;
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0)
            return 0;
    }
    return 1;
}

int main() {
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);
    if (isPrime(num))
        printf("%d is prime\n", num);
    else
        printf("%d is not prime\n", num);
    return 0;
}



Q4. Write a C program to reverse a given number.

Solution:

#include <stdio.h>

int reverseNumber(int num) {
    int rev = 0;
    while (num > 0) {
        rev = rev * 10 + num % 10;
        num /= 10;
    }
    return rev;
}

int main() {
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);
    printf("Reversed number: %d\n", reverseNumber(num));
    return 0;
}
 

 Q5. Write a C program to find the sum of digits of a number.

Solution:

#include <stdio.h>

int sumOfDigits(int num) {
    int sum = 0;
    while (num > 0) {
        sum += num % 10;
        num /= 10;
    }
    return sum;
}

int main() {
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);
    printf("Sum of digits: %d\n", sumOfDigits(num));
    return 0;
}

Q6. Write a C program to find the Fibonacci series up to a given term.

Solution:

 

#include <stdio.h>

void fibonacci(int n) {
    int a = 0, b = 1, c;
    printf("Fibonacci series: %d %d ", a, b);
    for (int i = 2; i < n; i++) {
        c = a + b;
        printf("%d ", c);
        a = b;
        b = c;
    }
    printf("\n");
}

int main() {
    int terms;
    printf("Enter the number of terms: ");
    scanf("%d", &terms);
    fibonacci(terms);
    return 0;
}

Q7. Write a C program to find the largest element in an array.

Solution:

#include <stdio.h>

int findMax(int arr[], int n) {
    int max = arr[0];
    for (int i = 1; i < n; i++) {
        if (arr[i] > max)
            max = arr[i];
    }
    return max;
}

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


Q8.  Write a C program to check if a given string is a palindrome or not.

Solution:

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

int isPalindrome(char str[]) {
    int left = 0;
    int right = strlen(str) - 1;

    while (left < right) {
        if (str[left] != str[right]) {
            return 0; // Not a palindrome
        }
        left++;
        right--;
    }

    return 1; // Palindrome
}

int main() {
    char str[100];

    printf("Enter a string: ");
    scanf("%s", str);

    if (isPalindrome(str)) {
        printf("%s is a palindrome.\n", str);
    } else {
        printf("%s is not a palindrome.\n", str);
    }

    return 0;
}

This program takes a string as input and checks if it is a palindrome or not. It uses two pointers (one from the start and one from the end) to compare characters and determine if the string is a palindrome.

 

Q9. Calculate the Fibonacci series up to a given number.

solution: 

#include <stdio.h>

// Function to print Fibonacci series
void fibonacci(int n) {
    int a = 0, b = 1, c;
    printf("Fibonacci series up to %d terms: ", n);
    printf("%d %d ", a, b);
    for (int i = 3; i <= n; i++) {
        c = a + b;
        printf("%d ", c);
        a = b;
        b = c;
    }
    printf("\n");
}

int main() {
    int num;
    printf("Enter the number of terms for Fibonacci series: ");
    scanf("%d", &num);
    fibonacci(num);
    return 0;
}

Q10. Calculate the sum of digits of a number.

solution: 

 #include <stdio.h>

// Function to calculate sum of digits
int sumOfDigits(int num) {
    int sum = 0;
    while (num != 0) {
        sum += num % 10;
        num /= 10;
    }
    return sum;
}

int main() {
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);

    int sum = sumOfDigits(num);
    printf("Sum of digits of %d is %d\n", num, sum);

    return 0;
}

Conclusion: Keep Coding, Keep Learning

Congratulations on completing these 10 C programming practice questions! By now, you've tackled a range of programming concepts and sharpened your problem-solving skills. Remember, programming is a journey of continuous learning and growth.

To further enhance your programming skills, consider exploring more challenges and projects. Here are a few suggestions to continue your coding journey:

  1. Explore online platforms like LeetCode, HackerRank, and CodeSignal for more programming challenges.
  2. Join coding communities and forums to engage with fellow programmers and learn from their experiences.
  3. Start working on small projects or contribute to open-source projects to apply your skills in real-world scenarios.
  4. Read programming blogs, articles, and books to stay updated with the latest trends and techniques in programming.

Never stop learning and exploring new avenues in the vast world of programming. The more you code, the more confident and skilled you'll become. Happy coding!


 
 





Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.