Showing posts with label multiply. Show all posts
Showing posts with label multiply. Show all posts

C / C++ Program to calculate element wise product of 2 arrays : element wise multiplication

Leave a Comment
/* The following C / C++ program require user to enter the elements of arrays having equal number of array elements. Equal number of array elements are required in this program. This program can be extended to any of the data type & to the 'n' number of arrays simultaneously*/

Code:
#include<iostream>
#include<conio.h>
using namespace std;
void main ()
{
int long a[100],b[100],c[100];
int d=0,n;
cout<<"Number of Elements in Arrays: ";
cin>>n;
cout<<"Enter For Array 1"<<endl;
while (d<n)
{
cout<<"Enter the element number "<<d+1<<" : ";
cin>>a[d];
d=d+1;
}
d=0;
cout<<"Enter For Array 2"<<endl;
while (d<n)
{
cout<<"Enter the element number "<<d+1<<" : ";
cin>>b[d];
d=d+1;
}
d=0;
while (d<n)
{
c[d]=a[d]*b[d];
d=d+1;
}
d=0;
cout<<"\nResult of element wise product of array elements:\n";
while (d<n)
{
cout<<c[d]<<"\t";
d=d+1;
}
getche();
}


Output

Output of C / C++ Program to calculate element wise product of 2 arrays : element wise multiplication
Output of C / C++ Program to calculate element wise product of 2 arrays : element wise multiplication
 
Read More...

C / C++ Program to Multiply two Numbers using BITWISE operations

1 comment
/* The following C / C++ code will be requiring two numbers from user as input. Then the program will perform bitwise AND till the second number doesn't become '0', first value is left shifted & 2nd value is right shifted in each iteration. Left Shifting multiplies by 2 for each loop where as Right Shifting divides by 2 for each loop*/
//CODE:-
 #include<stdio.h>
#include<stdlib.h>
void main()
{
int a,b,res;
printf("\nEnter the numbers:");

scanf("%d%d",&a,&b); // Assuming you entered a>b
res=0;
while(b != 0)
// Iterate the loop till b==0
{
if (b&01)
// Bitwise & of the value of b with 01
{
res=res+a;
// Add a to result if b is odd .
}
a<<=1;
// Left shifting the value contained in 'a' by 1
// multiplies a by 2 for each loop
b>>=1; // Right shifting the value contained in 'b' by 1.
}
printf("\nResult:%d\n",res);
system("pause");
}


Screenshot:
C / C++ Program to Multiply two Numbers using BITWISE operations
C / C++ Program to Multiply two Numbers using BITWISE operations Output

Read More...