You are on page 1of 9

UNIVERSITY OF ENGINEERING AND

TECHNOLOGY, TAXILA

SRS Assignment 01

Submitted to:
Engr. Rabia Arshad
Submitted by:
Mohtashim Ali
18-SE-63

DEPARTMENT OF SOFTWARE ENGINEERING


1-Write a Program to create a dynamic array has N Elements. Get integer values
Into array and then provide the following functionality:
a: Print all even numbers available in the array.
b: Print all odd numbers available in the array.
c: Print all prime numbers available in the array.
d: Print all square numbers available in the array.

#include<iostream>
using namespace std;
int even();
int odd();
int prime();
int cqn();

int main()
{
A:
int charr;

cout << "1. Print Even numbers. "<< endl;


cout << "2. Print Odd numbers. "<< endl;
cout << "3. Print Prime numbers. "<< endl;
cout << "4. Print Complete square numbers. "<< endl;
cout << "5. EXIT "<< endl;

cout << "\n\nSelect from the MENU: ";


cin >> charr;

switch(charr){
case 1:
even();
cout<<endl<<endl<<endl;
break;
case 2:
odd();
cout<<endl<<endl<<endl;
break;
case 3:
prime();
cout<<endl<<endl<<endl;
break;
case 4:
cqn();
cout<<endl<<endl<<endl;
break;
default:
cout << "Invalid Command!!" << endl;
break;
}

goto A;
return 0;
}
int even()
{
int n,i,j;
int arr[n];
cout<<"Size of Array: ";
cin>>n;
cout<<"Enter any "<<n<<" elements in Array: ";
for(i=0; i<n;i++)
{
cin>>arr[i];
}
cout<<"\nEven numbers :\t";
for(int i=0; i<n;i++)
{
if(arr[i]%2==0)

{
cout << arr[i] << "\t";
}
}
}

int odd()
{
int n,i,j;
int arr[n];
cout<<"Size of Array: ";
cin>>n;
cout<<"Enter any "<<n<<" elements in Array: ";
for(i=0; i<n;i++)
{
cin>>arr[i];
}
cout<<"\nOdd numbers :\t";
for(int i=0 ; i<n ; i++)
{
if (arr[i] % 2 != 0)
{
cout << arr[i] << "\t";
}
}
}

int prime()
{
int n ,i,j;
int arr[n];
cout<<"Size of Array: ";
cin>>n;
cout<<"Enter any "<<n<<" elements in Array: ";
for(i=0; i<n;i++)
{
cin>>arr[i];
}
cout << "\nPrime numbers :\t";
for( j=0 ; j<n ; j++){

for(i=2;i<arr[j];i++)
if(arr[j]%i==0)
break;
if(arr[j]==i && arr[j]>1 )
cout << arr[j] << "\t";
}
}

int cqn()
{
int m,n ,i,j;
int arr[n];
cout<<"Size of Array: ";
cin>>n;
cout<<"Enter any "<<n<<" elements in Array: ";
for(i=0; i<n;i++)
{
cin>>arr[i];
}
cout << "\nComplete squares :\t";
for( j=0 ; j<n ; j++){
i=0;
m = i * i;
while (m < arr[j]){
i = i + 1;
m = i * i;
}
if (m == arr[j])
cout << arr[j] << "\t";
}
}
2-Write a c++ program to find duplicate values of an array of integer values.
#include<iostream>
using namespace std;
int main()
{
A:
int i, arr[20],j,n;

cout<<"Enter Size of Array: ";


cin>>n;
cout<<"Enter any "<<n<<" num in array: ";
for(i=0;i<n;i++)
{
cin>>arr[i];
}

cout<<"Duplicate Values are: ";


for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(arr[i]==arr[j] )
{
cout<<arr[i]<<" ";
}
}
}
cout<<endl;
int k;
cout<<"Restart program? (1 or 0) ";
cin>>k;
if (k==1)
{
goto A;
}
}

3-Write a c++ program to find the common elements between two arrays of integers.
#include<iostream>
using namespace std;
int main()
{
A:
int i, arr[20],j,n,m,num=0,arr1[20];

cout<<"Enter Size of ist Array: ";


cin>>n;
cout<<"Enter any "<<n<<" num in ist array: ";
for(i=0;i<n;i++)
{
cin>>arr[i];
}
cout<<"Enter Size of 2nd Array: ";
cin>>m;
cout<<"Enter any "<<m<<" num in 2nd array: ";
for(i=0;i<m;i++)
{
cin>>arr1[i];
}
cout<<"Common elements are: ";
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(arr[i]==arr1[j])
{
cout<<arr[i]<<" ";
}
}
}
cout<<endl;
int k;
cout<<"Restart program? (1 or 0) ";
cin>>k;
if (k==1)
{
goto A;
}

4-Write a c++ program to get the difference between the largest and smallest values in an array of
integers.
#include<iostream>
using namespace std;
int main()
{
int i,j,n, max , min , diff;
cout<<"Enter a size: ";
cin>>n;
int arr[n];
cout<<"Enter "<<n<<" Elements in array: ";
for(i=0;i<n;i++)
{
cin>>arr[i];
}
max=arr[0];
for(i=0;i<n;i++)
{
if(max<arr[i])

max=arr[i];
}
min=arr[0];
for(i=0;i<n;i++)
{
if(min>arr[i])

min=arr[i];
}
diff=max-min;
cout<<"\nDifference Between "<< max<<" and "<<min<<" is "<<diff<<endl;
}

You might also like