0% found this document useful (0 votes)
2 views10 pages

Questions

The document contains eight programming tasks in C++ covering various concepts such as sorting numbers, calculating factorials, creating classes and objects, implementing inheritance, user authentication, swapping values using pointers, checking prime numbers, and finding maximum, minimum, and mean values in an array. Each task includes code snippets and sample outputs demonstrating the expected functionality. The tasks are designed to help users practice and understand fundamental programming concepts.

Uploaded by

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

Questions

The document contains eight programming tasks in C++ covering various concepts such as sorting numbers, calculating factorials, creating classes and objects, implementing inheritance, user authentication, swapping values using pointers, checking prime numbers, and finding maximum, minimum, and mean values in an array. Each task includes code snippets and sample outputs demonstrating the expected functionality. The tasks are designed to help users practice and understand fundamental programming concepts.

Uploaded by

SAMIRA KHAN
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1. Create user defined function to sort the inputted numbers in ascending order.

Use array
to store 10 numbers.
// Question 1
#include <iostream>
using namespace std;

void sortAscending(int arr[], int n) {


for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

int main() {
int numbers[10];
cout << "Enter 10 numbers: ";
for (int i = 0; i < 10; i++) {
cin >> numbers[i];
}
sortAscending(numbers, 10);
cout << "Numbers in ascending order: ";
for (int i = 0; i < 10; i++) {
cout << numbers[i] << " ";
}
cout << endl;
return 0;
}

// Sample Output:
// Enter 10 numbers: 5 2 8 1 9 3 7 4 6 0
// Numbers in ascending order: 0 1 2 3 4 5 6 7 8 9
2. Create user defined function to calculate factorial of a given number.
// Question 2
#include <iostream>
using namespace std;

int factorial(int n) {
int fact = 1;
for (int i = 1; i <= n; i++) {
fact *= i;
}
return fact;
}

int main() {
int num;
cout << "Enter a number: ";
cin >> num;
cout << "Factorial of " << num << " is " << factorial(num) << endl;
return 0;
}

// Sample Output:
// Enter a number: 5
// Factorial of 5 is 120

3. Create a class named Employee with parametrize constructor having parameters name
and salary, and a member function display( ) to show the information of the employee,
create an object of Employee in main function and access public and private members of
the class.

// Question 3

#include <iostream>

#include <string>

using namespace std;

class Employee {

private:
string name;

double salary;

public:

Employee(string n, double s) {

name = n;

salary = s;

void display() {

cout << "Employee Name: " << name << endl;

cout << "Employee Salary: " << salary << endl;

};

int main() {

Employee emp("John Doe", 50000);

[Link]();

return 0;

// Sample Output:

// Employee Name: John Doe

// Employee Salary: 50000

4. create a class named Vehicle having a functions names as Start_Engine( ) and Brakes( ).
The function should print the state of vehicle. create a class named Car inherited from
class Vehicle having a function named FourWheeler( ). The function should print
information about the number of wheels in a car
// Question 4

#include <iostream>

using namespace std;

class Vehicle {

public:

void Start_Engine() {

cout << "Engine Started" << endl;

void Brakes() {

cout << "Brakes Applied" << endl;

};

class Car : public Vehicle {

public:

void FourWheeler() {

cout << "Car has 4 wheels" << endl;

};

int main() {

Car myCar;

myCar.Start_Engine();

[Link]();

[Link]();
return 0;

// Sample Output:

// Engine Started

// Car has 4 wheels

// Brakes Applied

5. Create a sign-in interference which takes user name, password and compare the user
name with “AKHSS” and password with “abc12345”. If password is correct it shows
successfully login otherwise ask to retype the credential. User is allowed only 3
attempts, if fails program terminate with waring.

// Question 5

#include <iostream>

#include <string>

using namespace std;

int main() {

string username, password;

int attempts = 0;

bool loggedIn = false;

while (attempts < 3) {

cout << "Enter username: ";

cin >> username;

cout << "Enter password: ";

cin >> password;


if (username == "AKHSS" && password == "abc12345") {

cout << "Successfully Login" << endl;

loggedIn = true;

break;

} else {

attempts++;

cout << "Invalid credentials. Attempts remaining: " << 3 - attempts << endl;

if (!loggedIn) {

cout << "Warning: Too many failed attempts. Program terminated." << endl;

return 0;

// Sample Output (success):

// Enter username: AKHSS

// Enter password: abc12345

// Successfully Login

// Sample Output (failure):

// Enter username: user

// Enter password: wrong

// Invalid credentials. Attempts remaining: 2

// Enter username: test


// Enter password: test

// Invalid credentials. Attempts remaining: 1

// Enter username: admin

// Enter password: admin

// Invalid credentials. Attempts remaining: 0

// Warning: Too many failed attempts. Program terminated.

6. swap the values of two variables using pointers.

// Question 6

#include <iostream>

using namespace std;

int main() {

int a, b;

cout << "Enter two numbers: ";

cin >> a >> b;

cout << "Before swap: a = " << a << ", b = " << b << endl;

int *ptr1 = &a;

int *ptr2 = &b;

int temp = *ptr1;

*ptr1 = *ptr2;

*ptr2 = temp;

cout << "After swap: a = " << a << ", b = " << b << endl;
return 0;

// Sample Output:

// Enter two numbers: 10 20

// Before swap: a = 10, b = 20

// After swap: a = 20, b = 10

7. Create a user define function to show whether the inputted number is a prime number
or composite number.

// Question 7

#include <iostream>

#include <cmath>

using namespace std;

void checkPrime(int n) {

if (n <= 1) {

cout << n << " is neither prime nor composite" << endl;

return;

bool isPrime = true;

for (int i = 2; i <= sqrt(n); i++) {

if (n % i == 0) {

isPrime = false;

break;

}
if (isPrime)

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

else

cout << n << " is a composite number" << endl;

int main() {

int num;

cout << "Enter a number: ";

cin >> num;

checkPrime(num);

return 0;

// Sample Output:

// Enter a number: 7

// 7 is a prime number

//

// Enter a number: 12

// 12 is a composite number

8. Stores numeric values in a one dimensional array using for loop and finds the maximum,
minimum and mean values.
// Question 8
#include <iostream>
using namespace std;

int main() {
int arr[10];
cout << "Enter 10 numeric values: ";
for (int i = 0; i < 10; i++) {
cin >> arr[i];
}

int maxVal = arr[0];


int minVal = arr[0];
int sum = 0;

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


if (arr[i] > maxVal)
maxVal = arr[i];
if (arr[i] < minVal)
minVal = arr[i];
sum += arr[i];
}

double mean = sum / 10.0;

cout << "Maximum value: " << maxVal << endl;


cout << "Minimum value: " << minVal << endl;
cout << "Mean value: " << mean << endl;

return 0;
}

// Sample Output:
// Enter 10 numeric values: 15 22 8 31 42 5 19 27 33 11
// Maximum value: 42
// Minimum value: 5
// Mean value: 21.3

You might also like