For Loop - Print All Odd Numbers Between FirstNum And SecondNum
Feb 4, 2015
Prompt the user to enter two ints, firstNum and secondNum. Then print all odd numbers between firstNum and secondNum.
Here's my code-- my error is in this line: for(firstNum;firstNum<=secondNum;firstNum++)
The error is "not a statement."
import java.io.*;
import java.util.*;
public class ListOddNumbers
{
public static void main(String[] args) {
Scanner keyboard= new Scanner(System.in);
System.out.println("Enter your first number.");
int firstNum= keyboard.nextInt();
[Code] .....
View Replies
ADVERTISEMENT
Apr 3, 2015
I want to write a while loop to print all odd numbers between 1 and 100.
But it gives error. Where is the mistake?
public class pro {
public static void main(String[] args) {
// TODO Auto-generated method stub
int c;
int x=1;
[Code] ....
View Replies
View Related
Jan 27, 2015
I am trying to print a loop inside an array of a greater size than the loop itself. Like for example I have an array of size 7 but it has only 3 elements.
int[] array=new int[7]
array[0]=2;
array[1]=3;
array[2]=4;
now what I want to do is print these three numbers in a loop so that my array[3]=2;array[4]=3;array[5]=4 ...... till the last one. Also the array could be any size and not just 7.
View Replies
View Related
Aug 22, 2014
I tried to create file and write the output of my program in it in java when i use WriteLong then the file does not contain long value, how I create this file my program is to print prime numbers between 500000 to 10000000
public class primenumber {
public static void main(String[] args) {
long start = 5000000;
long end = 10000000;
System.out.println("List of prime numbers between " + start + " and " + end);
for (long i = start; i <= end; i++) {
if (isPrime(i)) {
System.out.println(i);
[Code] ....
View Replies
View Related
Feb 1, 2015
I'm trying to make a program that generates 20 random integers between 1 and 20 and then prints the list of random numbers to the screen. After that, I want to print a different list to screen with the same numbers from the first list only skipping any number that has been already printed to the screen. So two lists are printed to the screen. The first one has 20 random numbers. The second one has those same 20 numbers but only prints the numbers in the first list that aren't duplicated. So if m
y list of 20 random integers contains three 2s and two 14s, only one 14 and one 2 is printed to the second list. Currently, my code generates 20 numbers from 1 to 20 and stores those numbers in an array but I don't know how to print solve the second part of my problem. I don't know how to print the s different list only without duplicate numbers. As a result, my output is nothing because it doesn't print any number from the first list as oppose to skipping only duplicate one.
public void randomNum(){
System.out.println("Twenty random integers: ");
int max = 20; // max value for range
int min = 1; // min value for range
Random rand = new Random();
int[] all = new int[20];
[Code] ....
View Replies
View Related
Jul 25, 2014
I want to create two thread one thread will print odd number and another thread will print even number. but the number should be in a sequence like
1 2 3 4 5 6 7 8 9 10
View Replies
View Related
Mar 6, 2015
I amtrying to iterate a value of L,R both are range of BigInteger but its not working
BigInteger L=in.nextBigInteger();
BigInteger R=in.nextBigInteger();
for (BigInteger bi =L;
bi<=bi.compareTo(R);
bi = bi.add(BigInteger.ONE)) {
//Task to do with Numbers
}
I am trying to iterate a for loop in range of L and R but its not working
View Replies
View Related
Mar 26, 2014
I am currently trying to print a Diamond using for-loops but I seem to be confusing my self / over-complicating it.
This is what I have so far [URL]). When I run the code I get [URL].
I want to duplicate it on to the other side but I can't seem to be able to figure out a working for-loop to print it out.
Also, is there a faster/efficient method of doing something like this?
View Replies
View Related
Oct 20, 2014
On line 45 i am trying to add "-" symbol to my output phone number. As of now the out prints like "1800*45*3569377. I want it to print 1800-3569377, or even more ideal: 1-800-356-9377. When printing normally (system.out.println) I can print symbols, but when I try to print from a for-loop it says "unclosed character literal." import java.util.Scanner;
public class Phone_010473030 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a phone number string: ");
String Phone_num = input.nextLine();
Phone_num = Phone_num.toUpperCase();
[code]....
View Replies
View Related
Feb 6, 2015
Am having trouble understanding how to print my results (I have 50 of them) , only 10 per line.
Im using an array that is 1-50. The first 25 I raise to the power of 2 and print it.
The next 25, I multiple by 3 and print it
Public class KDowling {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
double [] alpha = new double[50]; //declare an array 50 indexes
int num=1; // declare variable num
int counter=1;
[Code] .....
I need to get this result:
1 4 9 16 25 36 49 64 81 100
121 144 169 196 225 256 289 324 361 400
441 484 529 576 625 78 81 84 87 90
etc...
Right now they are each printing out on their own line.
View Replies
View Related
Oct 27, 2014
I've almost got this to work. Right now it prints out one comma for the first set of 3 numbers. For exmaple, if the user input is 12345678 the code will print out 12345,678. I'm trying to figure out how to get it to print out commas for the rest of the numbers. My first thought was to use a loop but I can't get it to work....
private static void printWithCommas(NaturalNumber n, SimpleWriter out) {
assert n != null : "Violation of: n1 is not null";
assert out != null : "Violation of: out is not null";
assert out.isOpen() : "Violation of: out.is_open";
if (n.compareTo(THOUSAND) < 0) {
out.print(n);
[Code] .....
View Replies
View Related
Mar 11, 2015
Here is what I am trying to do:
Write a program to print the even numbers and the odd numbers between 0 and 30 using a single thread and then again using multiple threads.
I already finished the single-threaded program but am having trouble with the multi-threaded one. I have three classes; one class for odd numbers, one for even numbers, and one to execute the code. Here is my code so far:
Even Numbers:
Java Code:
public class EvenNumbers extends Thread {
public void run() {
for (int i=1; i<=30; i++) {
if (i%2 == 0) {
System.out.println("Even number " + i);
[Code] ....
Unfortunately, my output ends up looking a little weird:
Java Code:
C:UsersREDACTEDDropboxSchoolworkREDACTEDJava ProgrammingUnit 5 - Exception
Handling, AssertionsProgram - Thread>java MultiThread
Even Numbers:
Odd Numbers:
Even number 2
[Code] .....
View Replies
View Related
Jan 20, 2014
I just started taking a Java class and i'm having problems with this code,
I'm trying to add up the 4 numbers have them print out on screen.
public class finalGrade
{
public static void main(String[] arg)
{
double pLp = 28.3; //Participation, lab work, and programming
[Code] .....
View Replies
View Related
Jul 18, 2014
There's loads of problems with this. What I'm trying to do.
1) get a program to add the contents of an array together, preferably with a for loop and not the heavy handed version I've tried to use here.
2) get the for loop's output just once, since it won't compile or recognise the variable outside of the loop. How do I make the loop's 'counter' variable available everywhere?
public class retint {
public static void main(String[] args){
int[] onetoTen = {1,2,3,4,5,6,7,8,9,10};
for (int i=0; i<10; i++) {
int counter = (onetoTen[0] + onetoTen[1] + onetoTen[2] + onetoTen[3] + onetoTen[4] + onetoTen[5] +
onetoTen[6] + onetoTen[7] + onetoTen[8] + onetoTen[9]);
System.out.println(counter);
}
}
Terrible code, I know. There has to be a more efficient way.
View Replies
View Related
Jul 7, 2014
I am trying to make a program that prints triangle... and I did various test on each method to realise that the problem lies with this segment.When I call this method, nothing prints out, I figure there is something with the loop that I am not realizing.the loop is backwards because it's supposed to have the right side edge parralel (when I try to print it out the spaces do not appear, imagine the x are space...), so as each line is looped the # of spaces diminishes
xxxx*
xxx*x*
xx*xx*
x*xxx*
*****
public class test {
public static void main(String[] args){
for (int countdown = 5; countdown <= 1; countdown = countdown--){
showNTimes(countdown, ' ');
showNTimes(5- countdown, '*');
System.out.println("");
}
}
public static void showNTimes ( int nbTimes, char carac ) {
for ( int i = 1 ; i <= nbTimes ; i = i + 1 ) {
System.out.print( carac );
}
}
}
View Replies
View Related
Jan 1, 2015
This is my code
package com.arraydemo;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
public class ArrayStructures {
public long[] theArray;
public int arraySize;
public ArrayStructures(int size)
[code]....
and i am getting this output
----------
! 1712 !2156|
----------
! 1713 !4583|
----------
! 1714 !3981|
----------
[HENRY: 6000+ LINES DELETED -- Isn't it a bit ridiculous hard to read when you flood a post with thousands of output lines?]
----------
! 4998 !3094|
----------
! 4999 !836|
----------
12:10:56
I am expecting Number to be print from Index ) but they are not printing ,why?
If I use arraysize like 2000 I am getting all number starting from 0 to 1999 .why?
View Replies
View Related
May 3, 2014
I am trying to cast an integer into a double. it works but I need two decimal number after the dot and for example I can print 7.51 but how can I get it to print 7.50 and not 7.5
public int compareTo(Money o){
int d1 = (this.dollars * 100) + this.cents;
int d2 = (o.dollars * 100) + o.cents;
return d1 - d2;
}
On my main method I have
public static void main(String[] args){
Money money1 = new Money(10, 49);
Money money2 = new Money(2, 99);
System.out.println("result: " + "$" + (double)money1.compareTo(money2)/100);
}
It prints 7.5 but I want it to print 7.50, how do I do this .....
View Replies
View Related
Jun 1, 2014
i want to print triangle shape using number like
this
1
12
123
1234
12345
123456
this is my code
class shape{
public static void main(String [] args){
for(int x =0 ; x<=6;x++){
for(int y =0 ; x > y ; y++){
System.out.print(x);
}
System.out.print("
");
}
}
}
but my output is
1
22
333
4444
55555
666666
View Replies
View Related
Sep 29, 2014
I am able to get the integer in the array printed but I am lost on how to hold and print the distinct numbers.
This program will read in 10 numbers from the user via the console. It will display on the console only the distinct numbers, i.e. if a number appears multiple times in the input it is displayed only once.
Here is a sample test run:
Enter an integer: 2
Enter an integer: 6
Enter an integer: 1
Enter an integer: 8
Enter an integer: 6
Enter an integer: 1
Enter an integer: 2
Enter an integer: 4
Enter an integer: 7
Enter an integer: 5
The number of distinct values is 7
2 6 1 8 4 7 5
View Replies
View Related
Mar 27, 2014
Where I am doing mistake to print the numbers of Armstromg numbers requested.
import java.util.Scanner;
public class ArmStrongNumber {
public static void main(String[] args) {
// TODO Auto-generated method stub
int x, y, z=0, temp, temp1=1;
[Code] .....
View Replies
View Related
Jan 24, 2014
write a program that would ask for the five names using loop statement and print all names .
View Replies
View Related
Oct 1, 2014
I am working on my java program it prints out a table of angles and the sin, cos, and tan. For extra credit he wanted us to use nest loops to create a space after every five lines. I have come real close to getting this to work but have a problem with the very end of the table. The table needs to stop at 180...
public static void printTable(double angle,double sin,double cos,double tan) { // Method to create a table for the values
for (double j = 0; j <= 180;) {
for (double i = 1; i <= 5; i++) {//loop to print five lines of code
angle = Math.toRadians(j);
[Code] .....
Here is the end of the output of the table:
175.00.0872-0.9962-0.0875
176.00.0698-0.9976-0.0699
177.00.0523-0.9986-0.0524
178.00.0349-0.9994-0.0349
179.00.0175-0.9998-0.0175
180.00.0-1.00.0
181.0-0.0175-0.99980.0175
182.0-0.0349-0.99940.0349
183.0-0.0523-0.99860.0524
184.0-0.0698-0.99760.0699
View Replies
View Related
Oct 9, 2014
import java.util.Scanner;
public class Arrayunion {
/**
* @param args
[Code].....
View Replies
View Related
Oct 15, 2014
I created a random number generator for my course and it works perfectly. I now need to print only the top 15 highest numbers of occurrence from the RNG output. How i can do this?
Here is my block of code.
package section4;
import java.util.Random;
public class Lottery {
public static void main(String[] args) {
Random rand = new Random();
int freq[] = new int[51];
[Code] ....
View Replies
View Related
Aug 2, 2014
2.Check the Vehicle...Assume that vehicles are going through a two-way traffic intersection. There are three types of vehicles: car, motor bikes and trucks. Generate a series of 10 random integers, between 1 and 3, inclusive. The numbers represent the type of vehicle as stated below:
NumberVehicle Category
1 Car
2 Motor bikes
3 Trucks
Write a program, using a for loop, to count how many vehicles going through the traffic intersection are cars, motor bikes and trucks. Then, the program should print out the numbers for each vehicle category. There is no user input for this program.
View Replies
View Related
Jul 5, 2014
Assume that vehicles are going through a two-way traffic intersection. There are three types of vehicles: car, motor bikes and trucks. Generate a series of 10 random integers, between 1 and 3,inclusive.The numbers represent the type of vehicle as stated below:
NumberVehicle Category
1Car
2Motor bikes
3Trucks
Write a program, using a for loop, to count how many vehicles going through the traffic intersection are cars, motor bikes and trucks. Then, the program should print out the numbers for each vehicle category. There is no user input for this program. How do i do it so they will add up the sum of each vehicle?
The answer should be something like
Number of cars = X
Number of motor bikes = Y
Number of Trucks = Z
but i'm getting
Total number of vehicle:
cars
motorbikes
motorbikes
cars
Trucks
Trucks
motorbikes
motorbikes
Trucks
cars
public static void main(String[] args) {
[code]....
View Replies
View Related