Addition of 2 positive intgers
hey can anyone add 2 positive integers without using any arithmetic operators. no use of even shift operators.
πππ
πππ
Replies
-
shalini_goel14
Hey is increment operator allowed?rohitvishnuhey can anyone add 2 positive integers without using any arithmetic operators. no use of even shift operators.
πππ
If so then apply a for loop for smallest integer no. times and increment the other integer's value in it.
It will finally give you the value.
package src; public class AddOperation { public static void main(String[] args) { int a=10; int b=20; for(int i=0; i<10;i++){ b++; } System.out.println("Value of sum="+b) ; } }
Ouput :Value of sum=30 -
shalini_goel14Hey rohitvishnu,
One more way in Java, just got late in recalling this class BigDecimal (Amazing class -Play with it and have fun, its really Good) π
Check following program in Java
/* * AddingIntegersWithoutOperators.java * * Created on April 6, 2009, 9:59 AM * */ package myjava; import java.math.BigDecimal; /** * * @author shalinig */ public class AddingIntegersWithoutOperators { public static void main(String[] args){ BigDecimal firstNo=new BigDecimal(20); BigDecimal secondNo=new BigDecimal(30); BigDecimal sum; System.out.println("First Number ="+firstNo); System.out.println("Second Number ="+secondNo); sum=firstNo.add(secondNo); System.out.println("Sum of First Number and Second Number ="+sum); } }
Output:
First Number =20
PS: No ideas in any C, C++ π
Second Number =30
Sum of First Number and Second Number =50
Thanks -
pradeep_agrawalI feel the below code will do addition of two positive integers without using arithmetic or shift operators.
int add(int a, int b) { int sum = a; int i = 0; for(i=0; iThe function add takes two integer a and b as input and return the sum of those integers.
Note: The above code does not take care of conditions like overflow.
-Pradeep -
rohitvishnushalini_goel14Hey rohitvishnu,
One more way in Java, just got late in recalling this class BigDecimal (Amazing class -Play with it and have fun, its really Good) π
Check following program in Java
/* * AddingIntegersWithoutOperators.java * * Created on April 6, 2009, 9:59 AM * */ package myjava; import java.math.BigDecimal; /** * * @author shalinig */ public class AddingIntegersWithoutOperators { public static void main(String[] args){ BigDecimal firstNo=new BigDecimal(20); BigDecimal secondNo=new BigDecimal(30); BigDecimal sum; System.out.println("First Number ="+firstNo); System.out.println("Second Number ="+secondNo); sum=firstNo.add(secondNo); System.out.println("Sum of First Number and Second Number ="+sum); } }
Output:
PS: No ideas in any C, C++ π
Thanks
nice try but even the function will ultimately use + operator .
actually i havent found out the answer myself ..
i guess we have to use some string concepts
and no increment operators allowed -
slashfearHi Rohit,
Alright so adding two numbers with out using arithmetic operators and even the increment operators. Ok so this is how we do it:
/* written by : Arvind Language : C */ #include
int add(int a, int b) { if (!a) return b; else return add((a & b) << 1, a ^ b); } int main() { unsigned int a,b; printf("Enter the two numbers: \n"); scanf("%d",&a); scanf("%d",&b); printf("Sum is: %d",add(a,b)); }
And you can do the subtraction in the similar way just by adding the following function to the above code:
int sub(int a, int b) { return add(a, add(~b, 1)); }
and so the final program for performing addition as well as subtraction of two numbers is as shown below:
/* written by : Arvind Language : C */ #include
Alright lets explore the Logic behind the code:int add(int a, int b) { if (!a) return b; else return add((a & b) << 1, a ^ b); } int sub(int a, int b) { return add(a, add(~b, 1)); } int main() { unsigned int a,b; printf("Enter the two numbers: \n"); scanf("%d",&a); scanf("%d",&b); printf("Sum is: %d",add(a,b)); printf("\nSub is: %d",sub(a,b)); }
In digital electronics, an XOR gate is also known as a quarter adder. Basically an addition is performed on each individual bit, and the carry is discarded. All I'm doing here is applying the same concept.
sum = a ^ b;
If any bits match in position, then perform the addition on the current sum and the results of the carry.
In binary arithmetic, subtraction is simply adding the two's complement. The two's complement is simply one added to the one's complement. -
silverscorpionThat was fantastic. Good logic.. keep it up.
-
pradeep_agrawalThat's a piece of good code Slashfear.
But i thought the initial requirement was to not use even the shift operator while doing this.
-Pradeeprohitvishnuhey can anyone add 2 positive integers without using any arithmetic operators. no use of even shift operators. -
shalini_goel14@rohitvishu I guess you cannot do it using Strings, it will require "+" operator and append() will not add them. If you are able to do so, don't forget to share program here. π
-
slashfearHey guys,
This question made me scratch my head for a long time (as well as other CEans like shalini, pradeep, scorpion..... i thinkπ ) any way's finally i have found the solution guys Eureka..........π
Here is the program to add two numbers using a string class:
[B] /* Written by: Arvind(slashfear) Language: C++ */ #include
#include using namespace std; int add( int i, int j ) { std::string r( i, ' ' ) ; r.append( j, ' ' ) ; return r.size() ; } int main() { int i, j; cout<<"Enter two number: "; cin>>i; cin>>j; cout<<"The Sum of two numbers is : "< Thanks rohit for the question that filled my head for long time!!!!π
And let me know if this is what you where expecting.....;-)
-Arvind(Slashfear)
You are reading an archived discussion.
Related Posts
hi guys , i am coordinating an event in my collage which is web designing where the participants are supposed to design a dynamic website based on a theme given...
how to compile the servlets program?
what should i need for that ?
how to run servlet program?
how to download tomcat server(actually i visited their site but don't know...
A great site for electronics people is born today!!!
Go to
you'll be glad for this hint.
regards
π hello, i need a consultant (small and yet experienced) to then place my project within the capable hands of a consultant (large and yet still working
prototypes) to achieve...
Wishing CEan vinod12345madhu
πHaPpy Birthdayπ
May your dreams come true and you rock the world!
β