veronikaaa86

03. Combinations

Oct 15th, 2022
942
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.74 KB | None | 0 0
  1. package nestedLoops;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class P03Combinations {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         int n = Integer.parseInt(scanner.nextLine()); // 5
  10.  
  11.         int countValidComb = 0;
  12.         //int allComp = 0;
  13.         for (int i = 0; i <= n; i++) {
  14.             for (int j = 0; j <= n; j++) {
  15.                 for (int k = 0; k <= n; k++) {
  16.                     //allComp++;
  17.                     int sum = i + j + k;
  18.                     if (sum == n) {
  19.                         countValidComb++;
  20.                     }
  21.                 }
  22.             }
  23.         }
  24.         //System.out.println(allComp);
  25.         System.out.println(countValidComb);
  26.     }
  27. }
  28.  
Advertisement
Add Comment
Please, Sign In to add comment