0% found this document useful (0 votes)
13 views

Homework 05

Uploaded by

adibabyaz0
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Homework 05

Uploaded by

adibabyaz0
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

2024

5th Homework
CSE115L

Farhan Mahtab
CSE115.3
2422232642
Question 1: Find the sum of the following series using
user-defined function: 1/1! + 2/2! + 3/3! + ...... +N/N!
Code:
Result:
Question 2: Write a C code using functions that takes
two integers: a and b as inputs and returns the value of
ab.
Code:

#include <stdio.h>

int multiply(int a, int b)

return a * b;

int main()

int a, b;

printf("Enter the First integer (a): ");

scanf("%d", &a);

printf("Enter the Second integer (b): ");

scanf("%d", &b);

int result = multiply(a, b);

printf("\nThe product of %d and %d is: %d\n", a, b, result);

return 0;

}
Result:
Question 3: Compute the sum of the following geometric
progression using a function with 2 parameters r and n:
1 + r + r^2 + ... + r^n
Code:

#include <stdio.h>

double geometric_sum(int r, int n)

double sum = 0;

double term = 1;

int i;

for (i = 0; i <= n; i++)

sum += term;

term *= r;

return sum;

int main()

int r, n;

double result;

printf("Enter the common ratio (r): ");

scanf("%d", &r);
printf("Enter the exponent (n): ");

scanf("%d", &n);

result = geometric_sum(r, n);

printf("\nThe sum of the geometric progression is: %.2f\n", result);

return 0;

Result:
Question 4: Write a C program that reads an integer and
returns the reverse of that number using function.
Code:

#include <stdio.h>

int reverseNumber(int num)

int reversed = 0, digit;

while (num != 0)

digit = num % 10;

reversed = reversed * 10 + digit;

num /= 10;

return reversed;

int main()

int number, reversed;

printf("Enter an integer: ");

scanf("%d", &number);

reversed = reverseNumber(number);
printf("\nThe reverse of %d is: %d\n", number, reversed);

return 0;

Result:
Question 5: Write a C program using function that reads a floating point number n
and an integer d and then prints the rounded value of n up to d decimal places.
Code:
#include <stdio.h>

#include <math.h>

double roundToDecimalPlaces(double n, int d)

double factor;

factor = pow(10, d);

return round(n * factor) / factor;

int main()

double n, rounded;

int d;

printf("Enter a floating-point number(n): ");

scanf("%lf", &n);

printf("Enter the number of decimal places to round to(d): ");

scanf("%d", &d);

rounded = roundToDecimalPlaces(n, d);

printf("\nThe rounded value of %f to %d decimal places is: %.*f\n", n, d, d, rounded);


return 0;

Result:

You might also like