Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Taib Abdullah

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 17

PMAS-Arid AgricultureUniversityRawalpindi

University Institute of Information Technology

Programming fundamental
Course Title & Code:----------------------------------------------------------------

Assignment# LAB 5,6,7


-----------------------------------------------------------------------------
TAIB ABDULLAH
Student Name, Arid# & Class: --------------------------------------------------
BSCS (1C)
---------------------------------------------------------------------------------------------------

Dr .Habib
Submitted To: --------------------------------------------------------------------------
18/11/2022
Submission Date: ---------------------------------------------------------------------

For Official Use:

Total Marks: -------------------------------------------------------------------------------

Obtained Marks: -------------------------------------------------------------------------


//lab 5(loops)
1://find perfect numbers between 1 to 500
#include <iostream>
using namespace std;
int main() {
cout<< "Find the perfect numbers between 1 and 500"<<endl;
int i = 1, u = 1, sum = 0;
cout<< "\n The perfect numbers between 1 to 500 are: \n";
while (i <= 500) {
while (u <= 500) {
if (u < i) {
if (i % u == 0)
sum = sum + u;
}
u++;
}
if (sum == i) {
cout<< i << " " << "\n";
}
i++;
u = 1;
sum = 0;
}

}
(output)

2://check wether a number is prime or not


#include<iostream>
using namespace std;
int main(){
intnum,i,check=0;
cout<<"enter any number";
cin>>num;
for(i=1;i<=num;i++)
{
if(num%i==0)
{
check++;
}
}
if(check==2)
{
cout<<"the number is prime";
}
else
{
cout<<"the number is not prime";
}
}
(output)

3://wether a num can be express as sum of two numbers


#include <iostream>
using namespace std;
int main(){
T4Tutorials:
intnum;
int i;
boolisPrime = true;
cout<< "Enter a positive integer: ";
cin>>num;
if (num>0)
//to ensure the length of number is at least 2
{
for(i = 2; i <= num / 2; ++i)
{
if(num % i == 0)
{
isPrime = false;
break;
}
}
if (isPrime)
cout<< "This is a prime number";
else
cout<< "This is not a prime number";
}
return 0;
}
(output)

4://when value is 4 jump directly to next


#include<iostream>
using namespace std;
int main(){
int i;
for(i=1;i<=4;i++)
{
while(i==4)
break;
cout<<i<<endl;
}
cout<<"end of the loop";
}
(output)
5://draw pattern acc to number which user enter
#include <iostream>
using namespace std;

int main()
{
int space, rows;
char h;
cout<<"Enter number which you want to print"<<endl;
cin>>h;

cout<<"Enter number of rows: "<<endl;


cin>> rows;

for(int i = 1, k = 0; i <= rows; ++i, k = 0)


{
for(space = 1; space <= rows-i; ++space)
{
cout<<" ";
}

while(k != 2*i-1)
{
cout<<h;
++k;
}
cout<<endl;
}
return 0;
}
(output)
6://program that show the arrithemetic average of N grades
#include<iostream>
using namespace std;
int main()
{
int n, i;
float number, sum=0, arithmetic_average; //calculating the arithmetic average.
cout<<"Enter numbers to check their arithmetic average?"<<endl;
cin>>n;
cout<<"Enter "<<n<<" Number: "<<endl;
for(i=0; i<n; i++)
{
cin>>number;
sum = sum+number;
}
arithmetic_average= sum/n;
cout<<"Arithmetic Average = "<<arithmetic_average;
cout<<endl;
return 0;
}
(output)

ARRAYS
1://program that ask user to enter numbers of hours that six employes work nd store values in
array
#include<iostream>
using namespace std;
int main(){
int hours[6];
for(int e=1;e<=6;e++)
{
cout<<"working hour of "<<e<<endl;
cin>>hours[6];
}
for(int i=0;i<6;i++){
cout<<"passing from array"<<endl;
cin>>hours[i];
cout<<endl;
}
return 0;
}

2://ask user to enter 10 integers of an array the program must compute nd write how many
intergers are greater than or equal to 10
#include<iostream>
using namespace std;
int main()
{
int a[10];
int count =0;
cout<<"Enter 10 integers of an array :"<<endl;
for(int i=0;i<10;i++)
{
cin>>a[i];
if(a[i]>=10)
{
count++;
}
}
cout<<"There are "<<count<<" integers which are greater than or equal to 10."<<endl;
}

(output)
3://to swap 1st and last element of an array
for(int i = 0;i<n;i++){
cin>>arr[i]; #include<iostream>
using namespace std;

int main()
{
int n, temp;
cout<<"Enter Number of elements you want to enter :: ";
cin>>n;
intarr[n];

}
temp = arr[0];
arr[0] = arr[n-1];
arr[n-1] = temp;
cout<<"Array after swapping first and last elements "<<endl;
for(int i = 0;i<n;i++){
cout<<arr[i]<<"\t";
}
return 0;
}

4://input 5 numbers nd find their sum and average


#include<iostream>
using namespace std;
int main(){
intnum, array[num];
int sum=0, avg;
cout<< "Enter number of elements: ";
cin>>num;
for(int i=0;i<num;i++) {
cout<< "Enter Value " << i+1 << ":\t";
cin>> array[i];
}
for(int k=0;k<num;k++) {

sum+= array[k];

cout<<"Sum of " <<num<<" numbers is: "<<sum<<endl;

avg=sum/num;
cout<< "Average of "<<num<< " numbers is: "<<avg;
}

5://to reverse the perfect numbers in array


#include<iostream>
using namespace std;
int main(){
intarr[4]={6,28,496,8128};
int i;
for(i=0;i<4;i++)
cout<<"The perfect numbers in array are: "<<arr[i]<<endl;
int m, n,temp;
for ( m = 0, n = 4 - 1; m < 4/2; m++, n--) {
temp = arr[m];
arr[m] = arr[n];
arr[n] = temp; }
cout<< "\n Reverse of the array of perfect numbers is : " <<endl;
for ( m = 0; m < 4; m++)
{
cout<<arr[m] << " ";
}
}

6://to display even and odd of n value


#include <iostream>
using namespace std;
int main()
{
intarr[10],even[10],odd[10],evncnt=0,oddcnt=0,i;
cout<<"Input numbers in the array";
for(i=0;i<10;i++)
cin>>arr[i];
for(i=0;i<10;i++)
{
if(arr[i]%2==0)
even[evncnt++]=arr[i];
else
odd[oddcnt++]=arr[i];
}
cout<<"The even numbers are: ";
for(i=0;i<evncnt;i++)
cout<<even[i]<<" ";
cout<<"\nThe odd numbers are: ";
for(i=0;i<oddcnt;i++)
cout<<odd[i]<<" ";
}
(OUTPUT)

7://to find the largest 3 elements of an array


#include <iostream>
using namespace std;
voidlargest_nums(intarr[], intarr_size){
int max, max2, max3;
max3 = max = max2 = arr[0];
for(int i = 0; i <arr_size; i++){
if (arr[i] > max){
max3 = max2;
max2 = max;
max = arr[i];
}
else if (arr[i] > max2){
max3 = max2;
max2 = arr[i];
}
else if (arr[i] > max3)
max3 = arr[i];
}
cout<<endl<<"Three largest elements of the array are "<<max<<", "<<max2<<", "<<max3;
}
int main(){
int v;
cout<<"Enter size of array: "<<endl;
cin>>v;
cout<<endl;
intarr[v];
for(int m=0; m<v; m++){
cout<<"Enter elemnt [ "<<m+1<<"] :\t";
cin>>arr[m];}
int n = sizeof(arr) / sizeof(arr[0]);
cout<<"The array is : ";
for(int i = 0; i < n; i++)
cout<<arr[i]<<"\t";
largest_nums(arr, n);
return 0;
}

(OUTPUT)

LAB///07

Write a C++ program which declares two 3 x 3int arrays (Matrices). Store random values using
nested for loops in array and perform Addition and Multiplication of Matrices, stores the result
of operation in another Matrix (output arrays). And displays the results.
#include<iostream>
using namespace std;

int main(){
int arr1[3][3],arr2[3][3],arr_sum[3][3],arr_prod[3][3],i,j,k;

// First array

cout<<"Enter elements of first matrix: "<<endl;


for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
cout<<"Enter element a "<<i+1<<" "<<j+1<<" : ";
cin>>arr1[i][j];
}
cout<<"\n";

// Second array

cout<<"Enter elements of second matrix: "<<endl;


for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
cout<<"Enter element b "<<i+1<<" "<<j+1<<" : ";
cin>>arr2[i][j];
}

cout<<"\n";

// Display first array


cout<<"Matrix 1"<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<" "<<arr1[i][j];
}
cout<<endl;
}

cout<<"\n";

// Display second array


cout<<"Matrix 2"<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<" "<<arr2[i][j];
}
cout<<endl;
}

cout<<"\n";

// Adding both arrays


for(i=0;i<3;i++)
for(j=0;j<3;j++)
arr_sum[i][j]=arr1[i][j]+arr2[i][j];

// Displaying addition
cout<<"Result of addition of both matrices: "<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<" "<<arr_sum[i][j];
}
cout<<endl;
}

cout<<"\n";

// Initializing elements of matrix mult to 0.


for(i = 0; i < 3; ++i)
for(j = 0; j < 3; ++j)
{
arr_prod[i][j]=0;
}

// Multiplying both arrays


for(i=0;i<3;i++)
for(j=0;j<3;j++)
for(k=0;k<3;k++)
{
arr_prod[i][j] =arr_prod[i][j]+arr1[i][k]*arr2[k][j];
}

// Displaying Multiplication
cout<<"Result of Mulitplication of both matrices: "<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<" "<<arr_prod[i][j];
}
cout<<endl;
}

}
(output)

Q.2 WRITE A PROGRAM SEARCH THOURGH THE ARRAY AND FIND MAXIMUM
AND MINIMUM VALUE
#include<iostream>
#include <cstdlib>
using namespace std;
int main(){
int arr[10][10],i,j,max,min,inst;

for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
arr[i][j] = rand()%10;
}
}

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

cout<<"Maximum value in array is: "<<max<<endl;

for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
if(max==arr[i][j])
{
cout<<"Found at location "<<"["<<i+1<<"]"<<"["<<j+1<<"]"<<endl;

if((i>-1)&&(j>-1))
inst++;

}
}
}
cout<<inst<<" instances found. \n";
cout<<"\n";
/* MINIMUM
VALUE---------------------------------------------------------------------------------------*/

min=arr[0][0];
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
if(arr[i][j]<min)
min=arr[i][j];
}
}
cout<<"Minimum value in array is: "<<min<<endl;
inst=0;
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
if(min==arr[i][j])
{
cout<<"Found at location "<<"["<<i+1<<"]"<<"["<<j+1<<"]"<<endl;

if((i>-1)&&(j>-1))
inst++;

}
}
}
cout<<inst<<" instances found. \n";
cout<<"\n";

cout<<"The 10 x 10 array is :"<<endl;


for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
cout<<" "<<arr[i][j];
}
cout<<endl;
}
return 0;
}
(OUTPUT)
QNO-3 :Write C++ program such that the program searches through the array to find maximum
and minimum values and displays them with their respective location numbers. Your program
should have the following . Use the same array you declared in 7.2

#include<iostream>
#include <cstdlib>
using namespace std;

int main(){
int arr[10][10],inst,i,j,n;

for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
arr[i][j] = rand()%10;
}
}

cout<<"Enter a number to search: ";


cin>>n;

cout<<"\n";

for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
if(n==arr[i][j])
{
cout<<"Found at location "<<"["<<i+1<<"]"<<"["<<j+1<<"]"<<endl;

if((i>-1)&&(j>-1))
inst++;

}
}
}

if(inst==0)
{
cout<<"Value not found."<<endl;
exit(0);
}

cout<<inst<<" instances found. \n";


cout<<"\n";

cout<<"The 10 x 10 array is :"<<endl;


for(i=0;i<10;i++)

{
for(j=0;j<10;j++)
{
cout<<" "<<arr[i][j];
}
cout<<endl;
return 0;
}
(OUTPUT)

You might also like