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

Ans. To The Ques. No. 1

The document contains code snippets for C++ programs to calculate the average of an array, find the maximum and minimum values in an array, concatenate first and last names into a full name, calculate the factorial of a number recursively, and write and read from a text file.

Uploaded by

tasnim
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Ans. To The Ques. No. 1

The document contains code snippets for C++ programs to calculate the average of an array, find the maximum and minimum values in an array, concatenate first and last names into a full name, calculate the factorial of a number recursively, and write and read from a text file.

Uploaded by

tasnim
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Ans. To the Ques. No.

// Avarage of array

#include <iostream>

using namespace std;

int main() {

int i, count, sum, inputArray[900];

float average;

cout << "Enter number of array elements" << endl;

cin >> count;

cout << "Enter " << count << " elements\n";

// Read "count" elements from user

for(i = 0; i < count; i++) {

cin >> inputArray[i];

sum = 0;

for(i = 0; i < count; i++) {

sum += inputArray[i];
}

average = (float)sum / count;

cout << "Average of the array = " << average;

return 0;

Ans. To the Ques. No.2

#include<iostream>

using namespace std;

int main ()

int arr[100], n, i, max, min;

cout << "Enter the size of array : ";

cin >> n;

cout << "Enter the elements of 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];

cout << "maximum value : " << max << endl;

cout << " minimum value : " << min << endl;

return 0;

Ans. To the Ques. No. 3

#include <iostream>

using namespace std;

int main()

string firstName, lastName, result;

cout << "Enter your First name: ";

getline (cin, firstName);

cout << "Enter your Last name: ";

getline (cin, lastName);


result = firstName + lastName;

cout << "Full name = "<< result;

return 0;

Task 1

#include<iostream>

using namespace std;

int factorial(int x);

int main()

int x;

cout << "Enter a number: ";

cin >> x;

cout << "Factorial of " << x << " = " << factorial(x);

return 0;

}
int factorial(int x)

if(x > 1)

return x * factorial(x - 1);

else

return 1;

Task 2

#include <iostream>

#include<conio.h>

#include <fstream>

using namespace std;

int main()

fstream st; //Creating object

st.open("D:\myFile.txt",ios::out); //Creating new file

if(!st) //Checking & Reading

cout<<"File creation failed"<<endl;

char ch;

while (!st.eof())

st >>ch; //Reading from file

cout << ch; //Read from file


}

st.close(); //Closing file

else

cout<<"New file created";

st<<"Student1 name : akter"<<endl; //Writing to file

st<<"Student1 number : 80"<<endl;

st<<"Student2 name : susmita"<<endl;

st<<"Student2 number : 90"<<endl;

st<<"Student3 name : begom"<<endl;

st<<"Student3 number : 100"<<endl;

st.close(); //Closing file

getch();

return 0;

You might also like