java hemraj
java hemraj
sc.close();
}
}
OUTPUT:
switch (choice) {
case 1:
System.out.println("Enter Radius:");
Float r = sc.nextFloat();
System.out.println("Enter Height:");
Float h = sc.nextFloat();
double Volume = Math.PI * r * r * h;
System.out.printf("Volume of Cylinder: %f" ,Volume);
break;
case 2:
System.out.println("Enter Number for Finding Factorial : ");
int num = sc.nextInt();
long fact = 1;
for (int i = 1; i <= num; ++i) {
fact = fact * i;
}
System.out.printf("Factorial of %d = %d\n", num, fact);
break;
case 3:
System.out.println("Enter Number for Finding Armstrong Number : ");
int n = sc.nextInt();
int leng = 0;
int t1 = n;
while (t1 != 0) {
t1 = t1 / 10;
leng = leng + 1;
}
int t2 = n;
int arm = 0;
int rem;
while (t2 != 0) {
int mult = 1;
rem = t2 % 10;
for (int i = 1; i <= leng; i++) {
mult = mult * rem;
}
arm = arm + mult;
t2 = t2 / 10;
}
if (arm == n) {
System.out.println("The given number is armstrong..!");
} else {
System.out.println("The given number is not armstrong..!");
}
break;
case 4:
System.exit(0);
default:
break;
}
sc.close();
}
}
OUTPUT:
(4) Write a program to accept the array element and display in
reverseorder.
PROGRAM:
package ass1;
import java.util.Scanner;
public class Reversearray {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter size of Array :");
int n = sc.nextInt();
(2) Define a class MyNumber having one private int data member. Write a
default constructor to initialize it to 0 and another constructor to
initialize it to a value (Use this). Write methods isNegative, isPositive,
isZero, isOdd, isEven. Create an object in main. Use command line
arguments to pass a value to the object (Hint : convert string
argument to integer) and perform the above tests. Provide javadoc
comments for all constructors and methods and generate the html
help file.
PROGRAM:
Package as1setbb
public class MyNumber {
private int x;
public MyNumber(){
x=0;
}
public MyNumber(int x){
this.x=x;
}
public boolean isNegative(){
if(x<0) return
true; else
return false;
}
public boolean isPositive(){
if(x>0) return
true;
else return false;
}
public boolean isZero(){
if(x==0) return
true;
else return false;
}
public boolean isOdd()
{ if(x%2!=0) return true;
else return false;
}
public boolean isEven()
{ if(x%2==0) return true;
else return false;
}
public static void main(String [] args) throws ArrayIndexOutOfBoundsException
{
int x=Integer.parseInt(args[0]); MyNumber
m=new MyNumber(x);
if(m.isNegative())
System.out.println("Number is Negative");
if(m.isPositive())
System.out.println("Number is Positive"); if(m.isEven())
System.out.println("Number is Even");
if(m.isOdd())
System.out.println("Number is Odd"); if(m.isZero())
System.out.println("Number is Zero");
}
}
OUTPUT: