Russian Peasant Multiplication Algorithm, Exercises of C programming

This c++ program implements the russian peasant multiplication algorithm, a method for multiplying two numbers without using the standard multiplication operator. The user inputs two numbers, and the algorithm prints the product and the time taken to execute.

Typology: Exercises

2017/2018

Uploaded on 02/20/2018

gaurang-savaliya
gaurang-savaliya 🇭🇰

1 document

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
#include<bits/stdc++.h>
#include<iostream>
using namespace std;
int main()
{
int i,val1,val2;
int sum=0;
cout<<endl<<"Enter Value 1 : ";
cin>>val1;
cout<<endl<<"Enter Value 2 : ";
cin>>val2;
clock_t time;
time=clock();
while(val1>=1)
{
cout<<val1<<" "<<val2<<endl;
if(val1%2!=0)
{
sum=sum+val2;
}
val1=val1/2;
val2=val2*2;
}
cout<<endl<<"Multiplication is : "<<sum;
time=clock() - time;
printf("\n\ntotal time taken by RussianPeasant : %f\n",((float)time)/CLOCKS_PER_SEC);
}

Partial preview of the text

Download Russian Peasant Multiplication Algorithm and more Exercises C programming in PDF only on Docsity!

#include<bits/stdc++.h> #include using namespace std; int main() { int i,val1,val2; int sum=0; cout<<endl<<"Enter Value 1 : "; cin>>val1; cout<<endl<<"Enter Value 2 : "; cin>>val2; clock_t time; time=clock(); while(val1>=1) { cout<<val1<<" "<<val2<<endl; if(val1%2!=0) { sum=sum+val2; } val1=val1/2; val2=val2*2; } cout<<endl<<"Multiplication is : "<<sum; time=clock() - time; printf("\n\ntotal time taken by RussianPeasant : %f\n",((float)time)/CLOCKS_PER_SEC); }