Function

  1. "Write and test the following power () function that returns x raised to the power n,
    #include <stdio.h>

    // Function to calculate x raised to the power n
    double power(double x, int n) {
        double result = 1.0;
        int i;

        if (n >= 0) {
            for (i = 0; i < n; i++) {
                result *= x;
            }
        } else {
            for (i = 0; i > n; i--) {
                result /= x;
            }
        }

        return result;
    }

    int main() {
        double x;
        int n;

        // Input values for x and n
        printf("Enter the value of x: ");
        scanf("%lf", &x);
        printf("Enter the value of n: ");
        scanf("%d", &n);

        // Calculate and print the result
        double result = power(x, n);
        printf("%lf raised to the power %d is: %lf\n", x, n, result);

        return 0;
    }




  2. Any year is entered through the keyboard. Write a function to determine whether the year is leap year or not.

    #include <stdio.h>
    #include <stdbool.h> // Include standard library for boolean type

    // Function to check if a year is a leap year
    bool isLeapYear(int year) {
        // Leap year conditions
        if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
            return true;
        } else {
            return false;
        }
    }

    int main() {
        int year;

        // Input year from the user
        printf("Enter a year: ");
        scanf("%d", &year);

        // Check if the year is a leap year using the function
        if (isLeapYear(year)) {
            printf("%d is a leap year.\n", year);
        } else {
            printf("%d is not a leap year.\n", year);
        }

        return 0;
    }




  3. Write a general  purpose function to convert any given year into its Roman equivalent. use these Roman Equivalents for Decimal number: 1-I, 5-V, 10-X, 50-L, 100-C, 500-D, 1000-M

    example: Roman equivalent of 1988 is MDCCCCLXXXVIII

    Roman equivalent of 1525  is MDXXV

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

    // Function to convert a number to Roman numeral
    void intToRoman(int num, char* roman) {
        // Define arrays for Roman numerals and their corresponding values
        struct {
            int value;
            char symbol[4];
        } romanNumerals[] = {
            {1000, "M"},
            {900, "CM"},
            {500, "D"},
            {400, "CD"},
            {100, "C"},
            {90, "XC"},
            {50, "L"},
            {40, "XL"},
            {10, "X"},
            {9, "IX"},
            {5, "V"},
            {4, "IV"},
            {1, "I"}
        };

        int i = 0;
        while (num > 0) {
            // Check if the current value can be subtracted from num
            while (num >= romanNumerals[i].value) {
                strcat(roman, romanNumerals[i].symbol);
                num -= romanNumerals[i].value;
            }
            i++;
        }
    }

    int main() {
        int year;
        char roman[100] = ""; // Buffer to hold the Roman numeral string

        // Input year
        printf("Enter a year: ");
        scanf("%d", &year);

        // Convert year to Roman numeral
        intToRoman(year, roman);

        // Output the Roman numeral
        printf("Roman equivalent of %d is %s\n", year, roman);

        return 0;
    }




  4. Write a function to calculate the factorial value of any integer entered through the keyboard.

    #include <stdio.h>

    // Function to calculate factorial
    unsigned long long factorial(int n) {
        unsigned long long fact = 1;
       
        // Calculate factorial
        for (int i = 1; i <= n; ++i) {
            fact *= i;
        }
       
        return fact;
    }

    int main() {
        int num;

        // Input the number from the user
        printf("Enter a non-negative integer to calculate its factorial: ");
        scanf("%d", &num);

        // Check if the number is non-negative
        if (num < 0) {
            printf("Error: Factorial is not defined for negative numbers.\n");
        } else {
            // Calculate factorial using the function
            unsigned long long result = factorial(num);
            printf("Factorial of %d = %llu\n", num, result);
        }

        return 0;
    }





  5. "Write a function to add, subtract, multiply and divide two complex numbers(x+iy) and (c+id)."

    #include <stdio.h>

    // Structure to represent a complex number
    typedef struct {
        double real;
        double imag;
    } Complex;

    // Function to add two complex numbers
    Complex add(Complex a, Complex b) {
        Complex result;
        result.real = a.real + b.real;
        result.imag = a.imag + b.imag;
        return result;
    }

    // Function to subtract two complex numbers
    Complex subtract(Complex a, Complex b) {
        Complex result;
        result.real = a.real - b.real;
        result.imag = a.imag - b.imag;
        return result;
    }

    // Function to multiply two complex numbers
    Complex multiply(Complex a, Complex b) {
        Complex result;
        result.real = a.real * b.real - a.imag * b.imag;
        result.imag = a.real * b.imag + a.imag * b.real;
        return result;
    }

    // Function to divide two complex numbers
    Complex divide(Complex a, Complex b) {
        Complex result;
        double divisor = b.real * b.real + b.imag * b.imag;
        result.real = (a.real * b.real + a.imag * b.imag) / divisor;
        result.imag = (a.imag * b.real - a.real * b.imag) / divisor;
        return result;
    }

    int main() {
        Complex num1 = {3, 4};  // Representing 3 + 4i
        Complex num2 = {2, -1}; // Representing 2 - i

        // Add
        Complex sum = add(num1, num2);
        printf("Sum: %.2f + %.2fi\n", sum.real, sum.imag);

        // Subtract
        Complex difference = subtract(num1, num2);
        printf("Difference: %.2f + %.2fi\n", difference.real, difference.imag);

        // Multiply
        Complex product = multiply(num1, num2);
        printf("Product: %.2f + %.2fi\n", product.real, product.imag);

        // Divide
        Complex quotient = divide(num1, num2);
        printf("Quotient: %.2f + %.2fi\n", quotient.real, quotient.imag);

        return 0;
    }




  6. A positive integer is entered through the keyboard. Write a function to obtain the prime factor of this number. For example, Prime factors of 24 are 2,2,2 and 3 where as prime factor of 35 are 5 and 7.

    #include <stdio.h>

    // Function to print prime factors of a number
    void primeFactors(int n) {
        // Print the number of 2s that divide n
        while (n % 2 == 0) {
            printf("2 ");
            n = n / 2;
        }
     
        // n must be odd at this point. So we can skip one element (i = i + 2)
        for (int i = 3; i * i <= n; i = i + 2) {
            // While i divides n, print i and divide n
            while (n % i == 0) {
                printf("%d ", i);
                n = n / i;
            }
        }
     
        // This condition is to handle the case when n is a prime number greater than 2
        if (n > 2)
            printf ("%d ", n);
    }
     
    // Driver program to test above function
    int main() {
        int n;
       
        // Input number from user
        printf("Enter a positive integer: ");
        scanf("%d", &n);
     
        printf("Prime factors of %d are: ", n);
        primeFactors(n);
       
        return 0;
    }




  7. Write a program to count the number of words in a sentence.

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

    #define MAX_LENGTH 100

    // Function to count the number of words in a sentence
    int countWords(char sentence[]) {
        int count = 0;
        int length = strlen(sentence);
        int i;

        // Traverse the sentence character by character
        for (i = 0; i < length; i++) {
            // Increment count if a space or the end of the sentence is encountered
            if (sentence[i] == ' ' || sentence[i] == '\0') {
                count++;
            }
        }

        return count;
    }

    int main() {
        char sentence[MAX_LENGTH];

        // Input the sentence
        printf("Enter a sentence: ");
        fgets(sentence, sizeof(sentence), stdin);

        // Count the number of words
        int words = countWords(sentence);

        // Print the result
        printf("Number of words in the sentence: %d\n", words);

        return 0;
    }






0 Comments had been done yet:

Post a Comment