Anda di halaman 1dari 5

Program to multiply two numbers with different algorithms.

Source Code:

#include<iostream>

#include<time.h>

using namespace std;

long MulMethod1(int a,int b){

long sum=0;

if(a%2!=0){

sum=sum+b;

while(a!=1){

a=a/2;

b=b*2;

if(a%2!=0){

sum=sum+b;

return sum;

long MulMethod2(int a,int b){

long sum=0;

long k=1;

while(a>0){

long x=(a%10)*k;

long y=b;

long z=1;

while(y>0){

sum=sum+x*(y%10)*z;
y=y/10;

z=z*10;

a=a/10;

k=k*10;

return sum;

intmain(){

long a,b,k,j;

clock_t t1;

cout<<"Enter the two numbers to be multiplied";

cin>>a>>b;

t1=clock();

k=a;

j=b;

for(inti=0;i<100000;i++){

MulMethod1(k,j);

k++;

j++;

cout<<"value from first multiplication is "<<MulMethod1(a,b)<<endl;

t1=clock()-t1;

double time_taken = ((double)t1)/CLOCKS_PER_SEC;

cout<<time_taken<<endl;

clock_t t2;

t2=clock();

//cout<<t2<<endl;

k=a;
j=b;

for(inti=0;i<100000;i++){

MulMethod2(k,j);

k++;

j++;

cout<<"value from Second multiplication is "<<MulMethod2(a,b)<<endl;

t2=double(clock()-t2);

//cout<<t2<<endl;

time_taken = ((double)t2)/CLOCKS_PER_SEC;

cout<<time_taken<<endl;

clock_t t3;

t3=clock();

cout<<t3<<endl;

k=a;

j=b;

for(inti=0;i<100000;i++){

a*b;

k++;

j++;

cout<<"value from third multiplication is "<<a*b<<endl;

double temp = clock() - t3;

t3=(double)temp;

cout<<t3<<endl;

time_taken = ((double)t3)/CLOCKS_PER_SEC;

cout<<time_taken<<endl;

return 0;

}
Output:

1
Program to find unique numbers from pairs

#include<iostream>

using namespace std;

intmain(){

inta[100],n,unique;

cout<<"enter number of elements:";

cin>>n;

for(inti=0;i<n;i++){

cin>>a[i];

unique=a[0];

for(inti=1;i<n;i++){

unique= unique^a[i];

cout<<"unique number is:"<<unique<<endl;

return 0;

Output: