Java Lab 2
Java Lab 2
Loops
}
}
1. Program to find the biggest of the three numbers by taking the input from keyboard
public class Biggest {
public static void main(String a[])throws IOException {
DataInputStream dts=new DataInputStream(System.in);
System.out.println("Enter the first number");
int x=Integer.parseInt(dts. readLine());
System.out.println("Enter the second number");
int y=Integer.parseInt(dts. readLine());
System.out.println("Enter the third number");
int z=Integer.parseInt(dts. readLine());
if (x>y && x>z)
System.out.println("The biggest number is "+x);
else if (y>z && y>x)
System.out.println("The biggest number is "+y);
else
System.out.println("The biggest number is "+z);
}
2. Program to find the square root of a number by taking the input from keyboard
class Sqrt {
public static void main(String a[])throws IOException {
DataInputStream dts=new DataInputStream(System.in);
System.out.println("enter number to find squaroot");
int m=Integer.parseInt(dts. readLine());
System.out.println("square root of "+ m + " = "+Math.sqrt(m));
}
}
3. Program to find the sum of digits of a given number by taking the input from keyboard
class DigitAdd1{
public static void main(String args[])throws IOException{
int temp,sum=0;
DataInputStream dts=new DataInputStream(System.in);
System.out.println("enter number to get the sum of digits");
int num=Integer.parseInt(dts. readLine());
System.out.println("The number is:" +num);
while ( num > 0 ){
temp = num % 10;
num = num / 10;
sum = sum + temp;
}
System.out.println("The sum of the digits of the above number is:" +sum);
}
}
Best of luck