How do I write this program (java)?
Write a program that reads 3 integers from the console, call them x, y, and z. The program should then print
the values x, x+y, x+2y, x+3y, … as long as the value printed is less than or equal to z. For example, if the inputs were
10, 4, and 20, then the program would print 10 14 18 and if the inputs were 3 5 18 then the program would print 3 8 13
18.
Comments
import java.io.*;
class blahh
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader ( new InputStreamReader (System.in));
int x,y,z;
int i=0;
System.out.println("Enter value of x: ");
x=Integer.parseInt(br.readLine());
System.out.println("Enter value of y: ");
y=Integer.parseInt(br.readLine());
System.out.println("Enter value of z: ");
z=Integer.parseInt(br.readLine());
while( z < ( x + (i*y) ) )
{
System.out.print( ( x + (i*y) ) + " ");
i++;
}
}
}
Just one point to tell, we cannot use do-while here, because if we use it then there will be an error.
Consider x=8, y=6, z=7. In this case no value should be printed, thus using while helps.
here is some starter code for you for this application
_____________________________________________
import java.util.Scanner;
public class ProgramXYZ {
public static void main(String[] args) {
//declare variables / objects
Scanner input = new Scanner(System.in);
int x = 0, y = 0, z = 0;
int value = 0, index = 0;
System.out.println("please enter x ");
x = input.nextInt();
System.out.println("please enter y ");
y = input.nextInt();
System.out.println("please enter z ");
z = input.nextInt();
while ((x + index * y) <= z)
{
value = x + index * y;
System.out.println(" " + value);
index++;
}
}
}