simple calculator in c
simple calculator in c
#include <stdio.h> ...........................................................................this employs a standard input and output library to the code.
int product();
int division(); ............................ this is a declaration section that declares the functions that are available in the code.
int difference();
int square();
int num1, num2, result; ..............................................................................................These are variables for storage in the sum function.
printf ("enter the first number: "); ................................ ...........................................request user inputs
printf("enter the second number: "); ............................................request user second number input
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 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.
printf("enter the second number: "); ...........................................prompts the user to enter the second number.
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 num1, num2, result; .............................................................................................variables for storage in the product function.
printf ("enter the first number: "); ......................................................................prompts the user to enter the first number.
printf("enter the second number: "); ........................................................................prompts the user to enter the second number.
printf("the product of %d and %d is:%d\n", num1, num2, result); ................. displays the result of the multiplication of the two numbers.
int num1, num2, result; ................................................................................................variables for storage in the division function
printf("enter the first number: "); ............................................................................prompts the user to enter the second number
printf("enter the second number: "); ...........................................................................prompts the user to enter the second number
printf("the division of %d by %d is : %d\n", num1, num2, result ); ...........................outputs the result of the division function
printf("enter the number to square: "); ........................................................prompts the user the enter the number to be squared
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
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("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.
if( choice== 1 ){
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
}else if ( choice==4){
result=difference();
}else if (choice==5){
result=square();
}else if(choice==6){
}else{ if the user enters anything else apart from number 1 to 5 for the functions,
return 0; .........................................................indicates that the program has successfully completed its execution