Home > Net >  How to stop the loop from printing the *?
How to stop the loop from printing the *?

Time:12-05

I have created a project that breaks a big number to its roots, it works very well, but it prints an extra * at the end of the last root.

#include <iostream>
using namespace std;

int multinum(int a, int b);
int primeOp(int a);

int main()
{
    char ch;
    do {
        int a=0, b=0;
        multinum(a,b);
        
        cout << "\n\nDo you want to continue?(y/n)";
        cin >> ch;
    } while (ch=='y');
    
return 0;
}

int multinum(int num1, int num2)
{
    cout<< "\nPlease enter the first number : ";
    cin >> num1;
    cout << "\nPlease enter the second number : ";
    cin >> num2;

    cout << num1 <<" = ";
    primeOp(num1);
    
    cout << endl;
    
    cout << num2 <<" = ";
    primeOp(num2);

    return 0;
}

int primeOp(int a)
{
   int i, x, power;

    x=a;

    if (a%2==0)
    {
        power=0 ;
        while(a%2==0)
        {
            a/=2;
            power  ;
        }

        cout << 2 <<"^"<<power<< "*";
    }

    for (i=3; i<=x/2; i =2)
    {
        power=0 ;
        while(a%i==0)
        {
            a/=i;
            power  ;
        }

        if (power!=0)
            cout << i <<"^"<< power << "*";
            
        if (power!=0 && a%i== 0)
            cout << "*";
    }

    if(a==x)
        cout<< x << "^" << 1;


    return 0;
}

I tried to print * in different ways but none of them had any effect, I also tried to stop printing by the use of the last "i" or "power" but it was useless.

What should I do, to stop the * bring printed when it's not needed?

Example: 24 = 2^3 * 3^1 * --- it should become: 24 = 2^3*3^1

CodePudding user response:

In your primeOp() function, you can add an else condition to your if (power!=0 && a%i== 0) statement that will only print the * symbol if the power is not equal to zero and a is divisible by i.

Here is what the updated primeOp() function would look like:

int primeOp(int a)
{
   int i, x, power;

    x=a;

    if (a%2==0)
    {
        power=0 ;
        while(a%2==0)
        {
            a/=2;
            power  ;
        }

        cout << 2 <<"^"<<power<< "*";
    }

    for (i=3; i<=x/2; i =2)
    {
        power=0 ;
        while(a%i==0)
        {
            a/=i;
            power  ;
        }

        if (power!=0)
            cout << i <<"^"<< power << "*";
            
        // Add an else condition here to only print the '*' symbol
        // if the power is not equal to zero and 'a' is divisible by 'i'
        else if (power!=0 && a%i== 0)
            cout << "*";
    }

    if(a==x)
        cout<< x << "^" << 1;


    return 0;
}

After making this change, your code should work as expected and the extra * symbol should no longer be printed at the end of the output.

CodePudding user response:

If finding the last print is not easy make the first print special.

Print the first power like this:

cout << 2 <<"^"<<power;

Then print all the rest via

cout << "*2^"<<power;

I dont understand your code fully, but to know it is the first print you can use a boolean flag.

  • Related