0% found this document useful (0 votes)
8 views6 pages

OS (HandsOn Act Finals)

The document contains ten function-oriented C programs that perform various calculations. These include computing sums of sequences, areas of circles, conversions between units, and checking properties of numbers such as odd/even and leap years. Each program prompts the user for input and displays the results based on the specified computations.

Uploaded by

it.ivbbenzal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views6 pages

OS (HandsOn Act Finals)

The document contains ten function-oriented C programs that perform various calculations. These include computing sums of sequences, areas of circles, conversions between units, and checking properties of numbers such as odd/even and leap years. Each program prompts the user for input and displays the results based on the specified computations.

Uploaded by

it.ivbbenzal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

1.

Write a function-oriented program that will compute the sum of the sequence
numbers from 1 to N where N is a supplied value by the user.

#include <stdio.h>

int calculate_sum(int N) {
int sum = 0;
for (int i = 1; i <= N; i++) {
sum += i;
}
return sum;
}

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

if (N <= 0) {
printf("Please enter a positive number.\n");
} else {
int result = calculate_sum(N);
printf("The sum of numbers from 1 to %d is: %d\n", N, result);
}

return 0;
}

2. Write a function-oriented program that computes the total of the squares from 1
to N supplied by the user.

#include <stdio.h>

int calculate_square_sum(int N) {
int sum = 0;
for (int i = 1; i <= N; i++) {
sum += i * i;
}
return sum;
}

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

if (N <= 0) {
printf("Please enter a positive number.\n");
} else {
int result = calculate_square_sum(N);
printf("The sum of squares from 1 to %d is: %d\n", N, result);
}

return 0;
}
3. Write a function-oriented program to compute the area of the circle. Use this
formula

Area = PI * r^2. Where PI has a value of 3.1415.

#include <stdio.h>

#define PI 3.1415

double calculate_area(double r) {
return PI * r * r;
}

int main() {
double r;
printf("Enter the radius of the circle: ");
scanf("%lf", &r);

if (r <= 0) {
printf("Please enter a positive radius.\n");
} else {
double area = calculate_area(r);
printf("The area of the circle with radius %.2lf is: %.2lf\n", r, area);
}

return 0;
}

4. Write a function-oriented program that will ask the user to give five numbers
and then the program will compute the sum of the five numbers.

#include <stdio.h>

int calculate_sum(int num1, int num2, int num3, int num4, int num5) {
return num1 + num2 + num3 + num4 + num5;
}

int main() {
int num1, num2, num3, num4, num5;

printf("Enter five numbers:\n");


printf("Number 1: ");
scanf("%d", &num1);
printf("Number 2: ");
scanf("%d", &num2);
printf("Number 3: ");
scanf("%d", &num3);
printf("Number 4: ");
scanf("%d", &num4);
printf("Number 5: ");
scanf("%d", &num5);

int sum = calculate_sum(num1, num2, num3, num4, num5);

printf("The sum of the five numbers is: %d\n", sum);

return 0;
}

5. Write a function-oriented program that will convert the input inches to its
centimeter equivalent. 1 inch equals to 2.4 cms. Display the converted centimeters
values.

#include <stdio.h>

#define CONVERSION_FACTOR 2.4

float convert_inches_to_cm(float inches) {


return inches * CONVERSION_FACTOR;
}

int main() {
float inches;

printf("Enter the length in inches: ");


scanf("%f", &inches);

float centimeters = convert_inches_to_cm(inches);

printf("%.2f inches is equal to %.2f centimeters.\n", inches, centimeters);

return 0;
}

6. Write a function-oriented program that will ask the user to give two numbers and
then the program will solve the sum, product, difference and quotient, and display
the result on the screen.

#include <stdio.h>

double calculate_sum(double a, double b) {


return a + b;
}

double calculate_product(double a, double b) {


return a * b;
}

double calculate_difference(double a, double b) {


return a - b;
}
double calculate_quotient(double a, double b) {
if (b != 0) {
return a / b;
} else {
printf("Error: Division by zero is not allowed.\n");
return 0;
}
}

int main() {
double num1, num2;

printf("Enter two numbers:\n");


printf("Number 1: ");
scanf("%lf", &num1);
printf("Number 2: ");
scanf("%lf", &num2);

double sum = calculate_sum(num1, num2);


double product = calculate_product(num1, num2);
double difference = calculate_difference(num1, num2);
double quotient = calculate_quotient(num1, num2);

printf("\nResults:\n");
printf("Sum: %.2lf\n", sum);
printf("Product: %.2lf\n", product);
printf("Difference: %.2lf\n", difference);
if (num2 != 0) {
printf("Quotient: %.2lf\n", quotient);
}

return 0;
}

7. Write a function-oriented program that calculates the power value of a number.


The program will ask the user to give base number and exponent number and display
the result on the screen.

#include <stdio.h>

double calculate_power(double base, int exponent) {


double result = 1.0;
for (int i = 1; i <= exponent; i++) {
result *= base;
}
return result;
}

int main() {
double base;
int exponent;

printf("Enter the base number: ");


scanf("%lf", &base);
printf("Enter the exponent: ");
scanf("%d", &exponent);

double result = calculate_power(base, exponent);

printf("%.2lf raised to the power of %d is: %.2lf\n", base, exponent, result);

return 0;
}

8. Write a function-oriented program that will convert the input US Dollar (S)
value into its equivalent in Philippine Peso (PHP). Let us assume that one dollar
is equivalent to PHP 55.65 pesos.

#include <stdio.h>

#define EXCHANGE_RATE 55.65

double convert_usd_to_php(double usd) {


return usd * EXCHANGE_RATE;
}

int main() {
double usd;

printf("Enter the amount in US Dollars (USD): ");


scanf("%lf", &usd);

double php = convert_usd_to_php(usd);

printf("%.2lf USD is equal to %.2lf PHP.\n", usd, php);

return 0;
}

9. Write a function-oriented program that will ask the user to give a number and
then the program will check if the given number is an odd or even number and
display the result on the screen.

#include <stdio.h>

void check_odd_or_even(int number) {


if (number % 2 == 0) {
printf("%d is an even number.\n", number);
} else {
printf("%d is an odd number.\n", number);
}
}

int main() {
int number;

printf("Enter a number: ");


scanf("%d", &number);

check_odd_or_even(number);

return 0;
}

10. Write a function-oriented program that will ask the user to give a year and
then the program will determine if the given year by the user is a leap year or not
a leap year and display the result on the screen.

#include <stdio.h>

int is_leap_year(int year) {


if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
return 1;
} else {
return 0;
}
}

int main() {
int year;

printf("Enter a year: ");


scanf("%d", &year);

if (is_leap_year(year)) {
printf("%d is a leap year.\n", year);
} else {
printf("%d is not a leap year.\n", year);
}

return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy