Hey, I been doing good in Java programming until the algorithm workbench of Java
ID: 3533439 • Letter: H
Question
Hey, I been doing good in Java programming until the algorithm workbench of Java programming ( loops and etc ). I need your help please. Thank you in advance
1)Write a while loop that lets the user enter a number. The number should bo multiplied
by 10, and the result stored in the variable product. The loop should iterate as long
as product contains a value less than 100.
2)Write a do-while loop that asks the user to enter two numbers. The numbers should
be added and the sum displayed. The loop should ask the user whether he or she
wishes to perform the operation again. If so, the loop should repeat; otherwise it
should terminate.
3) Write a loop that asks the user to enter a number. The loop should iterate 10 times
and keep a running total of the numbers entered.
11) Write an input validation loop that asks the user to enter a number in the range of I
through 4.
13) Write nested loops to draw this pattern:
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
Explanation / Answer
1)
Scanner sc =new Scanner(System.in);
int product=0;
while(p<100)
{
int no= sc.nextInt();
product = no*10;
}
2)
Scanner sc =new Scanner(System.in);
int product=1;
do{
System.out.println("Enter 2 numbers: ");
int n1= sc.nextInt();
int n2= sc.nextInt();
System.out.println("Sum of the two numbers is: "+(n1+n2));
System.out.println("Do you want to continue?: (y/n) ");
char opt=sc.next().charAt(0);
}while(opt=='y');
3)
Scanner sc =new Scanner(System.in);
int sum=0;
for(int i=0;i<10;i++)
{
System.out.println("Enter a number: ");
int n= sc.nextInt();
sum =sum+n;
}
13)
int i,j;
for(i=7 ;i>0; i--)
{
for(j=i; j>=1; j--)
{
System.out.print("*");
}
System.out.println("");
}
11)
Scanner sc =new Scanner(System.in);
for(int i=0;;i++)
{
System.out.println("Enter range: ");
int n= sc.nextInt();
System.out.println("The range is: "+n);
System.out.println("Do you want to continue?: (y/n) ");
char opt=sc.next().charAt(0);
if(opt=='n')
{
break;
}
}
//Please rate