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

simple calculator in c

The document outlines a simple calculator program written in C, which includes functions for sum, difference, product, division, and square operations. Each function prompts the user for input, performs the respective calculation, and outputs the result. The main function provides a user interface to select operations and execute them in a loop until the user chooses to exit.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

simple calculator in c

The document outlines a simple calculator program written in C, which includes functions for sum, difference, product, division, and square operations. Each function prompts the user for input, performs the respective calculation, and outputs the result. The main function provides a user interface to select operations and execute them in a loop until the user chooses to exit.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

COM/M/0105/2024 MESHEK KIPCHIRCHIR BCS 104 SIMPLE CALCULATOR IN C

#include <stdio.h> ...........................................................................this employs a standard input and output library to the code.

int sum ();

int product();

int division(); ............................ this is a declaration section that declares the functions that are available in the code.

int difference();

int square();

int sum(){ ...................................................................................................................this is the definition of the sum function.

int num1, num2, result; ..............................................................................................These are variables for storage in the sum function.

printf ("enter the first number: "); ................................ ...........................................request user inputs

scanf("%d",&num1); ............................... ....................................store the inputted number

printf("enter the second number: "); ............................................request user second number input

scanf("%d",&num2); .............................store the inputted number.

result=num1+num2; ..................................calculates the sum of num1 and num2

printf("the sum of %d and %d is: %d\n",num1, num2, result); ................................outputs the result of the 2 numbers entered by the user.

return result; ......................................... .........................returns the result of the sum to the calling function.

int difference(){ ........................................................defines the difference function.

int num1, num2, result; ....................................these are variables for storage in the difference function.

printf("enter the first number: "); ..............................prompts the user to enter the first number.

scanf("%d",&num1); .....................................................takes and stores the imputed number above as num.

printf("enter the second number: "); ...........................................prompts the user to enter the second number.

scanf("%d",&num2); .............................................................................stores the inputted number above as num2

result=num1-num2; ....................................................................actual operation of the function(subtracts num2 from num1).

printf("the difference of %d and %d is:%d\n",num1, num2, result); ......this displays the result due difference function.

return result; ..................................................................................returns the result of the difference to the calling function.

int product(){ ....................................................................definition of the product function

int num1, num2, result; .............................................................................................variables for storage in the product function.

printf ("enter the first number: "); ......................................................................prompts the user to enter the first number.

scanf("%d”, &num1); ...................................................................................................stores the entered number as num1

printf("enter the second number: "); ........................................................................prompts the user to enter the second number.

scanf("%d",&num2); ...............................................................................................stores the second number as num2.

result=num1 * num2; ........................................................................indicates the multiplication of num1 with num2 as a result

printf("the product of %d and %d is:%d\n", num1, num2, result); ................. displays the result of the multiplication of the two numbers.

return result; .................................................................................................................returns the result of product to calling function.

int division () { .......................................................................................................................definition of division function

int num1, num2, result; ................................................................................................variables for storage in the division function

printf("enter the first number: "); ............................................................................prompts the user to enter the second number

scanf("%d",&num1); ..................................................................................................stores the first number as num1

printf("enter the second number: "); ...........................................................................prompts the user to enter the second number

scanf("%d",&num2); .................................................................................................................stores the second number as num2

result=num1/num2; ...........................................................................................indicates the division of num1 by num2 as a result

MESHEK KIPCHIRCHIR COM/M/0105/2024


COM/M/0105/2024 MESHEK KIPCHIRCHIR BCS 104 SIMPLE CALCULATOR IN C

printf("the division of %d by %d is : %d\n", num1, num2, result ); ...........................outputs the result of the division function

return result; ...............................................................................................returns the result of division to the calling function.

int square (){ ......................................................................................................definition of the square function.

int num1, result; .....................................................................................variables for storage in the square function.

printf("enter the number to square: "); ........................................................prompts the user the enter the number to be squared

scanf("%d",&num1); ..........................................................................................stores the number as num1

result= num1 * num1; .................................indicates that the square of a number is the product of itself and store it as result.

printf ("the square of %d is : %d\n", num1, result); ............................................................outputs the result of the squared number

return result; .................................................................................................returns the result of square to the calling function

int main() { ............................................................................................................................this defines the main function.

int choice, result; ............................................................................................................variables for storage in the main function

while(1){ ........................................................................THIS IS A LOOP FUNCTION

printf("THIS IS A SIMPLE CALCULATOR IN C \n"); ..................this displays that its is a simple calculator that is running

printf("Available operations\n"); ...................................................display of a line that introduces the defined functions available.

printf("1. sum\n");

printf("2. product\n");

printf("3. division\n"); .................................displays the available functions.

printf("4. difference\n");

printf("5. square\n");

printf(“6. Exit\n”);

printf("choose one of the above functions 1-5: "); .........................prompts the user to chose the number of function to operate on.

scanf("%d",&choice); ................................................................................stores the number of function the user chose.

if( choice== 1 ){

result= sum ();

}else if( choice== 2){

result= product();

}else if( choice == 3){ this tells the program that if the user chose 1, 2, 3, 4, 5 then he/she means to operate

result= division(); on sum, product, division, difference or square, respectively

}else if ( choice==4){

result=difference();

}else if (choice==5){

result=square();

}else if(choice==6){

print (“exiting program, goodbye.”)

break; ............................................................TERMINATES THE PROGRAM WHEN THE USER CHOOSES TO EXIT

}else{ if the user enters anything else apart from number 1 to 5 for the functions,

printf("invalid function"); then it will display that the function is invalid.

return 0; .........................................................indicates that the program has successfully completed its execution

MESHEK KIPCHIRCHIR COM/M/0105/2024

You might also like