0% found this document useful (0 votes)
41 views

C++ Lab Assignment - 1

Uploaded by

kondri tejaswini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

C++ Lab Assignment - 1

Uploaded by

kondri tejaswini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

ASSIGNMENT – 1

Name : K.Satya Prakash Roll no : AP21110011371

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;

cout<<" 3. Multiplication '*'"<<endl;

cout<<" 4. Division '/'"<<endl;


cout<<" 5. Modulous '%'"<<endl;

cout<<"Enter the number of operation u want to perform : "<<endl;


cin>>operation;
switch(operation)

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;

cout<<endl<<"Do you want to continue ?"<<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

3. Searching an element in an array.

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

4.To find the factorial of a given integer.

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

You might also like