In: Computer Science
-Write a program in C++:
• to find the sum of the series 1! /1+2! /2+3! /3+4! /4+5! /5 using the function1,
• to convert decimal number to binary number using the function2,
• to check whether a number is a prime number or not using the function3,
• to check whether two given strings are an anagram using the function4.
important must do in (Multi-Filing) of c++
file 1: function1.cpp
float function_1(){
float sum = 0; // variable to find the sum
for ( int i = 1;i < 5; i++){
int fact = 1;
// for loop to find the factorial
for( int j = 1; j <= i;j++){
fact = fact*j;
}
sum = sum + fact/i; // calculating the sum
}
return sum; // returning the sum
}
----------------------------------------------------------------------------------------------------------
file 2: function2.cpp
int function_2(int n, int b_num[]){
// array to store binary numbers
// index variable
int i = 0;
while (n > 0) {
// storing remainder in binary array
b_num[i] = n % 2;
n = n / 2;
i++;
}
return i; // returning the index value
}
-------------------------------------------------------------------------------------------------------
file 3: function3.cpp
bool function_3(int n){
int i;
bool is_prime = true; // to store whether number is prime or
not
for (i = 2; i <= n / 2; ++i) { // for loop to check
prime number
if (n % i == 0) { // test for prime
is_prime = false;
break;
}
}
return is_prime; // returning the result
}
------------------------------------------------------------------------------------------------------------
file 4: function4.cpp
#include <bits/stdc++.h>
using namespace std;
int function_4(string str_1, string str_2)
{
int n1 = str_1.length(); // finding the length of first
string
int n2 = str_2.length(); // finding the length of second
string
if (n1 != n2) // checking the length
return false;
sort(str_1.begin(), str_1.end()); // soriting the elements
sort(str_2.begin(), str_2.end()); // sorting the elements
for (int i = 0; i < n1; i++)
if (str_1[i] != str_2[i]) // checking if both the strings are soma
or not after arranging the character in ascending order
return false; // return the result
return true; // return the result
}
-------------------------------------------------------------------------------------------
Main file:
#include<iostream> // header file for basic input
output operation
#include<string> // header file for string operation
#include <limits> // header file to ignore the aditional
newline in cin
#include "function1.cpp" // including file
#include "function2.cpp" // including file
#include "function3.cpp" // including file
#include "function4.cpp" // including file
using namespace std;
int main(){ // main fucntion
cout<<"Sum of the series is : " << function_1(); //
calling the first fucntion
int number, bin_array[100]; // variable to store number and binary
number
cout << "\nEnter a decimal number to convert to
binary: " ;
cin >> number;
cout << "Binary value is: " ;
int i = function_2(number, bin_array); // getting the index to
which binary value is store in the array
for (int j = i - 1; j >= 0; j--)
cout << bin_array[j]; // printing the binary value
cout << endl;
int num; // user input for numbe rto check for prime
number
cout<< "\nEnter a number to check if it is prime or not:
";
cin>> num;
if(function_3(num)){
cout<< "Number is prime number\n";
}
else{
cout<< "Number is not prime\n" ;
}
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // to
remove the additional newline character present in cin which is
eaten by getline function which skip the input for the first
variable
string str_1, str_2;
cout<< "Enter string 1: ";
getline(cin, str_1); // user input for first string
cout<< "Enter string 2: ";
getline(cin, str_2); // user input for seconf string
if(function_4(str_1, str_2)){ // checking string is
anagram or not
cout << "Strings are anagram\n";
}
else{
cout <<"Strings are not anagram\n";
}
return 0;
}
------------------------------------------------------------------------------------------------------------
Sample output:
Note: Please let me know if you have any doubt, or there is any change required in the code.