Blogger templates

Searching...
Tuesday, March 19, 2013

Add two numbers in c without using plus operator

12:12 PM
How to add two numbers without using the plus operator in c

#include <stdio.h>

int main()
{
    int num1, num2;
    int summation;

    printf("Enter an integer (x): ");
    scanf("%d", &num1);
    printf("Enter an integer (y): ");
    scanf("%d", &num2);
    summation = num1 - ~num2 - 1;

    printf("\nSummation of %d and %d is: %d \n", num1, num2, summation);
    return 0;
}

2 comments:

  1. Here is another way to do that. Just use subtract operator. For Example:

    summation = num1 - (-num2) //dont need to use ~

    ReplyDelete
    Replies
    1. Exactly, but when you will subtract two numbers ~ is required. Or you have to use << Shifting operator.

      Delete
:) :)) ;(( :-) =)) ;( ;-( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ $-) (b) (f) x-) (k) (h) (c) cheer
Click to see the code!
To insert emoticon you must added at least one space before the code.