[VIEWED 1432
TIMES]
|
SAVE! for ease of future access.
|
|
|
Slackdemic
Please log in to subscribe to Slackdemic's postings.
Posted on 09-18-06 10:01
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
Hey, guys! I need to correct this code, but I can't get it to work. CORRECT THE CODE The following for loop should loop 10 times and compute the product of i times 2 plus 1 in each iteration. For example, if the loop counter is 4, the program should print 4 * 2 + 1 = 9. for ( int i = 1; i <= 10; i++ ) System.out.printf( "%d * 2 + 1 = %d", i, ( ++( i * 2 ) ) ); This is what I did: public class NewNumber { public static void main( String args[] ) { int k; for ( int i = 1; i <= 10; i++ ) System.out.printf( "%d * 2 + 1 = k\n", i); } // end for } How can I make it to calculate? It is a very simple program and has to be done in Java environment. Not complicated, but just confused..Someone with better logic?
|
|
|
|
Prakash_Ji
Please log in to subscribe to Prakash_Ji's postings.
Posted on 09-18-06 10:37
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
I think ur code prints ( i havent run ur code) 1 * 2 + 1 = k 2 * 2 + 1 = k etc If it is so then the error is clear, u never computer the value (k) you wanted to display. replace ur lines with these for ( int i = 1; i <= 10; i++ ) { k= i*2+1; System.out.printf( "%d * 2 + 1 = %d\n", i,k); } try this out and let us know. You seem to be new to Java, if so do ur homework sathi.
|
|
|
Slackdemic
Please log in to subscribe to Slackdemic's postings.
Posted on 09-18-06 10:48
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
Here's it!
|
|
|
dipankar77
Please log in to subscribe to dipankar77's postings.
Posted on 09-18-06 11:03
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
Man, I must say this Sajha forum is really something..its dynamic, versatile, accomodating, vibrant and above all there is something for everyone...so hey all you out there don't just view and bypass...put your words down and mark your territory...this is history in the making...have you come across so responsive forum like this one...if you have let us know and we will try to beat it. we will make this the voice of all nepalese living in the USA or the world for that matter!! keep posting the threads...it is the life-blood of the cyber life........
|
|
|
KaLaNkIsThAn
Please log in to subscribe to KaLaNkIsThAn's postings.
Posted on 09-18-06 11:24
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
System.out.println(i + " * " + " 2 + 1 = " + (i * 2 + 1)); garda hunna ra, C program garya style bhanda?
|
|
|
Slackdemic
Please log in to subscribe to Slackdemic's postings.
Posted on 09-18-06 11:39
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
Yes, Dipankar, although it is kind of polluted, too, it does rock!
|
|
|
Prakash_Ji
Please log in to subscribe to Prakash_Ji's postings.
Posted on 09-19-06 10:20
AM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
Kalikasthan, ur suggestion is correct. This C style programming is new to Java introduced in jdk's latest version. This version allows much more than earlier versions. And this is one of the examples to assist C programmars to write codes in Java
|
|