473,672 Members | 2,594 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 3794
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
2856
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 particular Node occurs on, except to count all of the new-line characters which occur in Text Nodes. ...
17
5913
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
10087
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
1975
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 correct number, at which point you then tell the user "Correct". I tried using the While Loop, and...
10
6761
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
2415
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 the you choose scissors I choose and nothing shows up. As soon as I take away the while again ==...
4
3508
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 many pairs satisfy x>y. 2.Two numbers x and y are to be keyed in. If the sum of x and y is greater...
1
1354
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 function (2x + 5) . I gave the user 3 trys to figure it out , and if he failed on the 3rd round , I am...
2
7668
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 up the string into its seperate parts and then checks if the input matches. If it does match one of...
0
8504
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8946
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8849
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7477
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6260
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5720
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4243
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2094
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.