You are on page 1of 4

set 21

Question 1 : 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.

program::

public class Main


{

public static boolean prime(int n){

for(int i=2;i<=n/2;i++){

if(n%i==0){

return false;

}
}

return true;

public static void main(String[] args) {

int n=66;

int flag=0;

for(int k=2;k<=n/2;k++){

if(prime(k)==true){

if(prime(n-k)==true){

flag=1;

System.out.print(" "+k+" "+(n-k)+" ="+n);

System.out.println();

}
}

}
if(flag==0)

System.out.println(" we can't make");


}
}

Question 2 : Take a 2 or 3 digit input number, reverse it and add it to the


original number until the obtained number is a palindrome or 5 iterations are
completed.

Input : n = 32
Output : 55
23 + 32 = 55 which is a palindrome.

Input : 39
Output : 363

program::

public class Main


{

public static int reverse(int k){

int temp=k,re=0,sum=0;

while(temp>0){

re=temp%10;

sum=sum*10+re;

temp/=10;

return sum;

public static void main(String[] args) {

int temp=0,re=0,sum=0,n=39,flag=0;

for(int i=1;i<=5;i++){

int reve=reverse(n);

n=n+reve;

int m=reverse(n);

if(n==m){

System.out.print(" no is"+n);
flag =1;

break;

if(flag==0){

System.out.println("we can't make");

}
}

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

Input : abcdef
Output : ebcdaf
program:

public class Main


{

public static boolean isvowel(char c){

if(c=='A'||c=='E'||c=='I'||c=='O'||c=='U'||c=='a'||c=='e'||c=='i'||c=='o'||
c=='u')

return true;

return false;

public static void main(String[] args) {

String s="hello world";//Output : hollo werld

char a[]=s.toCharArray();

int i=0;
int j=a.length-1;

while(i<j){

if(!isvowel(a[i])){

i++;
}

if(!isvowel(a[j])){
j--;
}

else
{

if(i<j){

char c=a[j];

a[j]=a[i];

a[i]=c;

i++;

j--;

for(char aa:a)

System.out.print(aa);

}
}

You might also like