Advertisement
thotfrnk

number1-0.java

Nov 11th, 2022 (edited)
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.86 KB | None | 0 0
  1. public class numberOne {
  2.     public static void main(String[] args) {
  3.         /*
  4.         *#1. Write java program using FOR loops for each of the following:
  5.         * (a) Print the sum and average of all the EVEN numbers from 20 to 150.
  6.          */
  7.  
  8.         int sum=0, average=0, num_count=0, product=1;
  9.  
  10.         for(int num=20; num<=150; num++) {
  11.  
  12.             if(num % 2 == 0) {
  13.  
  14.                 sum += num;
  15.  
  16.                 num_count++;
  17.  
  18.                 average = sum / num_count;
  19.                 //System.out.println(num+ " ");
  20.             }
  21.         }
  22.         System.out.println("Sum: " +sum);
  23.         System.out.println("Average: " +average);
  24.  
  25.         //(b) Print the product of all the numbers from 25 to 15.
  26.  
  27.         for(int no=15; no<=25; no++) {
  28.             product *= no;
  29.         }
  30.         System.out.println("Product: " +product);
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement