Qno1: Write a program that helps to find factorial,
percentage, average, Min Number, Max Number, and sort
the array in ascending and descending order. User select
the option what operation he/she wants to perform and
then enter data according to requirement.
Perform all things by using functions.
#include <iostream>
using namespace std;
void findFactorial(){
int n;
long double factorial = 1.0;
cout << "Enter a positive integer: ";
cin >> n;
if (n < 0)
cout << "Error! Factorial of a negative number doesn't exist.";
else {
for(int i = 1; i <= n; ++i) {
factorial *= i;
}
cout << "Factorial of " << n << " = " << factorial<<"\n\n";
}
}
void findPercentage(){
float number;
float percentage;
cout<<"Enter the number: ";
cin>>number;
cout<<"Enter the percentage: ";
cin>>percentage;
float percentageNumber= (number * percentage) / 100.0;
cout<<"Percentage: "<<percentageNumber<<"\n\n";
}
void findAverageArray(float numbers[],int size){
float sum=0;
for(int i=0;i<size;i++){
sum+=numbers[i];
}
float average=sum/(float)size;
cout<<"Average of the array: "<<average<<"\n\n";
}
void findMinNumberArray(float numbers[],int size){
float min=numbers[0];
for(int i=0;i<size;i++){
if(numbers[i]<min){
min=numbers[i];
}
}
cout<<"Min number of the array: "<<min<<"\n\n";
}
void findMaxNumberArray(float numbers[],int size){
float max=numbers[0];
for(int i=0;i<size;i++){
if(numbers[i]>max){
max=numbers[i];
}
}
cout<<"Max number of the array: "<<max<<"\n\n";
}
// print array
void printArray(float numbers[], int size) {
for (int i = 0; i < size; ++i) {
cout << " " << numbers[i];
}
cout << "\n\n";
}
void sortArrayInAscendingOrder(float numbers[],int size){
for(int i=0; i<(size-1); i++)
{
for(int j=0; j<(size-i-1); j++)
{
if(numbers[j]>numbers[j+1])
{
float temp = numbers[j];
numbers[j] = numbers[j+1];
numbers[j+1] = temp;
}
}
}
cout<<"\nThe array in ascending order:\n";
printArray(numbers,size);
}
void sortArrayInDescendingOrder(float numbers[],int size){
for(int i=0; i<(size-1); i++)
{
for(int j=0; j<(size-i-1); j++)
{
if(numbers[j]<numbers[j+1])
{
float temp = numbers[j];
numbers[j] = numbers[j+1];
numbers[j+1] = temp;
}
}
}
cout<<"\nThe array in descending order:\n";
printArray(numbers,size);
}
int main(){
float numbers[1000];
int size;
int option;
cout<<"Enter the size of array: ";
cin>>size;
for(int i=0;i<size;i++){
cout<<"Ente the number "<<(i+1)<<": ";
cin>>numbers[i];
}
do{
cout<<"1. Find factorial\n";
cout<<"2. Find percentage\n";
cout<<"3. Find average of the array\n";
cout<<"4. Find min number of the array\n";
cout<<"5. Find max number of the array\n";
cout<<"6. Sort the array in ascending order\n";
cout<<"7. Sort the array in descending order\n";
cout<<"8. Exit\n";
cout<<"Select the option: ";
cin>>option;
if(option==1){
findFactorial();
}else if(option==2){
findPercentage();
}else if(option==3){
findAverageArray(numbers, size);
}else if(option==4){
findMinNumberArray(numbers, size);
}else if(option==5){
findMaxNumberArray(numbers, size);
}else if(option==6){
sortArrayInAscendingOrder(numbers, size);
}else if(option==7){
sortArrayInDescendingOrder(numbers, size);
}else if(option==8){
//exit
}else{
cout<<"\nWrong menu item\n\n";
}
}while(option!=8);
system("pause");
return 0;
}
Comments
Leave a comment