Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
35 views9 pages

1

The document contains 4 code snippets that demonstrate different C programming concepts: 1. A program that calculates factorials and strong numbers recursively. It prompts the user for a number and prints all strong numbers below it. 2. A program that dynamically allocates memory for two integers, gets user input, performs basic math operations on the numbers (add, subtract, multiply, divide), and frees the allocated memory. 3. A program that calculates the real roots of a quadratic equation given coefficients a, b, c, and prints the results. 4. A financial calculator that computes future or present value of an investment given inputs like principal, interest rate, and number of periods.

Uploaded by

khoa20033002
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)
35 views9 pages

1

The document contains 4 code snippets that demonstrate different C programming concepts: 1. A program that calculates factorials and strong numbers recursively. It prompts the user for a number and prints all strong numbers below it. 2. A program that dynamically allocates memory for two integers, gets user input, performs basic math operations on the numbers (add, subtract, multiply, divide), and frees the allocated memory. 3. A program that calculates the real roots of a quadratic equation given coefficients a, b, c, and prints the results. 4. A financial calculator that computes future or present value of an investment given inputs like principal, interest rate, and number of periods.

Uploaded by

khoa20033002
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/ 9

1.

#include <stdio.h>

int factorial(int n) {

if (n == 0 || n == 1) {

return 1;

} else {

return n * factorial(n - 1);

int isStrongNumber(int num) {

int originalNum = num;

int sum = 0;

while (num > 0) {

int digit = num % 10;

sum += factorial(digit);

num /= 10;

return (sum == originalNum);

int main() {

int n;

printf("Enter a number: ");

scanf("%d", &n);

printf("Result: ");
for (int i = 1; i < n; i++) {

if (isStrongNumber(i)) {

printf("%d ", i);

printf("\n");

return 0;

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

int main() {
int *num1 = (int *)malloc(sizeof(int));
int *num2 = (int *)malloc(sizeof(int));

if (num1 == NULL || num2 == NULL) {


return 1;
}

printf("Enter num1: ");


scanf("%d", num1);

printf("Enter num2: ");


scanf("%d", num2);

int sum = *num1 + *num2;


int difference = *num1 - *num2;
int multiply = *num1 * *num2;

char divideResult[100];
if (*num2 != 0) {
sprintf(divideResult, "%.2f", (float)*num1 / *num2);
} else {
strcpy(divideResult, "Error");
}
printf("Sum: %d\n", sum);
printf("Difference: %d\n", difference);
printf("Multiplication: %d\n", multiply);
printf("Division: %s\n", divideResult);
free(num1);
free(num2);
return 0;
}

3.
#include <stdio.h>
#include <math.h>

int roots(double a, double b, double c, double *root1,


double *root2) {
double discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
*root1 = (-b + sqrt(discriminant)) / (2 * a);
*root2 = (-b - sqrt(discriminant)) / (2 * a);
return 2;
} else if (discriminant == 0) {
*root1 = -b / (2 * a);
*root2 = *root1;
return 1;
} else {
return 0;
}
}

int main() {
double a, b, c, root1, root2;
printf("Enter a: ");
scanf("%lf", &a);
printf("Enter b: ");
scanf("%lf", &b);
printf("Enter c: ");
scanf("%lf", &c);
int numRoots = roots(a, b, c, &root1, &root2);
if (numRoots == 2) {
printf("Two found: %lf and %lf\n", root1, root2);
} else if (numRoots == 1) {
printf("One found: %lf\n", root1);
} else {
printf("Not found.\n");
}
return 0;
}

4.
#include <stdio.h>
#include <math.h>

double financialCalculation(double principal, double rate,


int periods, int isFutureValue) {
double result;

if (isFutureValue) {
result = principal * pow(1 + rate, periods);
} else {
result = principal / pow(1 + rate, periods);
}
return result;
}
int main() {
double principal, rate;
int periods;
char calculationType;

printf("Investment Calculator\n");
printf("====================\n");

printf("Principal: ");
scanf("%lf", &principal);

printf("Annual Rate: ");


scanf("%lf", &rate);

printf("No of Years: ");


scanf("%d", &periods);

printf("Future (f) or Present (p): ");


scanf(" %c", &calculationType);
calculationType = tolower(calculationType);

int isFutureValue;
if (calculationType == 'f') {
isFutureValue = 1;
} else if (calculationType == 'p') {
isFutureValue = 0;
} else {
printf("Invalid input");
return 1;
}
double result = financialCalculation(principal, rate,
periods, isFutureValue);
printf("The Present Amount: %.2lf\n", principal);
// Print the result
if (isFutureValue) {
printf("The Future Value: %.2lf\n", result);
} else {
printf("Present Value: %.2lf\n", result);
}

return 0;
}

You might also like