+ 7

Java unsolving question

import java.util.Scanner; import java.util.*; import java.util.ArrayList; import java.util.Collections; public class Program { public static void main(String[] args) { Scanner s = new Scanner(System.in); ArrayList<Integer>list= new ArrayList<>(); ArrayList<Integer>list2= new ArrayList<>(); int num1= s.nextInt(); int num2= s.nextInt(); for(int n= num1; n<=num2;n++) { int count=0; for(int i=1;i<=n;i++) { if(n%i==0) { count++; } } if (count==2) { list.add(n); } } System.out.println("prime numbrs of list1"+":"+list); for(int i=0;i<=list.size()-1;i++) { for(int k=0;k<=list.size()-1;k++) { int a=list.get(i); int b=list.get(k); String t=Integer.toString(a); String s1=Integer.toString(b); String res=t+s1; int result=Integer.parseInt(res); int count2=0; for(int j=1;j<=result;j++) { if (result%j==0) { count2++; } if(count2==2) { list2.add(result); } } } System.out.println("prime numbers of list2"+":"+list2); int one=Collections.min(list2); int two=Collections.max(list2); System.out.println("small no. :"+ one); System.out.println("small no. :"+ two); for(int i=1; i<=list2.size()-2;i++) { int c= one+two; one=two; two=c; if(i==list2.size()-2) { System.out.println(c); } } }} Iam getting error in it. Could u help me If input: 30 70 Output should be : 2027041

20th Jun 2020, 9:30 AM
Haritha Vuppula
Haritha Vuppula - avatar
18 Réponses
+ 3
Haritha Vuppula Can you explain what you want to do here? I rectified your problem but did not get what you want to do. Can you please explain? Another thing before adding in list just check if the value already exist or not. https://code.sololearn.com/cEarGZK1sPNk/?ref=app
20th Jun 2020, 9:49 AM
A͢J
A͢J - avatar
+ 5
Haritha Vuppula I already told you there is one missing closing braces } before the condition if(count2 == 0) but you are not adding. ______________________ Now check your this logic for(int j=1;j<=result;j++) { if (result%j==0) { count2++; } } //missing if(count2==2) { list2.add(result); } } Do you think this is correct? Run the below code and see your code https://code.sololearn.com/cK1x9F509ly9/?ref=app
20th Jun 2020, 11:17 AM
A͢J
A͢J - avatar
+ 4
Haritha Vuppula To avoid this type of problem make a common method if there is repeated logic in your code. This is reason why we make method. Because method can be use as a reusable code. If you see my code I had just made a common method and used that.
20th Jun 2020, 11:41 AM
A͢J
A͢J - avatar
+ 3
Haritha Vuppula Ok but next time please add your attempts code with your problem so it would be easy. Let me check your question again.
20th Jun 2020, 10:00 AM
A͢J
A͢J - avatar
+ 3
Haritha Vuppula You did just a small mistake. Close the braces before this condition if(count2==2)
20th Jun 2020, 10:25 AM
A͢J
A͢J - avatar
+ 3
Haritha Vuppula Yes I got. Update your Code and see the result. Always remember for this type of problem if you get repeated logic then make a common method and use anywhere. See my code I made a common method to check prime number. If Number is prime then return true otherwise false. 2nd thing to make string of two numbers Just do like this: String res = num1 + "" + num2;
20th Jun 2020, 10:39 AM
A͢J
A͢J - avatar
+ 3
Haritha Vuppula Your program is correct. Just add one more } after line no 46 and before this condition if(count2==2) and remove one } before this line System.out.println("prime numbers of list2"+":"+list2);
20th Jun 2020, 10:57 AM
A͢J
A͢J - avatar
+ 2
AJ Anant thank you
20th Jun 2020, 10:43 AM
Haritha Vuppula
Haritha Vuppula - avatar
+ 2
AJ Anant thnq😇
20th Jun 2020, 11:32 AM
Haritha Vuppula
Haritha Vuppula - avatar
20th Jun 2020, 9:42 AM
Haritha Vuppula
Haritha Vuppula - avatar
+ 1
AJ Anant can u see my previous question post. this is solution to that question
20th Jun 2020, 9:54 AM
Haritha Vuppula
Haritha Vuppula - avatar
+ 1
AJ Anant https://code.sololearn.com/cPF2F7iMSoM4/?ref=app S but execution time out! it is cmng
20th Jun 2020, 10:32 AM
Haritha Vuppula
Haritha Vuppula - avatar
+ 1
AJ Anant did u get output!
20th Jun 2020, 10:34 AM
Haritha Vuppula
Haritha Vuppula - avatar
+ 1
Haritha Vuppula I said before that condition but you added somewhere else.
20th Jun 2020, 10:43 AM
A͢J
A͢J - avatar
+ 1
AJ Anant how to get unique combinations in list2??
20th Jun 2020, 10:47 AM
Haritha Vuppula
Haritha Vuppula - avatar
+ 1
Haritha Vuppula To get unique combination Just check if number is already added in the list like if(list2.contains(num) { //do nothing } else { list2.add(num) } Or if (!list2.contains(num)) list2.add(num)
20th Jun 2020, 10:54 AM
A͢J
A͢J - avatar
+ 1
AJ Anant i didnt get unique combinations of prime numbers in list2! i gave input: 1 3 prime numbrs of list1:[2, 3] prime numbers of list2:[22, 22, 22, 22, 22, 22, 22, 22, 22, 23, 32, 32, 33, 33, 33, 33, 33, 33, 33, 33] small no. :22 small no. :33 194821 instead i should get list2 as[ 23 ]
20th Jun 2020, 11:07 AM
Haritha Vuppula
Haritha Vuppula - avatar
0
java unsolving
22nd Jun 2020, 5:44 PM
Silvia Lucreca Muñóz Sierra
Silvia Lucreca Muñóz Sierra - avatar