Results 1 to 16 of 16
- 02-15-2013, 10:55 AM #1
Member
- Join Date
- Feb 2013
- Posts
- 6
- Rep Power
- 0
Display Patterns use Nested Loops
Hai i am newbie in java , can someone helpe me to get output like this :
Hoping can show some simple java source code by using nested Loop that is easy to understand. Many Thanks
1s Pattern:
1
2 4
3 6 9
4 8 12 16
2nd Pattern:
*
***
*****
***
*
3rd Pattern :
*****
*****
** **
*****
*****
4th Pattern:
*********
*********
*********
*********
...
- 02-15-2013, 11:04 AM #2
Re: Display Patterns use Nested Loops
Just read my signature.
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 02-15-2013, 11:06 AM #3
Re: Display Patterns use Nested Loops
This cannot be done in Java. Java uses a binary system in powers of two, so odd numbers like 3 and 9 can't be created.
On a more serious note: ofcourse this can be done, but this is a forum, not a Code Factory™. Show your code and where you run into problems. We're here to help, not to spoonfeed you teh codez of the Jav.
- 02-15-2013, 11:22 AM #4
Member
- Join Date
- Feb 2013
- Posts
- 6
- Rep Power
- 0
Re: Display Patterns use Nested Loops
I am so sorry , i am never thought as this forum as Code Factory. Here we go this is my 1st try on the 1st pattern , where i learn that i can put a pattern 1 but as sequence 1 ,2 ,3 ... but how to change make it as multiplier.
Java Code:public class Pattern{ public static void main(String[]args){ int i,j,k; for(i=0; i<4; i++) { for (k=i+1; k<= 2*i+1; k++) { System.out.print( k % 10); } System.out.println(); } } }
- 02-15-2013, 11:41 AM #5
Re: Display Patterns use Nested Loops
The inner loop is wrong. You multiply by 2, but you need to multiply by i+1. Your code would be a bit more readable if you run your outer loop from 1: for( int i = 1; i <=4; i++ ). Now you can see exactly what its bounds are without having to resort to i+1 all the time. But again, that's merely my personal preference.
Here's the loop in calculated form:
Java Code:(1*1) (1*2) (2*2) (1*3) (2*3) (3*3) (1*4) (2*4) (3*4) (4*4)
You also don't need the % operator. If your loops are correct, you only need to print i*k.
- 02-15-2013, 12:08 PM #6
Member
- Join Date
- Feb 2013
- Posts
- 6
- Rep Power
- 0
Re: Display Patterns use Nested Loops
thanks again , i see where i need to alter ,
This is 2nd pattern it should looks like diamonds , i am almost done it but i face some problems were the output looks like this :
Java Code:* *** ***** *** *
Java Code:class Pattern2 { public static void main(String[] args) { for (int i = 1; i <=6; i += 2) { for (int j = 0; j < 9 - i / 2; j++) System.out.print(" "); for (int j = 0; j < i; j++) System.out.print("*"); System.out.print("\n"); } for (int i = 3; i > 0; i -= 2) { for (int j = 0; j < 5 - i / 2; j++) System.out.print(" "); for (int j = 0; j < i; j++) System.out.print("*"); System.out.print("\n"); } } }
- 02-15-2013, 03:52 PM #7
Member
- Join Date
- Jan 2013
- Location
- Kolkata,India
- Posts
- 90
- Rep Power
- 0
Re: Display Patterns use Nested Loops
your 4th pattern is easy:
Java Code:public class pattern_4 { public static void p4(int n) { int i; for(i=1;i<=n;i++) { System.out.println("*********"); } } }
- 02-15-2013, 04:11 PM #8
Member
- Join Date
- Jan 2013
- Location
- Kolkata,India
- Posts
- 90
- Rep Power
- 0
Re: Display Patterns use Nested Loops
your 2nd pattern:
Java Code:public class pattern_2 { public static void p2() { int i,j; for(i=1;i<=6;i+=2) { for(j=1;j<=i;j++) { System.out.print("*"); } System.out.println(); } i=1; for(i=1;i<=4;i+=2) { for(j=3;j>=i;j--) { System.out.print("*"); } System.out.println(); } } }
- 02-15-2013, 04:25 PM #9
- 02-15-2013, 05:35 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: Display Patterns use Nested Loops
@harshit shah: spoonfeeding is not allowed here (and neither in a lot of other technical forums); because I'm such a nice guy I'll let you get away with it but rumours say that other moderators exist who'd love to shred you in the... shudder... rotating-knives machine before banning your remains ...
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 02-15-2013, 06:13 PM #11
Member
- Join Date
- Feb 2013
- Posts
- 6
- Rep Power
- 0
Re: Display Patterns use Nested Loops
I am really appreciate , all the help but i am prefer do from scratch on my own. i post it just as a reference where i can make even more. so the 2nd pattern i do with this way.
Java Code:import java.util.Scanner; public class Square { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Welcome! This program will take your\n" + "number input, and generate a square \n" + "of stars.\n"); System.out.print("Please enter a number: "); int number = input.nextInt(); for (int i=1; i<=number; i++) { for (int j=1; j<=number; j++) System.out.print("*"); System.out.println(); } } }
- 02-15-2013, 06:18 PM #12
Member
- Join Date
- Feb 2013
- Posts
- 6
- Rep Power
- 0
Re: Display Patterns use Nested Loops
Now i am moving on to another code , which is using math formula. i got the basic but i got this error keep bugging me. Really don't know what wrong with my code, hoping someone can tell which part that i am do wrong.
Java Code:import java.util.Scanner; public class Area { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter first two points (x1, y1) :"); double x1 = input.nextDouble(); double y1 = input.nextDouble(); System.out.print("Enter second two points (x2, y2) :"); double x2 = input.nextDouble(); double y2 = input.nextDouble(); double distance = Math.pow(((x2 - x1)(x2 - x1) + (y2 - y1)(y2 - y1)), 0.5); System.out.print("The distance of the two points is " + distance); } }
- 02-15-2013, 06:23 PM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: Display Patterns use Nested Loops
@LittleJava: Don't indent your code like that: the second System.out.println() statement doesn't belong to the inner loop while the indentation suggests otherwise.
kind regards,
Jos
ps. w.r.t. your next reply: Java doesn't know about implicit multiplication signs.Last edited by JosAH; 02-15-2013 at 06:25 PM.
Build a wall around Donald Trump; I'll pay for it.
- 02-15-2013, 06:27 PM #14
Senior Member
- Join Date
- Oct 2010
- Posts
- 393
- Rep Power
- 11
Re: Display Patterns use Nested Loops
Java doesn't quite understand the mathematical notation on line 17.
(x2 - x1)(x2 - x1) should be rewritten to ((x2 - x1)*(x2 - x1)). This also applies to the second half of the equation.
Regards.
- 02-16-2013, 02:03 AM #15
Member
- Join Date
- Feb 2013
- Posts
- 6
- Rep Power
- 0
Re: Display Patterns use Nested Loops
I try to do just what you suggest but when its compile no problems
Java Code:double distance = Math.pow((((x2 - x1)*(x2 - x1)) + ((y2 - y1)*(y2 - y1))), 0.5);
Java Code:Usage: java [-options] class [args...] (to execute a class) or java [-options] -jar jarfile [args...] (to execute a jar file) where options include: -d32 use a 32-bit data model if available -d64 use a 64-bit data model if available -server to select the "server" VM -hotspot is a synonym for the "server" VM [deprecated] The default VM is server. -cp <class search path of directories and zip/jar files> -classpath <class search path of directories and zip/jar files> A ; separated list of directories, JAR archives, and ZIP archives to search for class files. -D<name>=<value> set a system property -verbose:[class|gc|jni] enable verbose output -version print product version and exit -version:<value> require the specified version to run -showversion print product version and continue -jre-restrict-search | -no-jre-restrict-search include/exclude user private JREs in the version search -? -help print this help message -X print help on non-standard options -ea[:<packagename>...|:<classname>] -enableassertions[:<packagename>...|:<classname>] enable assertions with specified granularity -da[:<packagename>...|:<classname>] -disableassertions[:<packagename>...|:<classname>] disable assertions with specified granularity -esa | -enablesystemassertions enable system assertions -dsa | -disablesystemassertions disable system assertions -agentlib:<libname>[=<options>] load native agent library <libname>, e.g. -agentlib:hprof see also, -agentlib:jdwp=help and -agentlib:hprof=help -agentpath:<pathname>[=<options>] load native agent library by full pathname -javaagent:<jarpath>[=<options>] load Java programming language agent, see java.lang.instrument -splash:<imagepath> show splash screen with specified image See http://www.oracle.com/technetwork/java/javase/documentation/index.html for more details. Process completed.
- 02-16-2013, 12:58 PM #16
Member
- Join Date
- Jan 2013
- Location
- Kolkata,India
- Posts
- 90
- Rep Power
- 0
Similar Threads
-
Nested loops, and backwards patterns
By Evildoer in forum New To JavaReplies: 5Last Post: 11-24-2012, 07:38 AM -
Using Nested Loops
By son012189 in forum New To JavaReplies: 6Last Post: 10-02-2011, 05:30 AM -
[Semi-Beginner] (nested loops) What's wrong with my code? (nested loops)
By Solarsonic in forum New To JavaReplies: 20Last Post: 03-22-2011, 04:02 AM -
Nested Loops
By ks1615 in forum New To JavaReplies: 4Last Post: 02-18-2009, 02:48 AM -
Nested loops?
By gabriel in forum New To JavaReplies: 4Last Post: 08-06-2007, 04:51 PM
Bookmarks