Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • Arithmetic operations by using only one variable

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 530
    Comment on it

    Hey Guys,

    Today we are going to perform Arithmetic operations by using only one variable.

    It means, if we have define a single variable for loops or anything else then it must be used for all the operation.

    So, here, your only friend is "C" or "C++".

    Why "C" or "C++"? Because in these language one can manipulate even the address or say pointers.

    Lets do some programming:

    #include <stdio.h>
    #include <conio.h>

    well, these are header files (Everybody knows that)

    int main() {
    int *a;    // here is our little friend, by which we'll able to perform the operations.

    What is *a ?

    It is a format to define pointer variable. While "a" holds the memory address (Not any kind of Value).

    "*a" => holds the value in the memory address "a"

    print("Enter First Number: ");
    scanf("%d", a);

    Earlier, when input any variable through scanf

    1. The first thing we have to do is to tell what kind of data , the variable will hold , inside the inverted commas " "
    2. Then we are going to tell at which address it will going to store, followed by "&".

    But here we are not going to use "&" because our variable is a pointer variable and it will hold the memory address only.

    Now, we will take second input

    printf("Enter the Second Number: ");
    scanf("%d", (a+1));

    Now, what is that (a+1)

    It is a simple increment on the memory address corresponding to the size of the datatype.

    So the second value will be stored in the consecutive memory address.

    Now, we are going to display the Arithmetic operations

    print("Addition: %d", (*a)+*(a+1));
    print("Subtraction: %d", (*a)-*(a+1));
    print("Multiplication: %d", (*a)**(a+1));

    we will obviously get the result for Addition, subtraction and Multiplication but not division

    Why not division????

    because as we are going to write

    print("Division: %d", (*a)/*(a+1));

    the operator between (*a), *(a+1) is /. As Compiler will read the line, with single character.

    Then it will find a predefined operator while reading the above line which is /*. which is used for comments.

    So, At last we have only Addition, Subtraction and Division here

    #include <stdio.h>
    #include <conio.h>
    int main() {
    int *a;
    printf("Enter First Number: ");
    scanf("%d", a);
    printf("Enter the Second Number: ");
    scanf("%d", (a+1));
    printf("Addition: %d\n", (*a)+*(a+1));
    printf("Subtraction: %d\n", (*a)-*(a+1));
    printf("Multiplication: %d\n", (*a)**(a+1));
    return 0;

 0 Comment(s)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: