Addition of 2 positive intgers
😕😕😕
Member • Apr 5, 2009
Hey is increment operator allowed?rohitvishnuhey can anyone add 2 positive integers without using any arithmetic operators. no use of even shift operators.
😕😕😕
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
Member • Apr 5, 2009
/*
* 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 =20PS: No ideas in any C, C++ 😔
Second Number =30
Sum of First Number and Second Number =50
Member • Apr 5, 2009
int add(int a, int b) {
int sum = a;
int i = 0;
for(i=0; i<b;i++) {
sum++;
}
return sum;
}The function add takes two integer a and b as input and return the sum of those integers.Member • Apr 17, 2009
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:
PS: No ideas in any C, C++ 😔
Thanks
Member • Apr 21, 2009
/* written by : Arvind
Language : C */
#include <stdio.h>
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));
}
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 <stdio.h>
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));
}
Alright lets explore the Logic behind the code:Member • Apr 21, 2009
Member • Apr 21, 2009
-Pradeeprohitvishnuhey can anyone add 2 positive integers without using any arithmetic operators. no use of even shift operators.
Member • May 7, 2009
Member • May 8, 2009
[B]
/* Written by: Arvind(slashfear)
Language: C++ */
#include<iostream>
#include<string>
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 : "<<add(i,j);
return 0;
}[/B]
Thanks rohit for the question that filled my head for long time!!!!😉