[Show all top banners]

Slackdemic
Replies to this thread:

More by Slackdemic
What people are reading
Subscribers
:: Subscribe
Back to: Kurakani General Refresh page to view new replies
 Java Programming Help
[VIEWED 1432 TIMES]
SAVE! for ease of future access.
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?
 
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.
 
Posted on 09-18-06 10:48 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 
 
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........
 
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?
 
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!
 
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
 


Please Log in! to be able to reply! If you don't have a login, please register here.

YOU CAN ALSO



IN ORDER TO POST!




Within last 30 days
Recommended Popular Threads Controvertial Threads
TPS Re-registration case still pending ..
मन भित्र को पत्रै पत्र!
अमेरिकामा बस्ने प्राय जस्तो नेपालीहरु सबै मध्यम बर्गीय अथवा माथि (higher than middle class)
They are openly permitting undocumented immigrants to participate in federal elections in Arizona now.
Driver license help ASAP sathiharu
ढ्याउ गर्दा दसैँको खसी गनाउच
TPS Reregistration and EAD Approval Timeline.......
nrn citizenship
जाडो, बा र म……
Changing job after i-140 approval
Nepali **fake** Veterans. Be aware!!
Trasiting through Istanbul, Turkey
lost $3500 on penny stocks !!!
Is this a progressive step?
Nepalese Students Face Deportation over Pro-Palestine Protest
NOTE: The opinions here represent the opinions of the individual posters, and not of Sajha.com. It is not possible for sajha.com to monitor all the postings, since sajha.com merely seeks to provide a cyber location for discussing ideas and concerns related to Nepal and the Nepalis. Please send an email to admin@sajha.com using a valid email address if you want any posting to be considered for deletion. Your request will be handled on a one to one basis. Sajha.com is a service please don't abuse it. - Thanks.

Sajha.com Privacy Policy

Like us in Facebook!

↑ Back to Top
free counters