473,513 Members | 2,575 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

While loop to determine if 200 is an even number

1 New Member
Hello all, this is my first post on the Forum. I'm trying to learn Java programming, but I'm already stuck. Can someone help me with the practice exercise below? I don't yet understand loops. Thanks very much.

Write a program that makes an int variable num and sets it equal to some large number (like 200). Then use an if statement in a while loop that prints out "num is an even number" if num is even (hint: you can use (num % 2) == 0 to test if num is even. The % operator is the "modulo" operator, and it basically calculates the remainder when dividing two numbers).
Feb 24 '08 #1
3 3788
sukatoa
539 Contributor
Hello all, this is my first post on the Forum. I'm trying to learn Java programming, but I'm already stuck. Can someone help me with the practice exercise below? I don't yet understand loops. Thanks very much.

Write a program that makes an int variable num and sets it equal to some large number (like 200). Then use an if statement in a while loop that prints out "num is an even number" if num is even (hint: you can use (num % 2) == 0 to test if num is even. The % operator is the "modulo" operator, and it basically calculates the remainder when dividing two numbers).
Is 0 = 200 % 1? right? (if any number more than 0 % 1? always 0?)
Is 0 = 200 % 2? right? (if any number more than 0 % 2? always 0?)
Is 0 = 200 % 3? wrong?(if any number more than 0 % 3? always 0?)

You can get some ideas for algo here....

(hint: you can use (num % 2) == 0 to test if num is even. The % operator is the "modulo" operator, and it basically calculates the remainder when dividing two numbers).

Actually you can make it without using while loop. and it is at optimized state

correct me if im wrong,
sukatoa, Shadow shaman
Feb 25 '08 #2
hsn
237 New Member
just use % to figure out if the number is even or odd
Feb 25 '08 #3
farhanm84
2 New Member
Hello all, this is my first post on the Forum. I'm trying to learn Java programming, but I'm already stuck. Can someone help me with the practice exercise below? I don't yet understand loops. Thanks very much.

Write a program that makes an int variable num and sets it equal to some large number (like 200). Then use an if statement in a while loop that prints out "num is an even number" if num is even (hint: you can use (num % 2) == 0 to test if num is even. The % operator is the "modulo" operator, and it basically calculates the remainder when dividing two numbers).
If you are going to check the set of integer numbers then you can go with either while loop or for loop. Since it is given that to check a single integer whether it is an Even or not you can directly assign a value to an integer varibale num and perform the if condition check and then print the output as required.
Feb 25 '08 #4

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

Similar topics

2
2845
by: Jim Cobban | last post by:
I am using Xerces to read an XML file and load it into a DOM so I can update it and subsequently serialize the updated DOM. The problem I have is that as I traverse the DOM I would like to inform the user of exceptional conditions in the file. However I cannot find any way while doing a DOM traversal to determine the line number that a...
17
5894
by: Pushkar Pradhan | last post by:
I want to time my matrix multiply code (in MFLOPS). I want to run the code 100,000 times or some other big number. This can be done 2 ways (for and while loops): timer1 = time(NULL); for(n = 0; n < 100000; n++) mm(); timer2 = time(NULL); exectime = difftime(timer2, timer1);
147
10016
by: Michael B Allen | last post by:
Should there be any preference between the following logically equivalent statements? while (1) { vs. for ( ;; ) { I suspect the answer is "no" but I'd like to know what the consensus is
12
1962
by: Howard | last post by:
Hello everyone (total VB.NET beginner here), I'm reading the "SAMS Teach Yourself VB.NET In 21 Days" book, and came across an exercise that I can't get to work. The exercise asks that you create a game that makes the user guess a number from 1-100, and you tell the user "lower" or "higher" as they input their guesses, until they guess the...
10
6744
by: pamelafluente | last post by:
Hi I have a sorted list with several thousands items. In my case, but this is not important, objects are stored only in Keys, Values are all Nothing. Several of the stored objects (might be a large number) have to be removed (say when ToBeRemoved = true). class SomeObj ToBeRemoved be a boolean field end class
6
2411
by: Shawn Minisall | last post by:
I've been having some problems with using a while statement for one menu within another while statement for the main menu, first time I've done it. It's with choice number two from the menu. When I run the program, I get a UnboundLocalError: local variable 'ai' referenced before assignment. I initialize ai as "", but then it just skips to...
4
3501
by: danbuttercup | last post by:
Hi everyone I just recently learnt how to do while loops in my java class and I am completely lost. I have to make programs for the following questions but I have no idea were to start. 1.Several pairs (x,y) of numbers are to keyed in. Any pair with x=y terminates the loop. Determine and display counts of how many pairs satisfy x<y and how...
1
1347
by: sonyb | last post by:
In this program , I informed the user to enter 3 random numbers .. Each one of these numbers were taken and entered into an function (2x + 5) and after the 3rd number was inserted into this equation , the user was asked to figure out what function was used to ahieve that answer. I have a while loop, that compares the selection to the...
2
7586
by: cmb3587 | last post by:
I am having a problem with the validation of the account number and password. The beginning of the program asks for users account # then pwd. The program is then supposed to go to a checkID method that checks the account number and pwd with 3 stored accounts (each account/pwd/balance is stored as one long string) so the checkID method breaks...
0
7270
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7178
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7397
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7128
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7543
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5103
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3255
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3242
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
473
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.