473,386 Members | 1,830 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,386 software developers and data experts.

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

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_VALUE;

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

System.out.println(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 1326
chaarmann
785 Expert 512MB
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
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
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...
6
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
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...
9
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...
10
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...
2
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 {...
3
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...
4
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
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...

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.