Membuat Program C++ dengan Output 1=1 1+2=3 1+2+3=6 1+2+3+4=10 1+2+3+4+5=15 5+4+3+2+1=15 4+3+2+1=10 3+2+1=6 2+1=3 1=1

/*Membuat program dengan output
1=1
1+2=3
1+2+3=6
1+2+3+4=10
1+2+3+4+5=15
5+4+3+2+1=15
4+3+2+1=10
3+2+1=6
2+1=3
1=1
*/


#include <iostream.h>
void main()
{
int a, b, i, j, jumlah;
for (a=1; a<=5; a++)
{
jumlah = 0;
for (b=1; b<=a; b++)
{
jumlah=jumlah+b;
cout << b;
if (b < a)
{
cout << "+";
}
else if (b==a)
{
cout << "=" << jumlah;
}
}
cout << endl;
}
for (i=5; i>=1; i--)
{
jumlah = 0;
for (j=i; j>=1; j--)
{
jumlah = jumlah + j;
cout << j;
if (j == 1)
{
cout << "=" << jumlah;
}
else
{
cout << "+";
}
}
cout << endl;
}
cout << endl;
}

Hasil output :

Komentar

Postingan Populer