English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

can you write a program whose output is this one:
....................*.
................*...*..*.
............*...*..*..*.*.
........ *..*...*..*...*..*...*.
given the height is 4. JUST IGNORE THE DOTS(.)

how about this one(output)?

ENTER BINARY STRING: 100111
ENTER BINARY STRING: 11

...100111
+ 000011
_________
...101010

whatever you can answer, i will be grateful to you.

IGNORE THE DOTS.

2007-07-26 02:45:13 · 3 answers · asked by tu t'appelle comment? 1 in Computers & Internet Programming & Design

3 answers

#include
int main()
{
int i,j,k,h;
printf("Please enter height ");
scanf("%d",&h);
for(i=0;i {
for(j=0;j<((h-i)-1);j++)
{
printf(" ");
}
for(k=0;k<((2*i)+1);k++)
{
printf("* ");
}
printf("\n");
}
return 0;
}

2007-07-26 04:19:33 · answer #1 · answered by aceix 6 · 1 0

Hi,
There are many ways to program something. Here, i've written the basic programs. You can extend these further. It's fun.

Program for star tree:
#include
#include

void main() {
int height = 4;
int i, j, k;

clrscr();
// Main loop for height
for(i=0; i // Print spaces
for(j=height-i; j>1; j--)
printf(" ");
// Print first half stars triangle
for(j=0; j<=i; j++)
printf("*");
// Print second half mirror stars triangle
for(j=0; j printf("*");
printf("\n");
}

getch();
}


******************************
Program for binary addition
#include
#include
#include
#include

int BtoD(int);

void main() {
int a, b, c;
int dec_a, dec_b;
int i, trimFlag, intSize;
char bin_c[sizeof(int)*8+1];

clrscr();
// Input two binary numbers
printf("Enter first binary number: ");
scanf(" %d", &a);
printf("Enter second binary number: ");
scanf(" %d", &b);
// Convert binary numbers to decimal and add them
dec_a = BtoD(a);
dec_b = BtoD(b);
c = dec_a + dec_b;
// Store binary digits of sum in character array in reverse order
intSize = sizeof(int) * 8;
for(i=0; i < intSize; i++) {
bin_c[intSize-i-1] = (c & 0x1) + 48;
c = c >> 1;
}
// Print binary sum
printf("%d + %d = ", a, b);
trimFlag = 0;
for(i=0; i < intSize; i++) {
if(bin_c[i] == 48 && trimFlag == 0) continue;
else trimFlag = 1;
printf("%c", bin_c[i]);
}

getch();
}

// Function to convert Binary to Decimal
int BtoD(int x) {
int dec_x, i;

// Count number of digits and store individual digits in received argument
i = 0;
dec_x = 0;
do {
if((x % 10) > 1) {
printf("\nError: Invalid binary number.\n");
printf("Press any key to exit...");
getch();
exit(0);
}
dec_x += (x % 10) * (int)pow(2, i);
x = x / 10;
i++;
} while(x != 0);

return dec_x;
}

2007-07-26 05:20:17 · answer #2 · answered by Pixert 2 · 0 0

For the tree, row 0 has 1 asterisk, row 1 has 3, etc. (row n has 2n+1). Also, each row is indented by one space per row up from the bottom. The main loop to print the tree will look something like this:

int TREE_HEIGHT = 4;
for (int row=0; row for (int space=0; space<(TREE_HEIGHT - row - 1); ++space) printf(" ");
for (int tree=0; tree<(2*row+1); ++tree) printf("*");
printf("\n");
}

2007-07-26 03:23:24 · answer #3 · answered by McFate 7 · 0 1

fedest.com, questions and answers