You are on page 1of 9

ASSIGNMENT#3

PF THEOREY
BSSE-1-B
SUBMITTED BY: ABU BAKAR
SUBMITTED TO: SADAF ANWAR
QUESTION#1:
STRING ARRAY:

String array or array of string is an array of multiple strings.

Strings hold groups of characters, like a word or a phrase. In programming, strings allow
programmers to easily store, find, and change information in a program.

String variables make it easier for programmers to find information and reuse values in their
code. strings make simple for programmers to read and update the values allocated to various
variables across their programmers.

DECLARATION:

The array is named and the type of its elements are specified in a"array declaration." It can also
provide the array's size in terms of elements. A variable having an array type is regarded as a
pointer to the array elements' type.

For example, you can create an array named age and store the ages of 5 students as follows:

int age [5];

The array age will store 5 integers representing the ages of different students.

INITIALIZATION:

The process of assigning and storing elements to an array is known as array initialization.
Initialization can be done all at once or one by one. The first element in an array is kept at index
0, and the last member is kept at index n-1, where n is the array's total number of items.

CODE:
string names [5] = {“A”,” B”,” C”,” D”, “E”};

TO PRINT WHOLE ARRAY:


for (int I = 0; I < 4; I ++)

cout<<names [I] << endl; }

EXAMPLE:

#include<iostream>
using namespace std;

int main ()

string names [7] = {"A","B","C","D","E","F","G"};

for (int i = 0; i < 6; i ++)

cout<<names[i]<<endl;

return 0;

Output:

QUESTION#2:
CODE:

#include<iostream>

using namespace std;

int main ()

int x [5] = {0};


cout<<x [0] <<endl;

cout<<x [1] <<endl;

cout<<x [2] <<endl;

cout<<x [3] <<endl;

cout<<x [4] <<endl;

return 0;

Output:

QUESTION#3:

FIND NUMBER IS PRIME OR NOT.


CODE:
#include<iostream>

using namespace std;

int prime(int n)

int c=0,i;

for(i=2;i<n;i++)

if(n%i==0)

c=1;

if(c==0)

cout<<n<<" is a prime number";

else

cout<<n<<" is not a prime number";

int main()

int n;
cout<<"\nEnter a number: ";

cin>>n;

prime(n);

return 0;

Output:

QUESTION#4:

WRITE A NUMBER AND REVERSE OF THE NUMBER.


CODE:

#include<iostream>

using namespace std;

int reverse(int a)

while(a!=0)

int i=0;
i=i*10;

i=i+a%10;

a=a/10;

cout<<i;

int main()

int a;

cout<<"enter a number: ";

cin>>a;

cout<<"reverse of a number: ";

reverse(a);

return 0;

Output:

QUESTION#5:

2D ARRAY AND SHOW THEIR SUM.


CODE:

#include<iostream>

using namespace std;

int main()

{ int sum[2][2],matrix2[2][2]={1,2,3,4}, i,j,matrix1[2][2]={1,2,3,4};

cout<<"First matrix input:\n";

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

for(j=0;j<2;j++)

cout<<"matrix1["<<i<<"]["<<j<<"]="<<matrix1[i][j]<<endl;

cout<<"Second matrix input:\n";

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

for(j=0;j<2;j++)

cout<<"matrix2["<<i<<"]["<<j<<"]="<<matrix2[i][j]<<endl;

cout<<"Adding matrices\n";

cout<<"The resultant matrix is:\n";


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

for(j=0;j<2;j++)

sum[i][j]=matrix1[i][j]+matrix2[i][j];

cout<<"\t"<<sum[i][j];

cout<<endl;

return 0;

Output:

You might also like