简体   繁体   中英

C++ : how do I make the program exactly like in the picture?

结果应该是这样的:

*Buatlah program untuk menghitung perkalian deret bilangan genap membentuk segitiga siku terbalik dengan hasil seperti pada gambar di atas.

my program is like this :

#include <stdio.h>
#include <conio.h>
#include <iostream>
#include <iomanip>

using namespace std;

int main(int argc, const char *argv[])
{
    int i, j, n;
    for(i=0; i<5; puts(""),++i)
    {
        n=0;
        for(j=5; j>i; n+=2*(j--))
        {
            if(j>i+1) {
                printf("%d * ",2*j);
            }
            else {
                printf("%d ",2*j);
            }
        }
        printf("\t= %d",n);
    }
    printf("\t\t110");
    return(0);
}

how do I make the program exactly like in the picture above?

This code gives the output you stated in your question. I'm not sure though if you are intended to do it like this, since there might be a smarter solution using iomanip and iostream, because you included it.

#include <stdio.h>

using namespace std;

int main(int argc, const char *argv[])
{
    int i, j, n;
    for(i=0; i<5; puts(""),++i)
    {
        n=0;
        for(j=5; j>i; n+=2*(j--))
        {
            if(j>i+1) {
                printf("%d + ",2*j);
            }
            else {
                printf("%d ",2*j);
            }
        }
        for (int k = 0; k <= i; k++) printf("    ");
        printf("= %d",n);
    }
    printf("\t\t    ---------- +\n");
    printf("\t\t\t 110\n");
    return(0);
}
#include <iostream>
using namespace std;

int main(int argc, const char *argv[]){
    int i,  k, n, s=0;

    for(k=2; k <= 10; k+=2) {
        n=0;
        for(i=10; k <= i; i-=2) {
            if (k < i)
                cout<<i<<" + " ;
            else {
                cout<< i;
                cout.width(k*2);
                cout<< right<< " = ";
            }
            n+=i;
        }
    cout<<n<<"\n";
    s += n;
    }
    cout<< "------------------------- +"<<"\n";
    cout<< "                     "<<s<<"\n";
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM