Write a C program that displays characteristics of a number

Write a C++ program that displays characteristics of a number depending on whether it is a real or a complex number.
1.The user is asked if he wants the characteristics of a real number (choice 1) or a complex number (choice 2).
2. The following applies to CHOICE 1:
a. Obtain a real number (whole number) from the user.
b. Display the following characteristics:
i. Integer Part
ii. Fractional Part
iii. Round: Rounding value to the next integer if the fractional part is > 0.5 else it is approximated to the integer part of the number.
iv. Floor
v. Ceil
2. The following applies to CHOICE 2:
a. Obtain the real part of the complex number and the imaginary part of the complex number from the user. (Ex: X + Yj => X is the real part and Y is the complex part)
b. Display the following characteristics:
i. Real Part
ii. Imaginary Part
iii. Complex conjugate: Complex conjugate is obtained by reversing the sign of the complex part entered by the user. For ex: If X + Yj is the complex conjugate of X – Yj)
iv. Magnitude of the complex number: Formula: sqrt (X2 + Y2 )
v. Phase of the complex number, in degrees: Formula = atan (Y/X) * (180/PI)
Note: atan is a pre-defined function in cmath and PI is a const defined in cmath
Requirements:
1. You are not required to check for wrong inputs from the user. Assume right values for Choices 1 and 2.
2. You are required to use functions (user-defined functions) for the following operations:
a. Round for choice 1
b. Magnitude for choice 2
c. Phase for choice 2
3. You are required to use pre-defined (library) functions for the following operations:
a. Floor for Choice 1
b. Ceil for Choice 1
4. For the remaining characteristics, using functions is optional.
5. Be careful about dividing by zero for a pure-complex number (X=0) while computing the phase of the complex number. In this case, you can set the Phase of the complex number (in degrees) to 90.
A sample run of your program should look like: (BOLD RED implies User’s input and Blue NOT BOLD implies screen output)
Run 1:
Enter 1 to obtain the characteristics of a real number and 2 for a complex number:
1
To obtain the characteristics of a real number, please enter a whole number:
4.5
The characteristics of 4.5 are as follows:
Number Integer Fraction Round Floor Ceil
4.5 4 0.5 5 4 5
Run 2:
Enter 1 to obtain the characteristics of a real number and 2 for a complex number:
2
To obtain the characteristics of a complex number, please enter the integer and imaginary part of the number:
-3 4
The characteristics of 3 + 4j are as follows:
Number Real Imaginary Conjugate Magnitude-Phase (degrees)
-3 + 4j -3 4 -3 - 4j 5 -53.13

Solution

/**
C++ program that prompts user to enter the user choice for characteristics
of real number and complex number.
*/
//test.cpp
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
//function prototypes
int round(double x);
double maginitude(double r, double i);
double phase(double r, double i);
//PI constant
const double PI=3.1417;
//main function
int main()
{

   int userChoice;
   cout<<\"\ \ Enter 1 to obtain the characteristics of a real number and 2 for a complex number:\";
   cin>>userChoice;
   //for userchoice is 1
   if(userChoice==1)
   {
       float real;
       cout<<\"To obtain the characteristics of a real number, please enter a whole number:\";
       cin>>real;

       cout<<\" The characteristics of \"<<real<<\" are as follows\ :\";
       cout<<\" Number Integer Fraction Round Floor Ceil\ \";
       double fractpart, intpart;

      
       fractpart = modf (real , &intpart);
       cout<<real<<\"\\t\"<<intpart<<\"\\t\"<<fractpart<<\"\\t\"
           <<round(real)<<\"\\t\"<<floor(real)<<\"\\t\"<<round(real);
   }
   //for choice =2
   else if(userChoice==2)
   {
       int realNum;
       int imgNum;
       cout<<\"To obtain the characteristics of a complex number, please enter the integer and imaginary part of the number:\";
       cin>>realNum>>imgNum;

       cout<<\" The characteristics of \"<<realNum<<\" + \"<<imgNum<<\"j are as follows:\";

       cout<<realNum<<\"+\"<<imgNum<<\"j \";
       cout<<realNum<<\"\\t\"<<imgNum<<\" \";

       if(imgNum>0)
       {
           imgNum=(-1)*imgNum;      
           cout<<realNum<<\"-\"<<imgNum<<\"j\"<<\" \"<<maginitude(realNum,imgNum);
       }
       else
           cout<<realNum<<\"-\"<<imgNum<<\"j\"<<\" \"<<maginitude(realNum,imgNum);

       cout<<fixed<<setprecision(2)<<phase(realNum,imgNum);
   }


   system(\"pause\");
   return 0;
}

//Return round value
int round(double x)
{
   return ceil(x);
}
//returns the magnitude of the complex number
double maginitude(double r, double i)
{
   return sqrt(r*r+i*i);
}
//Returns the phase of the complex number
double phase(double r, double i)
{
   return atan (i/r) * (180/PI);
}

------------------------------------------------------------------------------

Sample output:

Enter 1 to obtain the characteristics of a real number and 2 for a complex numbe
r:1
To obtain the characteristics of a real number, please enter a whole number:4.5
The characteristics of 4.5 are as follows
: Number Integer Fraction Round Floor Ceil
4.5     4       0.5     5       4       5

Run2:

Enter 1 to obtain the characteristics of a real number and 2 for a complex numbe
r:2
To obtain the characteristics of a complex number, please enter the integer and
imaginary part of the number:-3
4
The characteristics of -3 + 4j are as follows:-3+4j -3 4 -3--4j 5 53.13

Write a C++ program that displays characteristics of a number depending on whether it is a real or a complex number. 1.The user is asked if he wants the charact
Write a C++ program that displays characteristics of a number depending on whether it is a real or a complex number. 1.The user is asked if he wants the charact
Write a C++ program that displays characteristics of a number depending on whether it is a real or a complex number. 1.The user is asked if he wants the charact

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site