0% found this document useful (0 votes)
3 views3 pages

Outcome B Examples

The document provides examples of basic calculator programs written in C and Octave. Both examples prompt the user for input values, perform arithmetic operations such as addition, multiplication, and division, and display the results. Additionally, the programs include functions for converting characters to uppercase and printing strings.

Uploaded by

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

Outcome B Examples

The document provides examples of basic calculator programs written in C and Octave. Both examples prompt the user for input values, perform arithmetic operations such as addition, multiplication, and division, and display the results. Additionally, the programs include functions for converting characters to uppercase and printing strings.

Uploaded by

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

Outcome B examples

C programming

Example 1

Write a C program to model a basic calculator. Request data values from a user and perform
arithmetic operations on the data.

#include <stdio.h>

// Function declarations
int add(int a, int b);
float multiply(float x, float y);
double divide(double p, double q);
char to_uppercase(char ch);
void print_string(char str[]);

int main() {
// Declare variables of different data types
int int_var1, int_var2;
float float_var1, float_var2;
double double_var1, double_var2;
char char_var;
char str_var[50];

// Input values from the user


printf("Enter two integers: ");
scanf("%d %d", &int_var1, &int_var2);

printf("Enter two floats: ");


scanf("%f %f", &float_var1, &float_var2);

printf("Enter two doubles: ");


scanf("%lf %lf", &double_var1, &double_var2);

printf("Enter a character: ");


scanf(" %c", &char_var);

printf("Enter a string: ");


scanf("%s", str_var);

// Function calls and output results


printf("Addition result: %d\n", add(int_var1, int_var2));
printf("Multiplication result: %.2f\n", multiply(float_var1, float_var2));
//if (double_var2 != 0)
printf("Division result: %.5lf\n", divide(double_var1, double_var2));
/*else
printf("Cannot divide by zero!\n");*/

printf("Uppercase character: %c\n", to_uppercase(char_var));


printf("String entered: ");
print_string(str_var);
return 0;
}

// Function definitions
int add(int a, int b) {
return a + b;
}

float multiply(float x, float y) {


return x * y;
}

double divide(double p, double q) {


return p / q;
}

char to_uppercase(char ch) {


if (ch >= 'a' && ch <= 'z')
return ch - 32;
return ch;
}

void print_string(char str[]) {


printf("%s\n", str);
}

Octave

Example 2: Write an Octave function to perform basic calculations. Prompt the user for input
and display the result of the calculations.

function Example_Octave_Functions_1()
% Integer operation
int1 = input("Enter an integer: ");
int2 = input("Enter another integer: ");
int_result = add(int1, int2);
fprintf("Addition result: %d\n", int_result);

% Floating-point operation
float1 = input("Enter a float: ");
float2 = input("Enter another float: ");
float_result = multiply(float1, float2);
fprintf("Multiplication result: %.2f\n", float_result);

% Double operation
double1 = input("Enter a double: ");
double2 = input("Enter another double: ");
%if double2 ~= 0
double_result = divide(double1, double2);
fprintf("Division result: %.5f\n", double_result);
%else
% fprintf("Cannot divide by zero!\n");
%end

% Character operation
char_var = input("Enter a character: ", "s");
char_result = to_uppercase(char_var);
fprintf("Uppercase character: %s\n", char_result);

% String operation
str_var = input("Enter a string: ", "s");
print_string(str_var);
end

% Function definitions

% Addition function (integer)


function result = add(a, b)
result = a + b;
end

% Multiplication function (float)


function result = multiply(x, y)
result = x * y;
end

% Division function (double)


function result = divide(p, q)
result = p / q;
end

% Convert character to uppercase


function result = to_uppercase(ch)
if ischar(ch) && ch >= 'a' && ch <= 'z'
result = upper(ch);
else
result = ch;
end
end

% Print string function


function print_string(str)
fprintf("String entered: %s\n", str);
end

Call the main function in the command window:

Example_Octave_Functions_1

You might also like