Advertisement
Wan_ich1

jumlah

Apr 18th, 2021
1,214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.46 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. /**
  4.  *
  5.  * @author WAN
  6.  */
  7. public class Jumlah {
  8.  
  9.     /**
  10.      * @param args the command line arguments
  11.      */
  12.     public static void main(String[] args) {
  13.         Scanner input = new Scanner(System.in);
  14.         int nilai,cari,temp;
  15.         int[] index = new int[2];
  16.         boolean done=false;
  17.        
  18.         System.out.println("Masukkan jumlah array");
  19.         nilai = input.nextInt();
  20.         int[] array = new int[nilai];
  21.         System.out.println("Masukkan nilai");
  22.         for(int i=0;i<nilai;i++)
  23.         {
  24.             array[i]=input.nextInt();
  25.         }
  26.        
  27.         for(int i=0;i<nilai;i++)
  28.         {
  29.             System.out.print(array[i] + ", ");
  30.         }
  31.        
  32.         System.out.print("\nMasukkan nilai yang dicari = ");
  33.         cari=input.nextInt();
  34.        
  35.         for(int i=0;i<nilai;i++)
  36.         {
  37.             for(int j=1;j<nilai;j++)
  38.             {
  39.                 temp=array[i]+array[j];
  40.                 if(temp == cari)
  41.                 {
  42.                     index[0]=i;
  43.                     index[1]=j;
  44.                     done=true;
  45.                     break;
  46.                 }
  47.             }
  48.         }
  49.         if(done)
  50.         {
  51.             System.out.println("ditemukan pada index = "+ index[0]+" + "+index[1]);
  52.         }
  53.         else
  54.         {
  55.             System.out.println("no output");
  56.         }
  57.        
  58.        
  59.         // TODO code application logic here
  60.     }
  61.    
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement