Monday 18 May 2020

TO CHECK WHETHER A POINT IS WITHIN THE CIRCLE OR NOT USING JAVA LANGUAGE

Q-40-  Write a java program that prompts the user to enter a point (x, y) and checks whether the point is within the circle centered at (0, 0) with radius 10. For example, (4, 5) is inside the circle and (9, 9) is outside the circle, 

 (Hint: A point is in the circle if its distance to (0, 0) is less than or equal to 10. The formula for computing the distance is √(𝑥2 − 𝑥1)2 + (𝑦2 − 𝑦1)2 .Test your program to cover all cases.) 

INPUT :

import java.util.Scanner;

public class NCM {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
double x1=0;
double y1=0;
double radius=10;
double x2 ,y2;
System.out.println("Enter The Point :");
x2=sc.nextDouble();
y2=sc.nextDouble();
double r=(x2-x1)*(x2-x1)+(y2-y1)*(y2-y1);
double distance=Math.pow(r, 0.5);
if(distance<=radius)
{
System.out.println("Point ("+x2+", "+y2+") Is In The Circle .");
}
else
{
System.out.println("Point ("+x2+", "+y2+") Is Not In The Circle .");
}

}

}

OUTPUT :

Enter The Point :
4 5
Point (4.0, 5.0) Is In The Circle .

Enter The Point :
9 9
Point (9.0, 9.0) Is Not In The Circle .



LET'S PLAY SCISSOR-ROCK-PAPER GAME USING JAVA LANGUAGE

Q-39-  Write a java program that plays the popular scissor-rock-paper game. (A scissor can cut a paper, a rock can knock a scissor, and a paper can wrap a rock.) The program randomly generates a number 0, 1, or 2 representing scissor, rock, and paper. The program prompts the user to enter a number 0, 1, or 2 and displays a message indicating whether the user or the computer wins, loses, or draws.

INPUT :

import java.util.Scanner;

public class NCM {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int num;
int random=(int)( Math.random()*(3));
System.out.println("Here scissor=0 ,rock=1 ,paper=2");
System.out.println("Enter Your Choice :");
num=sc.nextInt();
if(num==0 && random==0)
{
System.out.println("Computer Is Scissor And You Are Scissor.");
System.out.println("This Match Is Draw .");
}
else if(num==0 && random==1)
{
System.out.println("Computer Is Rock And You Are Scissor.");
System.out.println("Computer Won The Match.");
}
else if(num==0 && random==2)
{
System.out.println("Computer Is Paper And You Are Scissor.");
System.out.println("You Won The Match .");
}

else if(num==1 && random==0)
{
System.out.println("Computer Is Scissor And You Are Rock.");
System.out.println("You Won The Match .");
}
else if(num==1 && random==1)
{
System.out.println("Computer Is Rock And You Are Rock.");
System.out.println("This Match Is Draw .");
}
else if(num==1 && random==2)
{
System.out.println("Computer Is Paper And You Are Rock.");
System.out.println("Computer Won The Match .");
}
else if(num==2 && random==0)
{
System.out.println("Computer Is Scissor And You Are Paper.");
System.out.println("Computer Won The Match .");
}
else if(num==2 && random==1)
{
System.out.println("Computer Is Rock And You Are Paper.");
System.out.println("You Won The Match.");
}
else if(num==2 && random==2)
{
System.out.println("Computer Is Paper And You Are Paper.");
System.out.println("This Match Is Draw .");
}
else
{
System.out.println("You Have Entered Wrong Value .");
}

}

}

OUTPUT :

Here scissor=0 ,rock=1 ,paper=2
Enter Your Choice :
0
Computer Is Scissor And You Are Scissor.
This Match Is Draw .

Here scissor=0 ,rock=1 ,paper=2
Enter Your Choice :
1
Computer Is Paper And You Are Rock.
Computer Won The Match .

Here scissor=0 ,rock=1 ,paper=2
Enter Your Choice :
2
Computer Is Rock And You Are Paper.
You Won The Match.



FINDING DAYS IN A MONTH WITH RESPECT TO YEAR USING JAVA LANGUAGE

Q-38- Write a java program that prompts the user to enter the month and year and displays the number of days in the month. For example, if the user entered month 2 and year 2012, the program should display that February 2012 had 29 days. If the user entered month 3 and year 2015, the program should display that March 2015 had 31 days. 

INPUT :

import java.util.Scanner;

public class NCM {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int month, year;
System.out.println("Enter The Month And Year :");
month=sc.nextInt();
year=sc.nextInt();
if(month==1)
{
System.out.println("January "+year+" Had 31 Days.");
}
else if(month==2)
{
if((year%4==0) && (year%100!=0) || (year%400==0))
{
System.out.println("February "+year+" Had 29 Days.");
}
else
{
System.out.println("February "+year+" Had 28 Days.");
}
}
else if(month==3)
{
System.out.println("March "+year+" Had 31 Days.");
}
else if(month==4)
{
System.out.println("April "+year+" Had 30 Days.");
}
else if(month==5)
{
System.out.println("May "+year+" Had 31 Days.");
}
else if(month==6)
{
System.out.println("June "+year+" Had 30 Days.");
}
else if(month==7)
{
System.out.println("July "+year+" Had 31 Days.");
}
else if(month==8)
{
System.out.println("August "+year+" Had 31 Days.");
}
else if(month==9)
{
System.out.println("September "+year+" Had 30 Days.");
}
else if(month==10)
{
System.out.println("October "+year+" Had 31 Days.");
}
else if(month==11)
{
System.out.println("November "+year+" Had 30 Days.");
}
else if(month==12)
{
System.out.println("December "+year+" Had 31 Days.");
}
else
{
System.out.println("You Have Entered Wrong Input :");
}

}

}

OUTPUT :

Enter The Month And Year :
2
2012
February 2012 Had 29 Days.

Enter The Month And Year :
3
2015
March 2015 Had 31 Days.