473,604 Members | 2,487 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

While Loop that Outputs Lowest Integer Divisible by 5 and 7 in decrement.

1 New Member
Hi, I'm trying to make a code that gives me ONLY the lowest integer divisible by 5 and 7 (which should be 35) in decrement. I've been trying it for hours and I just don't understand what I'm doing wrong. Below is what I have written so far. Can anyone please help me review this and see what I'm doing wrong?


public class FiveAndSeven {

public static void main(String[] args) {
int i = 201;
int smallestNum = Integer.MAX_VAL UE;

while (i > 1) {
i--;
if (i % 5 == 0 && i % 7 == 0 && i < smallestNum) {
smallestNum = i;

System.out.prin tln(i);
}


}

}
}

I wanted to move the category and wanted to delete this post but not sure how, sorry for duplicate :(
Oct 11 '16 #1
1 1339
chaarmann
785 Recognized Expert Contributor
You are printing out ALL integers, because you put your print-statement into the while-loop. If you put it outside the loop, just after the closing bracket of the while-loop, then you will see only the smallest (last) result.

By the way, you can delete the "&& i < smallestNum" from your if-statement, because this is always true. (You are decrementing the variable i with each iteration in the while-loop, so how could it be possible that i will be bigger than before?
Oct 12 '16 #2

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

Similar topics

9
4164
by: JS | last post by:
#include <stdio.h> main(){ int c, i, nwhite, nother; int ndigit; nwhite = nother = 0; for (i = 0; i < 10; ++i)
15
9139
by: PagCal | last post by:
Is this language missing the functionality of a C/C++ 'continue' statement? For example: While NOT isEof() If condition ' a C or C++ continue would work here ' but we are forced to use a GoTo GoTo nxt End If
6
71958
by: John Pass | last post by:
What is the difference between a While and Do While/Loop repetition structure. If they is no difference (as it seems) why do both exist?
6
26241
by: obdict | last post by:
Hello, I used scanf() in a while loop, which ensures that user input is valid (must be an integer no greater than 21 or less than 3). If user enters a number out of the range, or enters non-number, he/she will be asked to retry. /* start */ int n;
9
3941
by: Cybex | last post by:
I am trying to get this to work but when ever I enter an proper integer it just hangs. The Switch default seems to catch the improper integers but the right ones are not triggering the way I thought they would. Any help would be appreciated... #include <iostream> #include <string>
10
2139
by: newbiemom | last post by:
I am totally confused I cant even get started and my assignment is getting later by the day. We have to use a while loop to do several things. This is the source code I have so far , can someone tell me where to go from here This is me trying to print odd numbers from 2 numbers input by the user. I also have to sum and print the even numbers. I have to print squares of numbers between 1 and 20. // Source Code # include <iostream> using...
2
6244
by: Mike Langworthy | last post by:
I am having a problem Breaking out of the while loop, it never terminates. any suggestions using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program
3
1462
by: LuK2007 | last post by:
Hey all, I would be delighted if someone could help me complete my coding, im trying to make it able for the user to type in as many percentages and display the coding for the grade as many times as they like using a WHILE LOOP and making the LOOP end when the user hits cancel! I got most of the coding just need a small snippet to get it fully function - any help will be GREATLY appreciated //here is so far: import...
4
3088
by: Haris Radoncic | last post by:
I picked up the "C Programming Language" and have been learning it bit by bit but Im alittle confused about EOF. I example 1-8, we need to write a program that outputs the number of tabs, blanks, and newlines entered into the program, using a while loop to control when the program performs the outputs. I know that EOF is a negative value (-1 apparently), but how do I enter it into the program so that the while loop ends since every keystroke is...
0
7997
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
7929
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8409
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...
1
8065
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8280
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6739
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...
0
5441
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
3955
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1526
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.