Results 1 to 2 of 2

Thread: how to write this pyramid using java?

  1. #1
    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. #2
    Join Date
    May 2009
    Posts
    511

    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; }

Similar Threads

  1. Write a Java Programm Help !!
    By jessesaini in forum Software Development
    Replies: 1
    Last Post: 08-05-2012, 11:38 AM
  2. How to write java program to print pyramid of stars?
    By Nadiaa in forum Software Development
    Replies: 6
    Last Post: 19-08-2011, 05:03 PM
  3. Need help to write this program in java?
    By frkadeel in forum Software Development
    Replies: 1
    Last Post: 01-12-2010, 03:58 PM
  4. How to write a Key Listener in Java?
    By N I C K in forum Software Development
    Replies: 5
    Last Post: 13-02-2010, 01:52 AM
  5. Write Once And Run Anywhere Nature In Java
    By D_chapple in forum Software Development
    Replies: 5
    Last Post: 10-02-2010, 06:42 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,872,597.53441 seconds with 17 queries