0% found this document useful (0 votes)
9 views14 pages

Quiz

The document contains 5 programming questions and their solutions in C++. Question 1 asks to write a function to print a pattern of stars for a given integer. Question 2 finds the three largest distinct elements in an array. Question 3 merges two arrays into a third array. Question 4 counts the number of occurrences of an integer in a sorted array. Question 5 creates a scientific calculator program with functions for addition, subtraction, multiplication, division and other mathematical operations.

Uploaded by

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

Quiz

The document contains 5 programming questions and their solutions in C++. Question 1 asks to write a function to print a pattern of stars for a given integer. Question 2 finds the three largest distinct elements in an array. Question 3 merges two arrays into a third array. Question 4 counts the number of occurrences of an integer in a sorted array. Question 5 creates a scientific calculator program with functions for addition, subtraction, multiplication, division and other mathematical operations.

Uploaded by

Farooq Tariq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

QUIZ

PROGRAMMING FUNDAMENTAL
NAME: MUHAMMAD FAROOQ
ROLL NO: 014
MINHAJ UNIVERSITY LAHORE
………………………………………………………………
QUESTION NO 1:-
Write an iterative function void printPattern(num) that takes as a parameter a nonnegative
integer and generates the following pattern of stars to generate the following pat- tern.

E.g. if the non-negative integer is 5, then following pattern is generated:

#include <iostream>

using namespace std;

void printPattern(int num) {

for(int i=1; i<=num; i++) {

for(int j=1; j<=i; j++) {

cout << "*";

cout<< endl;

for(int i=num-1; i>=1; i--) {


for(int j=1; j<=i; j++) {

cout << "*";

cout << endl;

int main() {

int num;

cout << "Enter a non-negative integer: ";

cin >> num;

printPattern(num);

return 0;

QUESTION NO 2:-

Find the three largest distinct elements in the array.

#include <iostream>

#include <algorithm>

using namespace std;

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

if (n < 3) {

cout << "Array must have at least 3 elements" << endl;

return;
}

int first = INT_MIN, second = INT_MIN, third = INT_MIN;

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

if (arr[i] > first) {

third = second;

second = first;

first = arr[i];

} else if (arr[i] > second && arr[i] != first) {

third = second;

second = arr[i];

} else if (arr[i] > third && arr[i] != first && arr[i] != second) {

third = arr[i];

if (third == INT_MIN) {

cout << "Array does not have three distinct elements" << endl;

} else {

cout << "The three largest distinct elements are: " << first << ", " << second << ", "
<< third << endl;

}
}

int main() {

int arr[] = {10, 4, 3, 8, 6, 7, 9, 2, 5, 1};

int n = sizeof(arr) / sizeof(arr[0]);

findThreeLargest(arr, n);

return 0;

QUESTION NO 3:-

Write a Program to merge two arrays.

#include<iostream>

using namespace std;

int main()

int a[5],b[5],c[10];

cout<<"Enter 5 elements in 1st array\n";

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

cin>>a[i];

cout<<"Enter 5 elements in 2nd array\n";

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

{
cin>>b[i];

cout<<"Array after emerging\n"<<endl;

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

c[i]=a[i];

c[5+i]=b[i];

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

cout<<c[i]<<" ";

return 0;

}
QUESTION NO 4:-

Count the number of occurrences in a sorted Array.


#include <iostream>

using namespace std;

int countOccurrences(int arr[], int n, int x)

int count = 0;

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

if (arr[i] == x) {

count++;

if (arr[i] > x) {

break;

return count;

int main()

int arr[] = {1, 2, 2, 3, 3, 3, 4, 5, 5};

int n = sizeof(arr) / sizeof(arr[0]);

int x = 3;

int count = countOccurrences(arr, n, x);

cout << "The number of occurrences of " << x << " is " << count << endl;

return 0;
}

QUESTION NO 5:-

Write a C++ program to create a calculator for mathematical


calculations such as addition, subtraction, division, multiplication.
Also add the functionality for Scientific calculators such as square
roots, functions, exponential operations, logarithms, trigonometric
functions, and hyperbolic functions.

#include <iostream>

#include<math.h>

using namespace std;

void addition();

void subtraction();

void multiplication();

void division();

void cube();

void squar();

void sqrt();

void pow();

void factorial();

int main()

cout << "SCIENTIFIC CALCULATOR"<<endl;

cout << "Enter 1 for aAddition "<<endl;

cout << "Enter 2 for subtraction"<<endl;


cout << "Enter 3 for multiplication "<<endl;

cout << "Enter 4 for division" <<endl;

cout <<" Enter 5 for cube "<<endl;

cout <<" Enter 6 for squar "<<endl;

cout <<" Enter 7 for sqrt "<<endl;

cout <<" Enter 8 for power "<<endl;

cout <<" Enter 9 for factorial "<<endl;

while (1)

int number;

cout << "Enter number: ";

cin >> number;

switch (number)

case 1:

addition();

break;

case 2:

subtraction();

break;

case 3:

multiplication();
break;

case 4:

division();

break;

case 5:

cube();

break;

case 6:

squar();

break;

case 7:

sqrt();

break;

case 8:

pow();

break;

case 9:

factorial();

break;

case 0:

exit(0);

break;
default:

cout << "\n** Enter valid choice**\n";

break;

return 0;

void addition()

cout<<"Enter the numbers to add: \n";

int a,b;

cout<<"Enter 1st number : ";

cin>>a;

cout<<"Enter 2nd number : ";

cin>>b;

cout<<"The addition of "<<a<<" and "<<b<<" is "<<a+b<<endl;

void subtraction(){
cout<<"Enter the numbers to subtract: \n";

int a,b;

cout<<"Enter 1st number : ";

cin>>a;

cout<<"Enter 2nd number : ";

cin>>b;

cout<<"The subtraction of "<<a<<" and "<<b<<" is "<<a-b<<endl;

void multiplication(){

cout<<"Enter the numbers to multipliation: \n";

int a,b;

cout<<"Enter 1st number : ";

cin>>a;

cout<<"Enter 2nd number : ";

cin>>b;

cout<<"The multiplication of "<<a<<" and "<<b<<" is "<<a*b<<endl;

void division(){

cout<<"Enter the numbers to divide: \n";

float a,b;

cout<<"Enter 1st number : ";

cin>>a;

cout<<"Enter 2 number : ";


cin>>b;

cout<<"The division of "<<a<<" and "<<b<<" is "<<a/b<<endl;

void cube(){

cout<<"Enter the numbers to find cube : \n";

int a;

cout<<"Enter number : ";

cin>>a;

cout<<"The cube of "<<a<<" is "<<a*a*a<<endl;

void squar()

cout<<"Enter the numbers to find squar : \n";

int a;

cout<<"Enter number : ";

cin>>a;

cout<<"The squar of "<<a<<" is "<<a*a<<endl;

void sqrt()

cout<<"Enter the numbers to find sqrt : \n";

int a;

cout<<"Enter number : ";


cin>>a;

cout<<"The sqrtof "<<a<<" is "<<a/2<<endl;

void pow()

cout<<"Enter the numbers to find pow : \n";

int a;

cout<<"Enter base : ";

cin>>a;

int b;

cout<<"Enter exponent";

cin>>b;

cout<<"The power of " << a << " and " << b << " is ""=" <<pow(a,b)
<<endl;

void factorial()

cout<<"Enter the numbers to find factorial : \n";

int a,fac=1;

cout<<"Enter number : ";


cin>>a;

for(int i=1;i<=a;i++)

fac=fac*i;

cout<<"The factorial of " << a << " is ""=" << fac <<endl;

You might also like