0% found this document useful (0 votes)
11 views17 pages

Calculator Application: Objective: Algorithm

Ss
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)
11 views17 pages

Calculator Application: Objective: Algorithm

Ss
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/ 17

1.

Calculator Application

Objective: Create a simple calculator that can add, subtract, multiply, and divide two numbers.

Algorithm:

1. Define a function calculate that takes two numbers and an operator.


2. Use a switch statement to perform the operation based on the operator.
3. Return the result of the calculation.
4. In main, prompt the user for two numbers and an operator.
5. Call the calculate function and display the result.

Source Code:

#include <stdio.h>

float calculate(float a, float b, char operator) {

switch (operator) {

case '+': return a + b;

case '-': return a - b;

case '*': return a * b;

case '/':

if (b != 0) return a / b;

else {

printf("Error: Division by zero.\n");

return 0; // Return zero for error handling

default:

printf("Error: Invalid operator.\n");

return 0;

}
int main() {

float num1, num2;

char operator;

printf("Enter first number: ");

scanf("%f", &num1);

printf("Enter second number: ");

scanf("%f", &num2);

printf("Enter operator (+, -, *, /): ");

scanf(" %c", &operator);

float result = calculate(num1, num2, operator);

printf("Result: %.2f\n", result);

return 0;

}
2. Temperature Converter

Objective: Convert temperatures between Celsius and Fahrenheit using functions.

Algorithm:

1. Define two functions: celsiusToFahrenheit and fahrenheitToCelsius.


2. Each function will take a temperature value and return the converted value.
3. In main, prompt the user for a temperature and the conversion type.
4. Call the appropriate conversion function based on the user input.
5. Print the converted temperature.

#include <stdio.h>

float celsiusToFahrenheit(float celsius) {

return (celsius * 9/5) + 32;

float fahrenheitToCelsius(float fahrenheit) {

return (fahrenheit - 32) * 5/9;

int main() {

float temperature;

char conversionType;

printf("Enter temperature: ");

scanf("%f", &temperature);

printf("Convert to (C for Celsius, F for Fahrenheit): ");

scanf(" %c", &conversionType);


if (conversionType == 'F' || conversionType == 'f') {

float result = celsiusToFahrenheit(temperature);

printf("Converted Temperature: %.2f°F\n", result);

} else if (conversionType == 'C' || conversionType == 'c') {

float result = fahrenheitToCelsius(temperature);

printf("Converted Temperature: %.2f°C\n", result);

} else {

printf("Invalid conversion type.\n");

return 0;

3. Array Sorting and Searching

Objective: Sort an array and perform a linear search.

Algorithm:

1. Define a function sortArray to sort an array using bubble sort.


2. Define a function linearSearch to find an element in the sorted array.
3. In main, create an array of integers and prompt the user for a search value.
4. Call sortArray to sort the array.
5. Call linearSearch to find the value and print the result.

#include <stdio.h>

void sortArray(int arr[], int size) {


for (int i = 0; i < size - 1; i++) {

for (int j = 0; j < size - i - 1; j++) {

if (arr[j] > arr[j + 1]) {

int temp = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = temp;

int linearSearch(int arr[], int size, int value) {

for (int i = 0; i < size; i++) {

if (arr[i] == value) {

return i; // Return the index

return -1; // Return -1 if not found

int main() {

int numbers[] = {34, 7, 23, 32, 5, 62};

int size = sizeof(numbers) / sizeof(numbers[0]);

printf("Original Array: ");


for (int i = 0; i < size; i++) {

printf("%d ", numbers[i]);

printf("\n");

sortArray(numbers, size);

printf("Sorted Array: ");

for (int i = 0; i < size; i++) {

printf("%d ", numbers[i]);

printf("\n");

int searchValue;

printf("Enter a value to search: ");

scanf("%d", &searchValue);

int index = linearSearch(numbers, size, searchValue);

if (index != -1) {

printf("Value %d found at index %d.\n", searchValue, index);

} else {

printf("Value %d not found in the array.\n", searchValue);

return 0;
}

4. Banking System Simulation

Objective: Simulate a simple banking system with deposit and withdrawal functions.

Algorithm:

1. Define a structure Account with fields for account number and balance.
2. Define functions deposit and withdraw that operate on an Account pointer.
3. In main, create an Account instance and prompt the user for operations.
4. Call the appropriate function based on user input.
5. Print the updated account balance.

#include <stdio.h>

typedef struct {

int accountNumber;

float balance;

} Account;

void deposit(Account *acc, float amount) {

acc->balance += amount;

printf("Deposited: %.2f\n", amount);

void withdraw(Account *acc, float amount) {

if (amount > acc->balance) {

printf("Insufficient funds!\n");

} else {

acc->balance -= amount;

printf("Withdrawn: %.2f\n", amount);


}

int main() {

Account myAccount = {123456, 1000.0};

int choice;

float amount;

while (1) {

printf("\n1. Deposit\n2. Withdraw\n3. Check Balance\n4. Exit\n");

printf("Choose an option: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter deposit amount: ");

scanf("%f", &amount);

deposit(&myAccount, amount);

break;

case 2:

printf("Enter withdrawal amount: ");

scanf("%f", &amount);

withdraw(&myAccount, amount);

break;

case 3:
printf("Current Balance: %.2f\n", myAccount.balance);

break;

case 4:

printf("Exiting...\n");

return 0;

default:

printf("Invalid choice. Please try again.\n");

return 0;

5. Student Grade Management

Objective: Manage student grades by calculating average and determining pass/fail.

Algorithm:

1. Define a structure Student with fields for name and grades.


2. Define a function calculateAverage that takes an array of grades and its size.
3. In main, create a Student instance and prompt for grades.
4. Call calculateAverage and determine if the student has passed.
5. Print the student's average and pass/fail status.

#include <stdio.h>

typedef struct {

char name[50];

float grades[5];
} Student;

float calculateAverage(float grades[], int size) {

float sum = 0;

for (int i = 0; i < size; i++) {

sum += grades[i];

return sum / size;

int main() {

Student student;

int numGrades = 5;

printf("Enter student name: ");

scanf("%s", student.name);

for (int i = 0; i < numGrades; i++) {

printf("Enter grade %d: ", i + 1);

scanf("%f", &student.grades[i]);

float average = calculateAverage(student.grades, numGrades);

printf("Average Grade: %.2f\n", average);

printf("Status: %s\n", average >= 50 ? "Pass" : "Fail");


return 0;

Exercise 7-Recursion

1. Factorial Calculation

Objective: Calculate the factorial of a given number using recursion.

Algorithm:

1. Define a recursive function factorial that takes an integer as input.


2. If the input is 0, return 1 (base case).
3. Otherwise, return the input multiplied by the factorial of the input minus one (recursive
case).
4. In main, prompt the user for a number and call the factorial function.
5. Print the result.

#include <stdio.h>

int factorial(int n) {

if (n == 0) {

return 1; // Base case

return n * factorial(n - 1); // Recursive case

int main() {

int number;

printf("Enter a positive integer: ");


scanf("%d", &number);

if (number < 0) {

printf("Factorial is not defined for negative numbers.\n");

} else {

printf("Factorial of %d is %d\n", number, factorial(number));

return 0;

2. Fibonacci Series

Objective: Generate the Fibonacci series up to a given number using recursion.

Algorithm:

1. Define a recursive function fibonacci that returns the nth Fibonacci number.
2. If n is 0, return 0; if n is 1, return 1 (base cases).
3. Otherwise, return the sum of the (n-1)th and (n-2)th Fibonacci numbers (recursive case).
4. In main, prompt the user for the number of terms and call the fibonacci function in a
loop to print the series.

#include <stdio.h>

int fibonacci(int n) {

if (n == 0) {

return 0; // Base case

if (n == 1) {
return 1; // Base case

return fibonacci(n - 1) + fibonacci(n - 2); // Recursive case

int main() {

int terms;

printf("Enter the number of terms in Fibonacci series: ");

scanf("%d", &terms);

printf("Fibonacci Series: ");

for (int i = 0; i < terms; i++) {

printf("%d ", fibonacci(i));

printf("\n");

return 0;

3. Power Calculation

Objective: Calculate the power of a number using recursion.

Algorithm:

1. Define a recursive function power that takes a base and an exponent.


2. If the exponent is 0, return 1 (base case).
3. If the exponent is negative, return the reciprocal of the power of the base and the positive
exponent.
4. Otherwise, return the base multiplied by the power of the base and the exponent minus
one (recursive case).
5. In main, prompt the user for the base and exponent, then call the power function.

#include <stdio.h>

double power(double base, int exponent) {

if (exponent == 0) {

return 1; // Base case

if (exponent < 0) {

return 1 / power(base, -exponent); // Handle negative exponent

return base * power(base, exponent - 1); // Recursive case

int main() {

double base;

int exponent;

printf("Enter base: ");

scanf("%lf", &base);

printf("Enter exponent: ");

scanf("%d", &exponent);

printf("%.2f^%d = %.2f\n", base, exponent, power(base, exponent));

return 0;

}
4. Reverse a String

Objective: Reverse a string using recursion.

Algorithm:

1. Define a recursive function reverseString that takes a string and its length.
2. If the length is 0 or 1, return the string (base case).
3. Otherwise, concatenate the last character with the reversed substring (recursive case).
4. In main, prompt the user for a string and call the reverseString function.

#include <stdio.h>

#include <string.h>

void reverseString(char str[], int start, int end) {

if (start >= end) {

return; // Base case

// Swap characters

char temp = str[start];

str[start] = str[end];

str[end] = temp;

// Recursive call

reverseString(str, start + 1, end - 1);

int main() {

char str[100];

printf("Enter a string: ");

fgets(str, sizeof(str), stdin);


str[strcspn(str, "\n")] = 0; // Remove newline character

reverseString(str, 0, strlen(str) - 1);

printf("Reversed string: %s\n", str);

return 0;

5. Sum of Digits

Objective: Calculate the sum of the digits of a number using recursion.

Algorithm:

1. Define a recursive function sumOfDigits that takes an integer.


2. If the number is 0, return 0 (base case).
3. Otherwise, return the last digit plus the sum of the digits of the number divided by 10
(recursive case).
4. In main, prompt the user for a number and call the sumOfDigits function.

#include <stdio.h>

int sumOfDigits(int n) {

if (n == 0) {

return 0; // Base case

return (n % 10) + sumOfDigits(n / 10); // Recursive case

int main() {

int number;
printf("Enter a number: ");

scanf("%d", &number);

printf("Sum of digits: %d\n", sumOfDigits(number));

return 0;

You might also like