Question

Java questions Part 1 Express the code fragment shown below using only a for loop. int...

Java questions

Part 1

Express the code fragment shown below using only a for loop.

int n = 0;

while (n <= 100)

{

int sum = sum + n;

n = n + 1;

System.out.println (sum);

}

Part 2

Use a loop structure to determine and print all positive integers between 1 and 100 that are divisible (evenly) by 5 and 8

0 0
Add a comment Improve this question Transcribed image text
Answer #1

PART 1:

The code fragment in a for loop can be expressed as -

int n ;

for(n=0;n<=100;n=n+1)

{

int sum = sum + n;

System.out.println (sum);

}

PART 2:

//Required java code with loop structure

class divisibility{
    
    // Result function with N
    static void result(int N)
    {    
        // iterating from 1 to N
        for (int num = 1; num <= N; num++) //using loop structure
        {    
            if (num % 5 == 0 && num % 8 == 0)
                System.out.print(num + " "); //printing output
        }
    }
     
    // Driver program
    public static void main(String []args)
    {
       
        int N = 100;
         
       
        result(N); // Calling function
    }
}

Please refer to the screenshot of the code to understand the indentation of the code


{ 1. class divisibility{ 2 3 // Result function with N 4 static void result(int N) 5 6 // iterating from 1 to N 7 for (int nu

//OUTPUT:

ostdout copy 40 80

Hope this helped. Please do upvote and if there are any queries please ask in the comments section.

Add a comment
Know the answer?
Add Answer to:
Java questions Part 1 Express the code fragment shown below using only a for loop. int...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • What is the output of the following code fragment? int i = 1; while( i <=...

    What is the output of the following code fragment? int i = 1; while( i <= 5 ) if(i == 2 || i == 4) System.out.println(i + ":" + " is an even index) System.out.println("i: " + i); i++;

  • A. What is the output of the following C++ code fragment? (all variables are of type...

    A. What is the output of the following C++ code fragment? (all variables are of type int) int count-1; int y-100; while (count 3) y=y-1 ; count+t cout << y << endl; cout<< count <<endl What is the value of x after control leaves the following while loop? (all variables are of type int) B. int x0 while (x < 20) sum- sum+x cout << sum<< endl;

  • in c++ language 1.Re-write the following for loop statement by using a while loop statement: int...

    in c++ language 1.Re-write the following for loop statement by using a while loop statement: int sum = 0; for(int i=0;i<=1000;i++){                 sum+=i; } 1 (b). Following is the main () function of a program. The program asks a user to enter 150 integers and print the largest integer user entered. int main() {    int score, highest;             //missing portion of the program             return 0;    } 1(c). Find the missing portion of the following code, so the...

  • 1(5 pts): For each code fragment below, give the complexity of the algorithm (O or Θ)....

    1(5 pts): For each code fragment below, give the complexity of the algorithm (O or Θ). Give the tightest possible upper bound as the input size variable increases. The input size variable in these questions is exclusively n. Complexity Code public static int recursiveFunction (int n)f f( n <= 0 ) return 0; return recursiveFunction (n - 1) 1; for(int i 0i <n; i+) j=0; for ( int j k=0; i; k < < j++) for (int j; m <...

  • (a)How many times does the code snippet given below display "Hello"? int x = 1; while...

    (a)How many times does the code snippet given below display "Hello"? int x = 1; while (x != 15) {    System.out.println ("Hello");    x++; } (b)What is the output of the following code fragment? int i = 1; int sum = 0; while (i <= 5) {    sum = sum + i;    i++; } System.out.println("The value of sum is " + sum); Quie 2 What is the output of the following snipped code? public class Test {...

  • Convert the following while loop into a for loop. int 1 - 50: int sum-07 while...

    Convert the following while loop into a for loop. int 1 - 50: int sum-07 while (sum < 1000) sum - sum + 1; Question 35 (2 points) Saved Given an int variable k that has already been declared, use a while loop to print a single line consisting of 80 dollar signs. Use no variables other than k. int sum = 0; for(int i = 100;sum < 10000;1-- sum = sum + i;

  • I need this code in java. Loops do just what they sound like they should -...

    I need this code in java. Loops do just what they sound like they should - they perform a statement again and again until a condition is fulfilled. For this lab you will be practicing using loops, specifically a for loop. The for loop takes the form of: for(/*variable initialization*/;/*stop condition*/; /*increment*/){ //statements to be done multiple times } Task Write an application that displays every perfect number from 2 through 1,000. A perfect number is one that equals the...

  • Please use induction Consider the following code fragment: int i -1; int s=1; while (i <-...

    Please use induction Consider the following code fragment: int i -1; int s=1; while (i <- n) Show that at the start of every iteration holds using induction

  • Array manipulation (a) Write Java code for a method exchange (int [] a, int i, int...

    Array manipulation (a) Write Java code for a method exchange (int [] a, int i, int j) that exchanges the values stored at indices i and j in the array a. You do not need to worry about cases where either i or j is an invalid index. Give the best estimate you can for its time complexity (b) In an ordered array of n items, how can we determine whether or not an item belongs to the list using...

  • This question deals with extending already existing Java code via a while loop. Ask the user...

    This question deals with extending already existing Java code via a while loop. Ask the user how many year inputs he wants to check - an integer. Assume that the user always gives an input which is between 1 and 5 (inclusive). Next, ask the user for K number of year inputs where K = the number user has given as input before (inside a while loop). Check for each year input whether that year is a leap year or...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT