I'm a c++ beginner In the for statement, the if statement

Asked 2 years ago, Updated 2 years ago, 27 views

#include <iostream>
using namespace std;
void main()
{
    int a ,b,total;
    for(a=1;a<=5;a++)
    {
        total=1;
        for(b=1;b<=a;b++)
        {
            total=total*a;
            if(a!=b)
            {
            cout<<total<<"x";
            }
            else
            cout<<total<<" ";
        }
        cout<<endl;
    }
} 

I want to print out the odd number as 0 and the even number as 1, so I think you can use value%2=1 I've been trying, but I can'tHelp me Should I put an if clause in the if clause? The value I want is

0

1x1

0x0x0

1x1x1x1

That's it.

c++

2022-09-22 12:02

1 Answers

using namespace std; void main()

{

int a, b, total;
for (a = 1; a <= 5; a++)
{
    total = 1;
    for (b = 1; b <= a; b++)
    {
        total = total * a;
        if (a != b)
        {
            cout << total%2 << "x";
        }
        else
            cout << total%2 << " ";
    }
    cout << endl;
}

} This is how I printed out the value I wanted.


2022-09-22 12:02

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.