[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 1414 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 60 days
Recommended Popular Threads Controvertial Threads
शीर्षक जे पनि हुन सक्छ।
NRN card pros and cons?
What are your first memories of when Nepal Television Began?
Basnet or Basnyat ??
nrn citizenship
Sajha has turned into MAGATs nest
Nas and The Bokas: Coming to a Night Club near you
डीभी परेन भने खुसि हुनु होस् ! अमेरिकामाधेरै का श्रीमती अर्कैसँग पोइला गएका छन् !
3 most corrupt politicians in the world
if you are in USA illegally and ask for asylum it is legal
Top 10 Anti-vaxxers Who Got Owned by COVID
निगुरो थाहा छ ??
Poonam pandey - death or suicide?
ढ्याउ गर्दा दसैँको खसी गनाउच
Doctors dying suddenly or unexpectedly since the rollout of COVID-19 vaccines
काेराेना सङ्क्रमणबाट बच्न Immunity बढाउन के के खाने ?How to increase immunity against COVID - 19?
Travelling to Nepal - TPS AP- PASSPORT
TPS Work Permit/How long your took?
How long does Citizenship take?
Will MAGA really start shooting people?
Nas and The Bokas: Coming to a Night Club near you
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