473,405 Members | 2,185 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Help using a For Structure

2
Hey

Im wonderin if anyone can help me with this question,Sum the odd numbers between 1 and 97, using a for structure. Might seem a bit simple but i cant get it to work. Anyone help?
Nov 9 '07 #1
3 1222
JosAH
11,448 Expert 8TB
Hey

Im wonderin if anyone can help me with this question,Sum the odd numbers between 1 and 97, using a for structure. Might seem a bit simple but i cant get it to work. Anyone help?
So you have to do this: sum(i= 1 ... 97 | i where i odd)

In Java that translates to something like this:

Expand|Select|Wrap|Line Numbers
  1. int sum= 0;
  2. for (int i= 1; i <= 97; i++)
  3.    if (odd(i)) // add i to sum
  4.  
But you can do better than that and get rid of that odd(i) predicate altogether:

Expand|Select|Wrap|Line Numbers
  1. int sum= 0;
  2. for (int i= 1; i <= 97; i+= 2)
  3.   // add i to sum
  4.  
kind regards,

Jos
Nov 9 '07 #2
DaveoB
2
This is what i've done and it's throwing up 43 errors. ne ideas?

public class Project_1_0
{
public static void main(String[] args)
{



for (int i= 1; i <= 97; i+= 2)
{

System.out.print(i);
System.out.print(" ");
}

}

}
Nov 9 '07 #3
r035198x
13,262 8TB
This is what i've done and it's throwing up 43 errors. ne ideas?

public class Project_1_0
{
public static void main(String[] args)
{



for (int i= 1; i <= 97; i+= 2)
{

System.out.print(i);
System.out.print(" ");
}

}

}
1.) Use code tags when posting code (like Jos did above)
2.) What are the errors you are getting?
Nov 10 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: PHPkemon | last post by:
Hi there, A few weeks ago I made a post and got an answer which seemed very logical. Here's part of the post: PHPkemon wrote: > I think I've figured out how to do the main things like...
4
by: Jim Hubbard | last post by:
I have some C# code that is supposed to wrap the defrag APIs and I am trying to convert it to VB.Net (2003). But, I keep having problems. The C# code is relatively short, so I'll post it...
11
by: Mannequin* | last post by:
Hi all, I'm working on a quick program to bring the Bible into memory from a text file. Anyway, I have three questions to ask. First, is my implementation of malloc () correct in the program to...
13
by: Chua Wen Ching | last post by:
Hi there, I saw this article here in vb.net. http://www.error-bank.com/microsoft.public.dotnet.languages.vb.1/148992_Thread.aspx and ...
18
by: James Radke | last post by:
Hello, We are currently using a user DLL that when working in VB 6.0 has a user defined type as a parameter. Now we are trying to use the same DLL from a vb.net application and are having...
2
by: leo2100 | last post by:
Hi, I need help with this program. The program is supposed to take a text file and identify the words in it, then it should print them and count how many times a word is repeated. At first main...
0
by: Miguel Dias Moura | last post by:
Hello, I am working on an Asp.Net 2.0 / SQL 2005 web site. I am using profile to save the users info on the database. For example, I have the following structure: Public Structure Name...
8
by: skumar434 | last post by:
i need to store the data from a data base in to structure .............the problem is like this ....suppose there is a data base which stores the sequence no and item type etc ...but i need only...
4
by: =?Utf-8?B?UHVjY2E=?= | last post by:
The function that I'm trying to call through DLLImport has a parameter that has a C code's vector's Itrator to a structure. I Have marshalled the structure in C# but how do I do the C type...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.