123k views
3 votes
Write a program that uses one loop to process the integers from 300 down to 200, inclusive. The program should detect multiples of 11 or 13, but not both. The multiples should be printed left-aligned in columns 8 characters wide, 5 multiples per line (See Example Output). When all multiples have been displayed, the program should display the number of multiples found and their sum.

User Imnk
by
3.5k points

1 Answer

3 votes

This question is incomplete. The complete question is given below:

Write a JAVA program that uses one loop to process the integers from 300 down to 200, inclusive. The program should detect multiples of 11 or 13, but not both. The multiples should be printed left-aligned in columns 8 characters wide, 5 multiples per line (See Example Output). When all multiples have been displayed, the program should display the number of multiples found and their sum.

Example Output

299 297 275 273 264

260 253 247 242 234

231 221 220 209 208

Answer:

// Program in Java

import java.util.*;

public class Main

{

public static void main(String[] args)

{

int sum = 300;

int c=0;

int s=0;

while (sum >= 200 )

{

if((sum % 11 == 0) &&(sum%13!=0))//multiple of 11 but not 13

{

System.out.print(sum+" ");

c++;

s+=sum;

if(c%5==0)

System.out.println();

}

if((sum % 13 == 0) &&(sum % 11 != 0))

{ System.out.print(sum+" ");

c++;

s+=sum;

if(c % 5 == 0)

System.out.println();

}

sum = sum - 1;

}

System.out.println("Number of multiples: " + c + " \\Sum of multiples : " + s);

}

}

Explanation:

  • Initialize the sum variable with 300, the variable c with zero which will be used to store count of multiples and variable s with zero which will be used to store their sum.
  • Run while loop until sum is greater than or equal to 200.
  • Inside the while loop, check if sum is multiple of 11 but not 13. If the condition is true, increment the count of c variable and also add the sum to s variable.
  • check if c is multiple of 5 and then do a nested If statement to check if it is also multiple of 13 but not 11 again. If the condition is true, increment the c and add the value of sum to s variable.
  • Again check in an if statement whether c is a multiple of 5. If that's true decrement the sum by 1.
  • Finally print out the number of multiples and the sum of multiples.

User Mlwn
by
4.0k points