Skip to main content

Posts

Showing posts with the label competitive exam

Determine whether a given number can be expressed as sum of two prime numbers or not

/* Write a program to determine whether a given number can be expressed as sum of two prime numbers or not. For example 34 can be expressed as sum of two prime numbers but 23 cannot be. */ import java.util.*; import java.io.*; public class SumOfTwoPrime { public static void Primer(int num) { ArrayList<Integer> ar = new ArrayList<Integer>(); boolean flag=false; int prevPrime = 1; for(int i=1;i<=num;i++) { for(int j=2;j<i;j++) { if(i%j==0) { flag=false; break; } else flag=true; } if(flag==true) { ar.add(i); } } for(int i=0;i<ar.size();i++) { for(int j=0;j<ar.size();j++) { if( (ar.get(i))+(ar.get(j)) == num ) System.out.println(num+" is sum of two primes "+ar.get(i)+" and "+ar.get(j)); } } }   public static void main(String args[]) {   Scanner sc = new Scanner(System.in);   int n = sc.nextInt();   Primer(n);   } }...

Given a string, reverse only vowels in it; leaving rest of the string as it is

/* Given a string, reverse only vowels in it; leaving rest of the string as it is. Input : abcdef Output : ebcdaf */ import java.io.*; import java.util.*; public class VowelReverse { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = sc.next(); String vowels = ""; String ans = ""; int arr[] = new int[str.length()]; for(int i=0;i<str.length();i++) { if(str.charAt(i)=='a' || str.charAt(i)=='e' || str.charAt(i)=='i' || str.charAt(i)=='o' || str.charAt(i)=='u') { vowels+=str.charAt(i); arr[i]=1; } } String revVowels = new StringBuffer(vowels).reverse().toString(); int j=0; for(int i=0;i<str.length();i++) { if(arr[i]!=1) { ans+=str.charAt(i); } else { ans+=revVowels.charAt(j); j++; } } System.out.println(ans); } }