Skip to main content
added 13 characters in body
Source Link
200_success
  • 145.7k
  • 22
  • 191
  • 481

So the last question I madeMy last question had a lot of views (over a 1000) so I decided to apply what I learned from the previous question and revamp the program. I learnt a lot from posting that question (functions, how to do a menu without using using a lot of printf and learning to not repeat code etc). So here's the updated program:

So the last question I made had a lot of views (over a 1000) so I decided to apply what I learned from the previous question and revamp the program. I learnt a lot from posting that question (functions, how to do a menu without using using a lot of printf and learning to not repeat code etc). So here's the updated program:

My last question had a lot of views (over a 1000) so I decided to apply what I learned from the previous question and revamp the program. I learnt a lot from posting that question (functions, how to do a menu without using using a lot of printf and learning to not repeat code etc). So here's the updated program:

Source Link
Daniel Morris
  • 323
  • 1
  • 2
  • 7

Simple calculator in C V2

So the last question I made had a lot of views (over a 1000) so I decided to apply what I learned from the previous question and revamp the program. I learnt a lot from posting that question (functions, how to do a menu without using using a lot of printf and learning to not repeat code etc). So here's the updated program:

#include <stdio.h>
#include <stdlib.h>

//Function prototype
void getData(int *,int *);
int addition(int,int);
int subtraction(int,int);
int multiplication(int,int);
int division(int,int);

//Main function
int main()
{
    do//Infinite do - While loop
    {
        int num1,num2,choice;

        printf("\nEnter a number from the list below\n\n"
               "\t1. Addition\n"
               "\t2. Subtraction\n"
               "\t3. Multiplication\n"
               "\t4. Division\n"
               "\t0. Quit program\n");

        printf("\nEnter number: ");
        scanf("%d",&choice);

        if (choice == 0)
        {
            puts("\nExiting program...");
            exit(0);
        }
        else
        {
            getData(&num1,&num2);//Get values for user input

            if(choice == 1)
            {
                printf("\n%d + %d = %d\n",num1,num2,addition(num1,num2));//Output addition
            }
            else if(choice == 2)
            {
                printf("\n%d - %d = %d\n",num1,num2,subtraction(num1,num2));//Output subtraction
            }
            else if(choice == 3)
            {
                printf("\n%d * %d = %d\n",num1,num2,multiplication(num1,num2));//Output multiplication
            }
            else if(choice == 4)
            {
                if(num2 == 0)
                {
                    printf("\nError! Division by zero! Exiting program\n");
                    exit(1);
                }
                else
                {
                    printf("\n%d / %d = %d\n",num1,num2,division(num1,num2));//Output division
                }
            }
            else
            {
                printf("Enter correct number! Exiting program");
                exit(1);
            }
        }
    }while(1);
}

//Get memory address of num1, num2 and store values in this location
void getData(int *num1_ptr,int *num2_ptr)
{
  printf("\nEnter first number: ");
  scanf("%d",num1_ptr);

  printf("\nEnter second number: ");
  scanf("%d",num2_ptr);
}

//Addition
int addition(num1,num2)
{
    return num1 + num2;
}

//Subtraction
int subtraction(int num1,int num2)
{
    return num1 - num2;
}

//Multiplication
int multiplication(int num1,int num2)
{
    return num1 * num2;
}

//Division
int division(int num1,int num2)
{
    return num1 / num2;
}
//End of code