#1
 Rainy Member Join Date: Sep 2011 Posts: 1
how to write this pyramid using java?

hi guys, so recently I was asked to write this program using java and I have no idea on how to do it. can anybody help me?
it looks like this
edit: well for some reason it keeps aligning the text to the left so here i uploaded the pic through imageshack

http://imageshack.us/photo/my-images/534/pryamid.png/

thanks in advance Last edited by Rainy : 01-09-2011 at 03:58 PM.
#2
 Vandam Member Join Date: May 2009 Posts: 503
re: how to write this pyramid using java?

Hello i am not that much aware with the java language but i am having similar kind of solution in c++, so you can apply this same logic in your java language because its toatlly depend on for loop. i hope this will help you.

1. #include <iostream>
2. #include <cmath>
3. using namespace std;
4.
5. int main(){
6. double c,r;
7.
8. for (r = 0; r <= 8; r++) {
9. for (c = 0; c == 7 - r; c++) {
10. if (ceil(log10(pow(2,c))) == 1)
11. cout << " " << pow(2,c);
12.
13. else if (ceil(log10(pow(2,c))) == 2)
14. cout << " " << pow(2,c);
15.
16. else if (ceil(log10(pow(2,c))) == 3)
17. cout << " " << pow(2,c); }
18.
19. //left half
20. for (c = 0; c == r; c++) {
21. if (ceil(log10(pow(2,c))) == 1)
22. cout << " " << pow(2,c);
23.
24. else if (ceil(log10(pow(2,c))) == 2)
25. cout << " " << pow(2,c);
26.
27. else if (ceil(log10(pow(2,c))) == 3)
28. cout << " " << pow(2,c); }
29.
30. //right half
31. for (c = r- 1; c >= 0; c--) {
32. if (ceil(log10(pow(2,c))) == 1)
33. cout << " " << pow(2,c);
34.
35. else if (ceil(log10(pow(2,c))) == 2)
36. cout << " " << pow(2,c);
37.
38. else if (ceil(log10(pow(2,c))) == 3)
39. cout << " " << pow(2,c); }
40.
41. cout << endl; }
42.
43.
44. return 0; }

 Tags:

 Thread Tools Search this Thread Show Printable Version Email this Page Search this Thread: Advanced Search Similar Threads for: "how to write this pyramid using java?" Thread Thread Starter Forum Replies Last Post Write a Java Programm Help !! jessesaini Software Development 1 08-05-2012 11:38 AM How to write java program to print pyramid of stars? Nadiaa Software Development 6 19-08-2011 05:03 PM Need help to write this program in java? frkadeel Software Development 1 01-12-2010 03:58 PM How to write a Key Listener in Java? N I C K Software Development 5 13-02-2010 01:52 AM Write Once And Run Anywhere Nature In Java D_chapple Software Development 5 10-02-2010 06:42 PM

All times are GMT +5.5. The time now is 04:31 PM.