Pages

Showing posts with label multiplication without * operator. Show all posts
Showing posts with label multiplication without * operator. Show all posts

Friday, July 5, 2013

Division using subtraction C++

/*
QUESTION:
WRITE A CODE for dividing two numbers but you can't use divide operator only operator that can be used for division in this question is subtraction operator i.e ' - ' 


CODE:
*/

#include <iostream>
using namespace std;
int main(){
 cout<<"\n\nEnter dividend: ";
 int a;
 cin>>a;
 cout<<"\n\nEnter divisor: ";
 int b;
 cin>>b;
 int quotient = 0;

 while(a>=b){
  a -= b;
  quotient ++;
 }

 cout<<"\nQuotient after division is: "<<quotient<<endl<<endl;

 return 0;
}

Saturday, June 29, 2013

Recursive function for multiplication of two numbers using addition in C++

/* QUESTION :

Write a function int Mul(int a,int b) that computes the product of two integers a and b. THE ONLY ARITHMETIC OPERATION THAT YOU ARE ALLOWED TO USE IS ADDITION '+' USING RECURSION
*/


// CODE :
#include <iostream>
using namespace std;

int Mul(int a,int b);
//MAIN STARTS
int main(){
int a,b;
cout<<"Enter first number : ";
cin>>a;


cout<<"\nEnter second number : ";
cin>>b;

cout<<"\n\nTHE RESULT AFTER MULTIPLICATION IS : "<<Mul(a,b)<<endl;
 return 0;
}

//MAIN ENDS
int Mul(int a , int b){
if (b==0)
return 0;

if (a==0)
return 0;

else
return (a + Mul(a,b-1));
}