- "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 ndouble 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 nprintf("Enter the value of x: ");scanf("%lf", &x);printf("Enter the value of n: ");scanf("%d", &n);// Calculate and print the resultdouble result = power(x, n);printf("%lf raised to the power %d is: %lf\n", x, n, result);return 0;}
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 yearbool isLeapYear(int year) {// Leap year conditionsif ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {return true;} else {return false;}}int main() {int year;// Input year from the userprintf("Enter a year: ");scanf("%d", &year);// Check if the year is a leap year using the functionif (isLeapYear(year)) {printf("%d is a leap year.\n", year);} else {printf("%d is not a leap year.\n", year);}return 0;}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 numeralvoid intToRoman(int num, char* roman) {// Define arrays for Roman numerals and their corresponding valuesstruct {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 numwhile (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 yearprintf("Enter a year: ");scanf("%d", &year);// Convert year to Roman numeralintToRoman(year, roman);// Output the Roman numeralprintf("Roman equivalent of %d is %s\n", year, roman);return 0;}Write a function to calculate the factorial value of any integer entered through the keyboard.
#include <stdio.h>// Function to calculate factorialunsigned long long factorial(int n) {unsigned long long fact = 1;// Calculate factorialfor (int i = 1; i <= n; ++i) {fact *= i;}return fact;}int main() {int num;// Input the number from the userprintf("Enter a non-negative integer to calculate its factorial: ");scanf("%d", &num);// Check if the number is non-negativeif (num < 0) {printf("Error: Factorial is not defined for negative numbers.\n");} else {// Calculate factorial using the functionunsigned long long result = factorial(num);printf("Factorial of %d = %llu\n", num, result);}return 0;}"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 numbertypedef struct {double real;double imag;} Complex;// Function to add two complex numbersComplex 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 numbersComplex 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 numbersComplex 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 numbersComplex 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 + 4iComplex num2 = {2, -1}; // Representing 2 - i// AddComplex sum = add(num1, num2);printf("Sum: %.2f + %.2fi\n", sum.real, sum.imag);// SubtractComplex difference = subtract(num1, num2);printf("Difference: %.2f + %.2fi\n", difference.real, difference.imag);// MultiplyComplex product = multiply(num1, num2);printf("Product: %.2f + %.2fi\n", product.real, product.imag);// DivideComplex quotient = divide(num1, num2);printf("Quotient: %.2f + %.2fi\n", quotient.real, quotient.imag);return 0;}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 numbervoid primeFactors(int n) {// Print the number of 2s that divide nwhile (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 nwhile (n % i == 0) {printf("%d ", i);n = n / i;}}// This condition is to handle the case when n is a prime number greater than 2if (n > 2)printf ("%d ", n);}// Driver program to test above functionint main() {int n;// Input number from userprintf("Enter a positive integer: ");scanf("%d", &n);printf("Prime factors of %d are: ", n);primeFactors(n);return 0;}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 sentenceint countWords(char sentence[]) {int count = 0;int length = strlen(sentence);int i;// Traverse the sentence character by characterfor (i = 0; i < length; i++) {// Increment count if a space or the end of the sentence is encounteredif (sentence[i] == ' ' || sentence[i] == '\0') {count++;}}return count;}int main() {char sentence[MAX_LENGTH];// Input the sentenceprintf("Enter a sentence: ");fgets(sentence, sizeof(sentence), stdin);// Count the number of wordsint words = countWords(sentence);// Print the resultprintf("Number of words in the sentence: %d\n", words);return 0;}
0 Comments had been done yet:
Post a Comment