C Program for Addition, Subtraction, Multiplication, Division

addition_subtraction_multiplication_division

C program for addition, subtraction, multiplication and division has been shown here. These are the basic arithmetic operations. The following section also covers the algorithm, pseudocode and time complexity of the program.






1. Algorithm to Perform Addition, Subtraction, Multiplication & Division


1. Take two numbers a and b as input.

2. Compute w = a + b

3. Compute x = a - b

4. Compute y = a * b

5. Compute z = a / b

6. Display w, x, y, z




2. Pseudocode to Perform Addition, Subtraction, Multiplication & Division


Input : Two numbers $a$ and $b$

Output : Value of $(a+b)$, $(a-b)$, $(a*b)$, $(a/b)$

1. Procedure compute($a$, $b$):

2. $w \leftarrow a + b$

3. $x \leftarrow a - b$

4. $y \leftarrow a * b$

5. $z \leftarrow a / b$

6. Print $w, x, y, z$

7. End Procedure





3. Time complexity to Perform Addition, Subtraction, Multiplication & Division


Time Complexity: O(1)

Addition, subtraction, multiplication, division are constant operations.




4. C Program & output to Perform Addition, Subtraction, Multiplication & Division of Integers

Code has been copied
/********************************************
    	alphabetacoder.com
 C program for addition, subtraction,
 multiplication and division of two integers
**********************************************/

#include<stdio.h>

int main() {
    // declare variables
    int a, b, w, x, y, z;

    // take input
    printf("Enter first number: ");
    scanf("%d", & a);
    printf("Enter second number: ");
    scanf("%d", & b);

    // perform operations
    w = a + b;
    x = a - b;
    y = a * b;
    z = a / b;

    // display result
    printf("Addition: %d\n", w);
    printf("Subtraction: %d\n", x);
    printf("Multiplication: %d\n", y);
    printf("Division: %d\n", z);

    return 0;
}

Output


Enter first number: 6

Enter second number: 4

Addition: 10

Subtraction: 2

Multiplication: 24

Division: 1





5. C Program & output to Perform Addition, Subtraction, Multiplication & Division of floating-point numbers

Code has been copied
/*****************************************************************
                   alphabetacoder.com
 C program for addition, subtraction, multiplication and division 
*****************************************************************/

#include<stdio.h>

int main(){
    // declare variables
    double a, b, w, x, y, z;
	
    // take input
    printf("Enter first number: ");
    scanf("%lf",&a);
    printf("Enter second number: ");
    scanf("%lf",&b);
	
    // compute operations
    w = a + b;
    x = a - b;
    y = a * b;
    z = a / b;
	
    // display result
    printf("Addition: %lf\n", w);
    printf("Subtraction: %lf\n", x);
    printf("Multiplication: %lf\n", y);
    printf("Division: %lf\n", z);
	
    return 0;
}

Output


Enter first number: 4.6

Enter second number: 2.2

Addition: 6.800000

Subtraction: 2.400000

Multiplication: 10.120000

Division: 2.090909





6. C Program & output to Perform Addition, Subtraction, Multiplication & Division using pointers

Code has been copied
/********************************************************************************
                   alphabetacoder.com
 C program for addition, subtraction, multiplication and division using pointers 
********************************************************************************/

#include<stdio.h>

int main() {
    // declare variables
    double a, b, w, x, y, z, * n1, * n2;

    // take input
    printf("Enter first number: ");
    scanf("%lf", & a);
    printf("Enter second number: ");
    scanf("%lf", & b);

    //store the address in pointer
    n1 = & a;
    n2 = & b;

    // compute operations
    w = * n1 + * n2;
    x = * n1 - * n2;
    y = * n1 * * n2;
    z = * n1 / * n2;

    // display result
    printf("Addition: %lf\n", w);
    printf("Subtraction: %lf\n", x);
    printf("Multiplication: %lf\n", y);
    printf("Division: %lf\n", z);

    return 0;
}

Output


Enter first number: 4

Enter second number: 6

Addition: 10.000000

Subtraction: -2.000000

Multiplication: 24.000000

Division: 0.666667