C++ Lab Assignment - 1
C++ Lab Assignment - 1
1.Takes two integer operands and one operator form the user, performs the operation and then
prints the result.
C code :
#include <iostream>
using namespace std;
int main()
int a,b,operation;
char ch;
cout<<"Enter any two integer operands : "<<endl;
cin>>a>>b;
do
{
cout<<" 1. Addition '+'"<<endl;
cout<<" 2. Substraction '-'"<<endl;
case 1:
cout<<a+b;
break;
case 2:
cout<<a-b;
break;
case 3:
cout<<a*b;
break;
case 4:
cout<<a/b;
break;
case 5:
cout<<a%b;
break;
default:
cout<<"\tInvalid Operation"<<endl;
cin>>ch;
}while(ch=='Y'||ch=='y');
OUTPUT :
2.Generate all the prime numbers between 1 and n, where n is a value supplied by the user.
C code
#include <iostream>
using namespace std;
int main()
{
int n,i,j,count;
cout<<"Enter n value : "<<endl;
cin>>n;
for(i=2;i<=n;i++)
{
count=0;
for(j=2;j<i;j++)
{
if (i%j==0)
{
count+=1;
}
}
if(count==0)
{
cout<<i<<endl;
}
OUTPUT
C code :
#include <iostream>
using namespace std;
int main()
{
int a[50],i,n,ele;
cout<<"Enter the size of the array : "<<endl;
cin>>n;
cout<<"Enter the elements in the array : "<<endl;
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout <<"The elements in the array are : ";
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
cout<<"Enter the element u want to search in the array :"<<endl;
cin>>ele;
for(i=0;i<n;i++)
{
if (a[i]==ele)
{
cout<<"Element is present in the array"<<endl;
break;
}
}
if(a[i]!=ele)
{
cout<<"Element is not present in the array"<<endl;
}
return 0;
}
OUTPUT
C code :
#include <iostream>
using namespace std;
int main()
{
int n,i,fact;
cout<<"Enter n value : ";
cin>>n;
fact=1;
if(n==1)
{
cout<<"The factorial is 1";
}
else
{
for(i=1;i<=n;i++)
{
fact=fact*i;
}
cout<<"The factorial of "<<n<<" is : "<<fact;
}
}
OUTPUT