Thursday, July 9, 2009

C Programmer... anybody?

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.

C Programmer... anybody?
#include %26lt;stdio.h%26gt;


int main()


{


int i,j,k,h;


printf("Please enter height ");


scanf("%d",%26amp;h);


for(i=0;i%26lt;h;i++)


{


for(j=0;j%26lt;((h-i)-1);j++)


{


printf(" ");


}


for(k=0;k%26lt;((2*i)+1);k++)


{


printf("* ");


}


printf("\n");


}


return 0;


}
Reply: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%26lt;TREE_HEIGHT; ++row) {


for (int space=0; space%26lt;(TREE_HEIGHT - row - 1); ++space) printf(" ");


for (int tree=0; tree%26lt;(2*row+1); ++tree) printf("*");


printf("\n");


}
Reply: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 %26lt;stdio.h%26gt;


#include %26lt;conio.h%26gt;





void main() {


int height = 4;


int i, j, k;





clrscr();


// Main loop for height


for(i=0; i%26lt;height; i++) {


// Print spaces


for(j=height-i; j%26gt;1; j--)


printf(" ");


// Print first half stars triangle


for(j=0; j%26lt;=i; j++)


printf("*");


// Print second half mirror stars triangle


for(j=0; j%26lt;i; j++)


printf("*");


printf("\n");


}





getch();


}








******************************


Program for binary addition


#include %26lt;stdio.h%26gt;


#include %26lt;conio.h%26gt;


#include %26lt;stdlib.h%26gt;


#include %26lt;math.h%26gt;





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", %26amp;a);


printf("Enter second binary number: ");


scanf(" %d", %26amp;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 %26lt; intSize; i++) {


bin_c[intSize-i-1] = (c %26amp; 0x1) + 48;


c = c %26gt;%26gt; 1;


}


// Print binary sum


printf("%d + %d = ", a, b);


trimFlag = 0;


for(i=0; i %26lt; intSize; i++) {


if(bin_c[i] == 48 %26amp;%26amp; 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) %26gt; 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;


}


No comments:

Post a Comment