Udemy coupon 100 off 2017 ::Udemy coupon code free :: Get 100+ Free Udemy coupons Here

Find out Prime & Sum Of Prime is prime or not.

Program to Find Prime & Sum Of Prime is prime or not.

Here is my problem statement:
A prime number (or a prime) is a natural number greater than 1 that can only be divided by 1 and itself.
Ex-First few Prime number are-
2, 3, 5, 7, 11, 13, 17, 19,...

A Digit Prime is a prime number whose sum of digits is also prime.
For example the prime number 23 is a Digit prime because 2+3=5 and 5 is a prime number. 17 is not a Digit prime because 1+7 = 8, and 8 is not a prime number.

Write a program to find out the Digit Prime Number within a certain range at specified position.and print the Digit Prime Number of specified position within the range.

Input:
Input three positive integers : lower limit , upper limit , and pos is the position of the required digit prime number



Out Put is as following :












.

Source code: 

import java.io.*;
class PrimesAndSum
{
public static void main(String args[])  throws IOException
{
int low,up,pos;
int sum,temp,num;
int sumPrimeNo=0;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
   System.out.println("Enter the Lower Limit : ");
  low=Integer.parseInt(br.readLine());
System.out.println("Enter the Uper Limit : ");
  up=Integer.parseInt(br.readLine());
System.out.println("Enter the Position : ");
  pos=Integer.parseInt(br.readLine());

boolean found = false;
boolean isPrime = true;
boolean isSumPrime = true;
for(int i=low;i<=up;i++)
{
isPrime = true;
for(int j=2; j <= i/2; j++) 
{
if((i % j) == 0) 
{
isPrime =  false;
break;
}
}
if(isPrime) 
{
num=i;
sum=0;
while(num>0)
{
temp=num%10;
num=num/10;
sum=sum+temp;
}
isSumPrime = true;
for(int k=2; k <= sum/2; k++) 
{
if((sum % k) == 0) 
{
isSumPrime = false;
break;
}
}
if(isSumPrime) 
{
sumPrimeNo++;
if(sumPrimeNo==pos)
{
found=true;
System.out.println("Number  "+ i +" is Prime.Sum of this prime is also prime:  "+sum+"  Number is at position:  "+ sumPrimeNo);
}
//System.out.println("Number  "+ i +" is Prime.Sum of this prime is also prime:  "+sum+"  But it is at position:  "+ sumPrimeNo);
}
else
{
//System.out.println("Numer   " +i+" is prime.But  Sum of prime is not prime  "+sum);
}

}

}
if(!found)
{
System.out.println("No match");
}
}
}
  • Save the file with name PrimesAndSum.java 
  • Complie with: javac PrimesAndSum.java
  • Run with: java PrimesAndSum 


Moving Plates Game Using Java.

Games using Java

Moving Plates Game Using Java.

Out Put is as following :




You Can Download also : Download Moving Plates Game

Source Code:

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/*
<applet code=SimplePlatesGame.class width=800 height=600>
</applet>
*/
public class SimplePlatesGame extends Applet implements MouseListener ,MouseMotionListener
{
Color c1,c2;
Label l;
int score=0;
int temp;
int x=0,y=0;
int xclick,yclick;
int xmin=0,xmax=0,ymin=0,ymax=0;
int r1,g1,b1,r2,g2,b2;
public void init() 

l=new Label("Score is "+ score);
add(l);
r2=100;g2=b2=0;
c1 = new Color(128, 128, 255);
c2 = new Color(r2, g2,b2 );
newthread s=new newthread(this);
temp=0;
addMouseListener(this);
}

public void paint(Graphics g) 
{
setBackground(c1);
l.setBackground(c1);
c2 = new Color(r2, g2,b2 );
g.setColor(c2);
g.fillRect(x, y, 30, 20);
l.setText("Score is "+ score);
showStatus(xmin+"  "+xmax+"  "+ymin+"  "+ymax+"  "+xclick+" "+yclick);
}
public void mouseClicked(MouseEvent me) 
{
xclick= me.getX() ;
yclick= me.getY(); 
//showStatus(xclick+" "+yclick);
if((xclick>=xmin & xclick <=xmax) & (yclick>=ymin & yclick<=ymax))
{
score=score+5;
}
repaint();
}

public void mouseEntered(MouseEvent me) {}
public void mouseExited(MouseEvent me) {}
public void mousePressed(MouseEvent me) {}
public void mouseReleased(MouseEvent me) {}
public void mouseDragged(MouseEvent me) {}
public void mouseMoved(MouseEvent me) {}
}
class newthread implements Runnable
{
Thread t;
SimplePlatesGame obj;
newthread(SimplePlatesGame obj)
{
this.obj=obj;
t=new Thread(this ,"animate");
t.start();
}
public void run()
{
try
{
for(;;)
{
Thread.sleep(300);
if(obj.temp==0)
{
obj.x=obj.x+15;
if(obj.x>=690)
{
obj.temp=1;
obj.x=0;
obj.y=560;
}
obj.r2=obj.r2+5;
obj.g2=obj.g2+20;
if(obj.g2>=250)
{
obj.g2=0;
}
if(obj.r2>=250)
{
obj.r2=100;
}

}
if(obj.temp==1)
{
obj.x=obj.x+15;
obj.y=obj.y-15;
if(obj.x>=690 || obj.y<=0)
{
obj.temp=2;
obj.x=690;
obj.y=560;
}
obj.g2=obj.g2+5;
obj.b2=obj.b2+20;
if(obj.g2>=250)
{
obj.g2=0;
}
if(obj.b2>=250)
{
obj.b2=60;
}


}
if(obj.temp==2)
{
obj.x=obj.x-15;
obj.y=obj.y-15;
if(obj.x<=0 || obj.y<=0)
{
obj.temp=3;
obj.x=690;
obj.y=00;
}
obj.b2=obj.b2+5;
obj.r2=obj.r2+20;
if(obj.b2>=250)
{
obj.b2=20;
}
if(obj.r2>=250)
{
obj.r2=50;
}

}
if(obj.temp==3)
{
obj.x=obj.x-15;
obj.y=obj.y+15;
if(obj.x<=0 || obj.y>=560)
{
obj.temp=0;
obj.x=00;
obj.y=00;
}
obj.b2=obj.b2+5;
obj.r2=obj.r2+20;
obj.g2=obj.g2+10;
if(obj.b2>=250)
{
obj.b2=20;
}
if(obj.r2>=250)
{
obj.r2=50;
}
if(obj.g2>=250)
{
obj.g2=0;
}
}
obj.xmin=obj.x;
obj.xmax=obj.x+30;
obj.ymin=obj.y;
obj.ymax=obj.y+20;
obj.repaint();
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}




  • Save the file with name SimplePlatesGame.java 
  • Compile with: javac SimplePlatesGame.java
  • Run with:appletviewer SimplePlatesGame.java