Question

In: Computer Science

-Write a program in C++: • to find the sum of the series 1! /1+2! /2+3!...

-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++

Solutions

Expert Solution

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.


Related Solutions

Write a complete program to sum the following series: (hint: use the for loop) 1 /2...
Write a complete program to sum the following series: (hint: use the for loop) 1 /2 + 1/ 4 + 1 /6 + ⋯ + 1 /100 PS: use C++ language please
Write an assembly language program for 8051 microcontroller to find the sum of the following series,...
Write an assembly language program for 8051 microcontroller to find the sum of the following series, 1, -2, +3, -4, +5, -6,..., up to 100 terms. Store lower byte in R0 and higher byte in R1.
• Write a C++ program to find the largest umber, smallest number and sum of all...
• Write a C++ program to find the largest umber, smallest number and sum of all the element of a given array of 20 integers • Note − Declare array to 20 numbers − Input values for 20 array elements − Find largest number − Find smallest number − Find sum of all numbers • Display the results
Use the formula for the sum of the first n terms of a geometric series to find S9 for the series 12, 6, 3, 3/2 , …
Use the formula for the sum of the first n terms of a geometric series to find S9 for the series 12, 6, 3, 3/2 , …  
1) Write an algorithm to calculate the sum of the following series: Sum =x-x3/3! + x5/5!...
1) Write an algorithm to calculate the sum of the following series: Sum =x-x3/3! + x5/5! – x7/7! +……. Stop when the term<0.0001. 2) An internet service provider charges its subscribers per month as follows: Data usage (n), in gbs charges (NIS) 0.0<n<=1.0 250 1.0<n<=2.0 500 2.0<n<=5.0 1000 5.0<n<=10.0 1500 n>10 2000 Write a C program to read the usage(n) from a file and print the charges to be paid by the subscribers. Your program must include the function calculate...
write pseudocode not c program If- else programming exercises 1.    Write a C program to find...
write pseudocode not c program If- else programming exercises 1.    Write a C program to find maximum between two numbers. 2.    Write a C program to find maximum between three numbers. 3.    Write a C program to check whether a number is negative, positive or zero. 4.    Write a C program to check whether a number is divisible by 5 and 11 or not. 5.    Write a C program to check whether a number is even or odd. 6.    Write...
Write a C++ program to read N numbers. Find sum, product, and average of N numbers
Write a C++ program to read N numbers. Find sum, product, and average of N numbers
Write a C++ program to determine the sum of two 1-D matrices: A = [a b...
Write a C++ program to determine the sum of two 1-D matrices: A = [a b c d] B = [e f g h] The user will provide the values of a to h.
Write a program in C++ that computes the sum of odd numbers between 1 and 117....
Write a program in C++ that computes the sum of odd numbers between 1 and 117. Execute the program and submit a screen capture of the program and its results.
Program – version 1: Sum of Range Algorithm Write a program that will sum the integers...
Program – version 1: Sum of Range Algorithm Write a program that will sum the integers between a given range (limit your range from 0 to 50). For example, if the user want to add the integers between (and including) 1 and 10, then the program should add: 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 Algorithm: Program title/description Ask the user to input a start value in the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT