Calculator Application: Objective: Algorithm
Calculator Application: Objective: Algorithm
Calculator Application
Objective: Create a simple calculator that can add, subtract, multiply, and divide two numbers.
Algorithm:
Source Code:
#include <stdio.h>
switch (operator) {
case '/':
if (b != 0) return a / b;
else {
default:
return 0;
}
int main() {
char operator;
scanf("%f", &num1);
scanf("%f", &num2);
return 0;
}
2. Temperature Converter
Algorithm:
#include <stdio.h>
int main() {
float temperature;
char conversionType;
scanf("%f", &temperature);
} else {
return 0;
Algorithm:
#include <stdio.h>
arr[j + 1] = temp;
if (arr[i] == value) {
int main() {
printf("\n");
sortArray(numbers, size);
printf("\n");
int searchValue;
scanf("%d", &searchValue);
if (index != -1) {
} else {
return 0;
}
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;
acc->balance += amount;
printf("Insufficient funds!\n");
} else {
acc->balance -= amount;
int main() {
int choice;
float amount;
while (1) {
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%f", &amount);
deposit(&myAccount, amount);
break;
case 2:
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:
return 0;
Algorithm:
#include <stdio.h>
typedef struct {
char name[50];
float grades[5];
} Student;
float sum = 0;
sum += grades[i];
int main() {
Student student;
int numGrades = 5;
scanf("%s", student.name);
scanf("%f", &student.grades[i]);
Exercise 7-Recursion
1. Factorial Calculation
Algorithm:
#include <stdio.h>
int factorial(int n) {
if (n == 0) {
int main() {
int number;
if (number < 0) {
} else {
return 0;
2. Fibonacci Series
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) {
if (n == 1) {
return 1; // Base case
int main() {
int terms;
scanf("%d", &terms);
printf("\n");
return 0;
3. Power Calculation
Algorithm:
#include <stdio.h>
if (exponent == 0) {
if (exponent < 0) {
int main() {
double base;
int exponent;
scanf("%lf", &base);
scanf("%d", &exponent);
return 0;
}
4. Reverse a String
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>
// Swap characters
str[start] = str[end];
str[end] = temp;
// Recursive call
int main() {
char str[100];
return 0;
5. Sum of Digits
Algorithm:
#include <stdio.h>
int sumOfDigits(int n) {
if (n == 0) {
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
return 0;