Advertisement
Shahar_Goldenberg

p124-125, p132-133

Nov 17th, 2022 (edited)
657
0
Never
2
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.61 KB | None | 0 0
  1. import java.util.*;
  2. public class homework
  3. {
  4.     public static Scanner reader = new Scanner(System.in);
  5.     public static void Q21() {
  6.         int n, c = 0;
  7.         for (int i = 1; i < 9; ++i) {
  8.             System.out.print("Enter number: ");
  9.             n = reader.nextInt();
  10.             if (n % 2 == 0) c++;
  11.         }
  12.         System.out.println(c);
  13.     }
  14.     public static void Q22() {
  15.         int c = 0;
  16.         for (int i = 1; i < 301; ++i) {
  17.             if (Math.sqrt(i) == (int)(Math.sqrt(i))) c++;
  18.         }
  19.         System.out.println(c);
  20.     }
  21.     public static void Q25() {
  22.         System.out.print("Enter the number of people that atend the test: ");
  23.         int n = reader.nextInt();
  24.         int mistake, c = 0;
  25.         for (int i = 0; i < n; ++i){
  26.             System.out.print("Number of mistake in signs: ");
  27.             mistake = reader.nextInt();
  28.             if (mistake == 0){
  29.                 System.out.print("Number of mistake over all: ");
  30.                 mistake = reader.nextInt();
  31.                 if (mistake < 4) c++;
  32.             }
  33.         }
  34.         System.out.println("Number of people that pass: " + c);
  35.         System.out.println("Pass rate: " + c*100.0/n);
  36.     }
  37.     public static void Q27() {
  38.         System.out.print("Enter number: ");
  39.         int n = reader.nextInt();
  40.         for (int i = 2; i < n; ++i) {
  41.             if (n % i == 0) return;
  42.         }
  43.         System.out.println(n + " is a prime number");
  44.     }
  45.     public static void Q33() {
  46.         int n, c = 0;
  47.         for (int i = 0; i < 30; ++i) {
  48.             System.out.print("Enter number: ");
  49.             n = reader.nextInt();
  50.             if (n > 9 && n < 100) c += n;
  51.         }
  52.         System.out.println("The sum of the even numbers: " + c);
  53.     }
  54.     public static void Q34() {
  55.         System.out.print("Enter number: ");
  56.         int n = reader.nextInt();
  57.         int assembly = 1;
  58.         for (int i = 1; i < n; ++i) {
  59.             assembly *= i;
  60.         }
  61.         System.out.println("The assembly for" + n + "is: " + assembly);
  62.     }
  63.     public static void Q36() {
  64.         System.out.println("-------------Q36-------------");
  65.         System.out.print("Enter number: ");
  66.         int n1 = reader.nextInt();
  67.         System.out.print("Enter number: ");
  68.         int n2 = reader.nextInt();
  69.         int max = Math.max(n1,n2), min = Math.min(n1, n2);
  70.         int sum = 0, mul = 1;
  71.         for (int i = min; i < max; ++i) {
  72.             sum += i; mul *= i;
  73.         }
  74.         System.out.println("The sum is: " + sum + ", and the product is: " + mul);
  75.     }
  76.     public static void Q38() {
  77.         System.out.println("-------------Q38-------------");
  78.         System.out.print("Enter base of exponent: ");
  79.         int base = reader.nextInt();
  80.         System.out.print("Enter exponent: ");
  81.         int exp = reader.nextInt();
  82.         for (int i = 1; i < exp; ++i) {
  83.             base *= base;
  84.         }
  85.         System.out.println("The exponent is: " + base);
  86.     }
  87.     public static void Q39() {
  88.         System.out.println("-------------Q39-------------");
  89.         System.out.print("Enter number: ");
  90.         int n = reader.nextInt();
  91.         int c = 0;
  92.         for (int i = 1; i < n; ++i) {
  93.             if (n % i == 0) {
  94.                 c += i;
  95.             }
  96.         }
  97.         if (c == n) {
  98.             System.out.println(n + " is perfect number");
  99.             for (int i = 1; i < n; ++i) {
  100.                 if (n % i == 0) { System.out.print(i + " + "); }
  101.             }
  102.         }
  103.     }
  104.         public static void main(String[] args)
  105.     {
  106.         Q21();Q22();Q25();Q27();Q33();Q34();Q36();Q38();Q39();
  107.     }
  108. }
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement