Computer Science, asked by uttamstar07, 9 months ago

int
for (i=5;i<=25;i=i+3) ;
Sopln(i)
{​

Answers

Answered by rkpr587
1

Explanation:

Convert each of the following while-loops to a for-loop.

int num = 0;

while (num < 12)

{

if (num % 2 == 0)

System.out.println(num);

num++;

}

ANSWER:

int num;

for (num = 0; num < 12; num++) // or for (int num = 0; num < 12; num++)

{

if (num % 2 == 0)

System.out.println(num);

}

int num = 20;

int i = 1;

while (num < 35 && num > 5)

{

if (i % 2 == 0)

num += 2 * i;

else

num -= 3 * i;

System.out.println(i + ", " + num);

i++;

}

ANSWER:

int num = 20;

for (int i = 1; num < 35 && num > 5; i++)

{

if (i % 2 == 0)

num += 2 * i;

else

num -= 3 * i;

System.out.println(i + ", " + num);

}

// or another way, with multiple initialization

for (int num = 20, i = 1; num < 35 && num > 5; i++)

{

if (i % 2 == 0)

num += 2 * i;

else

num -= 3 * i;

System.out.println(i + ", " + num);

}

What is the output of each of the following code segments?

for (int i = 0; i < 50; i = i + 5)

{

if ( (i * 2) % 4 == 0)

System.out.println(i);

}

ANSWER:

0

10

20

30

40

for (int i = 0; i < 3; i++)

for (int j = 0; j < 2; j++)

System.out.println("i = " + i + ", j = " + j);

ANSWER:

i = 0, j = 0

i = 0, j = 1

i = 1, j = 0

i = 1, j = 1

i = 2, j = 0

i = 2, j = 1

for (int i = 0; i < 3; i++)

{

System.out.println("i = " + i);

for (int j = 0; j < 2; j++)

System.out.println(" j = " + j);

}

ANSWER:

i = 0

j = 0

j = 1

i = 1

j = 0

j = 1

i = 2

j = 0

j = 1

sum = 0;

for (int i = 0; i <= 3; i++)

for (int j = 5; j > i; j--)

sum += j;

System.out.println(sum);

ANSWER:

50

because the execution proceeds in the following way:

i j sum

0 5 5

0 4 9

0 3 12

0 2 14

0 1 15

1 5 20

1 4 24

1 3 27

1 2 29

2 5 34

2 4 38

2 3 41

3 5 46

3 4 50

What does the method mys return when called with an argument of 6? Choose one that applies.

public static int mys(int n)

{

int k = n;

int s = 1;

while (k > 1)

s *= k--;

return s;

}

121

520

720

5043

ANSWER: c (720 because k--; returns the value before the decrement, thus 6 * 5 * 4 * 3 * 2 = 720)

Write a program to print all of the capital letters in a line of input. For example, here is the sample session of the program.

Enter a line of input: This is the End of CSC 211

TECSC

ANSWER:

import java.util.Scanner;

public class PrintCaps

{

public static void main(String[] args)

{

Scanner sc = new Scanner(System.in);

System.out.print("Enter a line of input: ");

String input = sc.nextLine(); // receive a whole line

for (int i = 0; i < input.length(); i++)

if (Character.isUpperCase(input.charAt(i)))

System.out.print(input.charAt(i)); // print one char at a time

System.out.println(); // to bring the cursor back

}

}

Write a program to print n lines of output, where the ith line of output consists of i asterisks. For example:

Enter a positive integer: 8

*

**

***

****

*****

******

*******

********

ANSWER: This is an interesting application which uses a nested loop.

import java.util.Scanner;

public class PrintAsterisks

{

public static void main(String[] args)

{

Scanner sc = new Scanner(System.in);

System.out.print("Enter a positive integer: ");

int num = sc.nextInt();

// The outer-loop for rows. Conceptually easier to start index (i) from 1.

for (int i = 1; i <= num; i++)

{

// i is the number of asterisks to print.

// Use an inner loop to print that many asterisks.

// The index (j) can start from 0 and goes until (and not including) i.

for (int j = 0; j < i; j++)

System.out.print('*'); // print one asterisk

System.out.println(); // to bring the cursor back

}

}

}

Similar questions