JAVA - take each for loop and rewrite as a while statement
a) int result = 0;
for (int i = 1; i <= 10; i++) { result = result + i;} System.out.println(result);
b) int result = 1;
for (int i = 1; i <= 10; i++) {result = i - result;} System.out.println(result);
c) int result = 1;
for (int i = 5; i > 0; i--) {result = result * i;} System.out.println(result);
d) int result = 1;
for (int i = 1; i <= 10; i = i * 2) {result = result * i;} System.out.println(result);
QA
public class Main {
public static void main(String[] args) {
//QA.
int result=0;
int i=1;
while(i<=10)
{
result=result+i;
i++;
}
System.out.println(result);
}
}
QB.
public class Main {
public static void main(String[] args) {
//QB
int result=1;
int i=1;
while(i<=10)
{
result=i-result;
i++;
}
System.out.println(result);
}
}
QC.
public class Main {
public static void main(String[] args) {
//QC.
int result=1;
int i=5;
while(i>0)
{
result=result*i;
i--;
}
System.out.println(result);
}
}
QD.
public class Main {
public static void main(String[] args) {
//QD..
int result=1;
int i=1;
while(i<=10)
{
result=result*i;
i=i*2;
}
System.out.println(result);
}
}
If you have any questions comment down. Please don't simply downvote and leave. If you are satisfied with answer, please? upvote thanks
Get Answers For Free
Most questions answered within 1 hours.