Thursday 26 March 2020

FINDING SMALLER AMONG ALL USING JAVA LANGUAGE

Q-33-  If the ages of Rahul, Ayush and Ajay are input through the keyboard, write a java program to determine the elder among them.

INPUT :
import java.util.Scanner;
public class NCM {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int Rahul;
int Ayush;
int Ajay;
System.out.println("Enter The Ages Of Rahul ,Ayush And Ajay : ");
Rahul=sc.nextInt();
Ayush=sc.nextInt();
Ajay=sc.nextInt();
if(Rahul>Ayush && Rahul>Ajay)
{
System.out.println("Rahul Is Elder Among Them.");
}
else if(Ayush>Ajay && Ayush>Rahul)
{
System.out.println("Ayush Is Elder Among Them.");
}
else if(Ajay>Rahul && Ajay>Ayush)
{
System.out.println("Ajay Is Elder Among Them.");
}
}
}



OUTPUT :
Enter The Ages Of Rahul ,Ayush And Ajay :
16  15  17

Ajay Is Elder Among Them.

FINDING COORDINATE USING JAVA LANGUAGE

Q-32- Write a java program that takes the x – y coordinates of a point in the Cartesian plane and prints a message telling either an axis on which the point lies or the quadrant in which it is found.

INPUT :
import java.util.Scanner;
public class NCM {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner (System.in);
double x, y;
System.out.println("Enter The Value Of x and y :");
x=sc.nextDouble();
y=sc.nextDouble();
if(x==0 && y==0)
{
System.out.println("("+x+", "+y+") is on the Origin.");
}
else if(x==0 && y>0)
{
System.out.println("("+x+" , "+y+") is on the Y-axis.");
}
else if(x>0 && y==0)
{
System.out.println("("+x+" , "+y+") is on the X-axis.");
}
else if(x==0 && y<0)
{
System.out.println("("+x+" , "+y+") is on the Y-axis.");
}
else if(x<0 && y==0)
{
System.out.println("("+x+" , "+y+") is on the X-axis.");
}
else if(x>0 && y>0)
{
System.out.println("("+x+", "+y+") is on the First Quadrant.");
}
else if(x<0 && y>0)
{
System.out.println("("+x+" , "+y+") is on the Second Quadrant.");
}
else if(x<0 && y<0)
{
System.out.println("("+x+" , "+y+") is on the Third Quadrant.");
}
else if(x>0 && y<0)
{
System.out.println("("+x+" , "+y+") is on the Fourth Quadrant.");
}

}


}

OUTPUT :
Enter The Value Of x and y :
-1 -2.5

(-1.0 , -2.5) is on the Third Quadrant.

Enter The Value Of x and y :
0
4.8
(0.0 , 4.8) is on the Y-axis.


SOLVING LINEAR EQUATION USING CRAMER'S RULE BY JAVA LANGUAGE

Q-31- You can use Cramer’s rule to solve the following  2 X 2 system of linear equation:

𝑎𝑥 + 𝑏𝑦 = 𝑒     
𝑐𝑥 + 𝑑𝑦 = 𝑓
                           x=(𝑒𝑑 − 𝑏𝑓) /(𝑎𝑑 − 𝑏𝑐)                  𝑦 =(𝑎𝑓 − 𝑒𝑐)/ (𝑎𝑑 − 𝑏𝑐)

Write a java program that prompts the user to enter a, b, c, d, e, and f and displays the result. If (ad - bc) is 0, report that “The equation has no solution.”

INPUT :
import java.util.Scanner;
public class NCM {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
double a ,b ,c ,d ,e ,f ;
double x , y ,z;
System.out.println("Enter The Value Of a ,b ,c ,d ,e ,f :");
a=sc.nextDouble();
b=sc.nextDouble();
c=sc.nextDouble();
d=sc.nextDouble();
e=sc.nextDouble();
f=sc.nextDouble();
z=a*d-b*c;
x=(e*d-b*f)/z;
y=(a*f-e*c)/z;
if(z==0)
{
System.out.println("The Equation Has No Solution.");
}
else
{
System.out.println("x is "+x+" and y is "+y);
}
}


}

OUTPUT :
Enter The Value Of a ,b ,c ,d ,e ,f :
9.0 4.0 3.0 -5.0 -6.0 -21.0

x is -2.0 and y is 3.0

Enter The Value Of a ,b ,c ,d ,e ,f :
 1.0 2.0 2.0 4.0 4.0 5.0 
The Equation Has No Solution.


FINDING ROOTS OF A QUADRATIC EQUATION USING JAVA LANGUAGE

Q-30- The two roots of a quadratic equation 𝑎𝑥2 + 𝑏𝑥 + 𝑐 = 0  can be obtained using the following formula:

𝑟1 = −𝑏+√𝑏2−4𝑎𝑐 2𝑎         and           𝑟2 = −𝑏−√𝑏2−4𝑎𝑐 2𝑎
 b2 - 4ac is called the discriminant of the quadratic equation. If it is positive, the equation has two real roots. If it is zero, the equation has one root. If it is negative, the equation has no real roots.
Write a java program that prompts the user to enter values for a, b, and c and displays the result based on the discriminant. If the discriminant is positive, display two roots. If the discriminant is 0, display one root. Otherwise, display “The equation has no real roots” Note that you can use Math.pow(x, 0.5) to compute √𝑥

INPUT :
import java.util.Scanner;
public class NCM {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
double a ,b ,c;
double d ,r1 ,r2;
System.out.println("Enter The Value Of a ,b ,c :");
a=sc.nextDouble();
b=sc.nextDouble();
c=sc.nextDouble();
d = b*b-4*a*c;
double D=Math.pow(d, 0.5);
r1=(-b+D)/2*a;
r2=(-b-D)/2*a;
if(d==0)
{
System.out.println("The Equation Has One Root "+r1);
}
else if(d>0)
{
System.out.println("The Equation Has Two Real Roots "+r1+" and "+r2);
}
else if(d<0)
{
System.out.println("The Equation Has No Real Roots.");
}


}


}

OUTPUT :
Enter The Value Of a ,b ,c :
1 3 1

The Equation Has Two Real Roots -0.3819660112501051 and -2.618033988749895

Enter The Value Of a ,b ,c :
1 2 1
The Equation Has One Root -1.0

Enter The Value Of a ,b ,c :
1 2 3
The Equation Has No Real Roots.


FINDING LETTERS, NUMBERS AND SPECIAL CHARACTERS USING JAVA LANGUAGE

Q-29- Any character is entered through the keyboard, write a java program to determine whether the character entered is a capital letter, a small case letter, a digit or a special symbol. The following table shows the range of ASCII values for various characters.  Characters ASCII Values 
A – Z 65 – 90 
a – z 97 – 122
0 – 9 48 – 57 
special symbols 0 - 47, 58 - 64, 91 - 96, 123 – 127

INPUT :
import java.util.Scanner;
public class NCM {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
char ch;
System.out.println("Enter The Character :");
ch=sc.next().charAt(0);
if(ch>=65 && ch<=90)
{
System.out.println("This Character Is A Capital Letter. ");
}
else if(ch>=97 && ch<=122)
{
System.out.println("This Character Is A Small Letter. ");
}
else if(ch>=48 && ch<=57)
{
System.out.println("This Character Is A Digit.");
}
else if((ch>=0 && ch<=47) || (ch>=58 && ch<=64)
|| (ch>=91 && ch<=96) || (ch>=123 && ch<=127))
{
System.out.println("This Character Is A Special Symbol.");
}
else
{
System.out.println("You Have Entered Something Different.");
}

}


}

OUTPUT :
Enter The Character :
A

This Character Is A Capital Letter.

Enter The Character :
a
This Character Is A Small Letter.

Enter The Character :
5
This Character Is A Digit.

Enter The Character :
*
This Character Is A Special Symbol.


EVEN OR ODD USING JAVA LANGUAGE

Q-28- Input an integer through the keyboard. Write a java program to find out whether it is an odd number or even number.

INPUT :
import java.util.Scanner;
public class NCM {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int num;
System.out.println("Enter The Number :");
num=sc.nextInt();
if(num%2==0)
{
System.out.println("It Is An Even Number.");
}
else
{
System.out.println("It Is An Odd Number.");
}
}


}

OUTPUT :
Enter The Number :
4

It Is An Even Number.

Enter The Number :
5
It Is An Odd Number.

STUDENT MARKING PROCESS USING JAVA LANGUAGE

Q-27-Write a java program to input the mark of a student and check if the student mark is greater than or equal to 40, then it generates the following message.  “Congratulation! You have passed the exam.”  Otherwise the output message is  “Sorry! You have failed the exam.”

INPUT :
import java.util.Scanner;
public class NCM {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int mark;
System.out.println("Enter The Mark You Have Secured :");
mark=sc.nextInt();
if(mark>=40 )
{
 if(mark<=100)
 {
  System.out.println("Congratulation ! You Have Passed The Exam.");
 }
 else
 {
  System.out.println("Oops! You Have Entered Wrong Input....");
 }
}
else
{
System.out.println("Sorry! You Have Failed The Exam.");
}
}


}

OUTPUT :
Enter The Mark You Have Secured :
39

Sorry! You Have Failed The Exam.

Enter The Mark You Have Secured :
73
Congratulation ! You Have Passed The Exam.