Assignment#1 Solution

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 4

PRIME NUMBER

#include<iostream.h>
bool prime(int);
void main()
{
int r;
int a;

cout<<"Enter a number "<<endl;


cin>>a;
r=prime(a); /// function call

if(r==false)
cout<<"The number is not prime"<<endl;
else
cout<<"The number is prime"<<endl;

bool prime(int c)
{

for (int i=2;i<c;i++)


{
if(c%i==0)
return false;
}

return true;
}
FACTORIAL

#include<iostream.h>
int factorial(int);
void main()
{
int a;
cout<<"Enter a number: ";
cin>>a;
if (a<0)
cout<<"Factorial of zero or less than zero is not possiable"<<endl;
else
cout<<"The factorial of "<<a<<" is "<<factorial(a)<<endl;

int factorial(int a)
{
int fact=1;
for(int i=1;i<=a;i++)
fact=fact*i;
return fact;
}
AND, OR, XOR Operations
# include <iostream.h>
int main()
{
int a[10],b[10],c[10],choice;
while (choice!=-1)
{
cout<<"\n Which of the operations do you want to perform."<<endl;
cout<<"(1) AND GATE "<<endl;
cout<<"(2) OR GATE "<<endl;
cout<<"(3) XOR GATE "<<endl;
cout<<"Select the operation or press -1 to exit: ";
cin>>choice;
switch (choice)
{
case 1:
{

cout<<"Enter the first sequence of bits of size 5: "<<endl;


for(int j=0;j<5;j++)
cin>>a[j];
cout<<"Enter the second sequence of bits of size 5: "<<endl;
for(j=0;j<5;j++)
cin>>b[j];
cout<<"The output is: "<<endl;
for(int i=0;i<5;i++)
{
if(a[i]==1&&b[i]==1)
c[i]=1;
else
c[i]=0;
cout<<c[i];
}
cout<<endl;

break;
}
case 2:
{

cout<<"Enter the first sequence of bits of size 5: "<<endl;


for(int j=0;j<5;j++)
cin>>a[j];
cout<<"Enter the second sequence of bits of size 5: "<<endl;
for(j=0;j<5;j++)
cin>>b[j];
cout<<"The output is: "<<endl;
for(int i=0;i<5;i++)
{
if(a[i]==1||b[i]==1)
c[i]=1;
else
c[i]=0;
cout<<c[i];
}
cout<<endl;

break;
}
case 3:
{

cout<<"Enter the first sequence of bits of size 5: "<<endl;


for(int j=0;j<5;j++)
cin>>a[j];
cout<<"Enter the second sequence of bits of size 5: "<<endl;
for(j=0;j<5;j++)
cin>>b[j];
cout<<"The output is: "<<endl;
for(int i=0;i<5;i++)
{
if(a[i]==1&&b[i]==1)
c[i]=0;
else if(a[i]==0&&b[i]==0)
c[i]=0;
else
c[i]=1;
cout<<c[i];
}
cout<<endl;

}
}

}
return 0;
}

You might also like