program solved
program solved
//arithmetic operator
static void arith(int a,int b,int c) {
if(a<0 || b<0 ||c<0) {
throw new ArithmeticException("enter again:strictly no negative number");
}
switch(c) {
case 1:
System.out.println(a+b);
break;
case 2:
System.out.println(a-b);
break;
case 3:
System.out.println(a*b);
break;
case 4:
try {
System.out.println(a/b);
}
catch(Exception e) {
System.out.println(e+"enter valid denominator");
}
break;
}
}
// 2)greatest of two number
static void two(int a,int b) {
if(a<0 || b<0) {
throw new ArithmeticException("enter again:strictly no negative number");
}
System.out.println((a>b)? a:b);
}
//3)greatest of three
static void three(int a,int b,int c) {
if(a<0 || b<0 || c<0) {
throw new ArithmeticException("enter again:strictly no negative number");
}
System.out.println((a>b && a>c)? a:(b>c)? b:c);
}
//4)positive or negative
static void pos(int a) {
System.out.println((a<0)? "negative":"positive");
}
//5)even or odd
static void eo(int a) {
System.out.println((a%2==0) ? "even":"odd");
}
//7)factorial number
static int fact(int a) {
// if(a<=0) {
// throw new ArithmeticException("to find factorial :number should be positive");
//
// }
if(a>0) {
return a*fact(a-1);}
return 1;
}
//8)fibonacci series
static void fib(int a) {
int one=0;
int two=1;
int three;
System.out.print(one+" "+two);
for(int i=3;i<=a;i++) {
three=one+two;
one=two;
two=three;
System.out.print(three+" ");
}
System.out.println();
}
static void pal(String a) {
int size=a.length();
boolean val=true;
for(int i=0;i<a.length()/2;i++) {
if(a.charAt(i)!=(a.charAt(size-1-i))) {
val=false;
}
}
if(val) {
System.out.println("palindrome");
}
else {
System.out.println("not a palindrome");
}
}
//10)armstrong
static void armstrong(int a) {
int n=a;
int val=0;
while(n>0) {
int rem=n%10;
val+=rem*rem*rem;
n/=10;
}
if(val==a) {
System.out.println("armstrong number");
}
else {
System.out.println("not a armstrong number");
}
}
//11)prime
static void prime(int a) {
int count=0;
for(int i=2;i<a/2;i++) {
if(a%i==0) {
count++;
}
}
System.out.println((count>1)?"not a prime ":"prime");
}
//12)swap two numbers
static void swap(int a,int b) {
System.out.println("12)before swap: a:"+a+" ,"+"b:"+b);
a=a+b;
b=a-b;
a-=b;
System.out.println("after swap: a:"+a+" ,"+"b:"+b);
}
public static void main(String[] args) {
//1)arithmetic
try {
arith(-1,5,1);
}
catch(ArithmeticException e) {
System.out.println(e);
}
System.out.print("1)the output is ");
arith(5,8,3);
//2) greatest of two
try {
two(-1,5);
}
catch(ArithmeticException e) {
System.out.println(e);
}
System.out.print("2)the output is ");
two(5,8);
//3)greatest of three
try {
three(-1,5,9);
}
catch(ArithmeticException e) {
System.out.println(e);
}
System.out.print("3)the output is ");
three(5,17,10);
//4)positive
System.out.print("4)the output is ");
pos(-89);
//5)even or odd
System.out.print("4)the output is ");
eo(258);
//6)arithmetic
try {
arith(-9,5,1);
}
catch(ArithmeticException e) {
System.out.println(e);
}
System.out.print("6)the output is ");
arith(5,832,3);
//7)factorial
System.out.print("7)the output is ");
System.out.println(fact(5));
//8)fibonacci
System.out.print("8)the output is ");
fib(20);
//9)palindrome
System.out.print("9)the output is ");
pal("maddam");
//10)armstrong
System.out.print("10)the number is a ");
armstrong(371);
//11)prime
System.out.print("11)the number is ");
prime(89);
//12)swap two numbers
swap(10,20);
}
}
The output:
java.lang.ArithmeticException: enter again:strictly no negative number
1)the output is 40
java.lang.ArithmeticException: enter again:strictly no negative number
2)the output is 8
java.lang.ArithmeticException: enter again:strictly no negative number
3)the output is 17
4)the output is negative
4)the output is even
java.lang.ArithmeticException: enter again:strictly no negative number
6)the output is 4160
7)the output is 120
8)the output is 0 11 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181
9)the output is palindrome
10)the number is a armstrong number
11)the number is prime
12)before swap: a:10 ,b:20
after swap: a:20 ,b:10