0% found this document useful (0 votes)
20 views5 pages

Java Arithmetic and Logic Operations

Uploaded by

vkaruppasamy2004
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views5 pages

Java Arithmetic and Logic Operations

Uploaded by

vkaruppasamy2004
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

public class level1 {

//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:
[Link](a+b);
break;
case 2:
[Link](a-b);
break;
case 3:
[Link](a*b);
break;
case 4:
try {
[Link](a/b);
}
catch(Exception e) {
[Link](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");

}
[Link]((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");

}
[Link]((a>b && a>c)? a:(b>c)? b:c);
}
//4)positive or negative
static void pos(int a) {
[Link]((a<0)? "negative":"positive");
}
//5)even or odd
static void eo(int a) {
[Link]((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;
[Link](one+" "+two);
for(int i=3;i<=a;i++) {
three=one+two;
one=two;
two=three;
[Link](three+" ");
}
[Link]();
}
static void pal(String a) {
int size=[Link]();
boolean val=true;
for(int i=0;i<[Link]()/2;i++) {
if([Link](i)!=([Link](size-1-i))) {
val=false;

}
}
if(val) {
[Link]("palindrome");
}
else {
[Link]("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) {
[Link]("armstrong number");
}
else {
[Link]("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++;
}
}
[Link]((count>1)?"not a prime ":"prime");
}
//12)swap two numbers
static void swap(int a,int b) {
[Link]("12)before swap: a:"+a+" ,"+"b:"+b);
a=a+b;
b=a-b;
a-=b;
[Link]("after swap: a:"+a+" ,"+"b:"+b);
}
public static void main(String[] args) {
//1)arithmetic
try {
arith(-1,5,1);
}
catch(ArithmeticException e) {
[Link](e);
}
[Link]("1)the output is ");
arith(5,8,3);
//2) greatest of two
try {
two(-1,5);
}
catch(ArithmeticException e) {
[Link](e);
}
[Link]("2)the output is ");
two(5,8);
//3)greatest of three
try {
three(-1,5,9);
}
catch(ArithmeticException e) {
[Link](e);
}
[Link]("3)the output is ");
three(5,17,10);
//4)positive
[Link]("4)the output is ");
pos(-89);
//5)even or odd
[Link]("4)the output is ");
eo(258);
//6)arithmetic
try {
arith(-9,5,1);
}
catch(ArithmeticException e) {
[Link](e);
}
[Link]("6)the output is ");
arith(5,832,3);
//7)factorial
[Link]("7)the output is ");
[Link](fact(5));
//8)fibonacci
[Link]("8)the output is ");
fib(20);
//9)palindrome
[Link]("9)the output is ");
pal("maddam");
//10)armstrong
[Link]("10)the number is a ");
armstrong(371);
//11)prime
[Link]("11)the number is ");
prime(89);
//12)swap two numbers
swap(10,20);
}
}
The output:
[Link]: enter again:strictly no negative number
1)the output is 40
[Link]: enter again:strictly no negative number
2)the output is 8
[Link]: enter again:strictly no negative number
3)the output is 17
4)the output is negative
4)the output is even
[Link]: 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

Common questions

Powered by AI

The `fact` method calculates the factorial of a positive integer recursively. For any integer `a` greater than 0, it multiplies `a` by the factorial of `a-1`. The recursion base case occurs when `a` is 1, where the method returns 1, allowing the recursion to unwind .

The main method in the program handles invalid input scenarios by wrapping calls to methods in try-catch blocks. Each method call that can potentially throw an ArithmeticException (such as operations involving negative numbers) is enclosed in a try block. If an exception is thrown, it is caught in the catch block, where the specified error message is then displayed, maintaining program flow .

The `palindrome` function checks if a string reads the same forwards and backwards by comparing characters sequentially from the start and end towards the center. If all character pairs match, it confirms the string as a palindrome. Otherwise, it determines it is not. This check is case-sensitive and assumes no structural or whitespace alterations in comparison .

The `arith` function manages negative inputs by throwing an ArithmeticException if any of the input integers `a`, `b`, or `c` is negative. This exception message instructs the user to enter non-negative numbers only. This error handling ensures that operations such as addition, subtraction, multiplication, and division are performed only with valid non-negative numbers .

The `fib` function iteratively computes the Fibonacci series starting with two initial terms `0` and `1`. It then calculates the subsequent terms by summing the last two recorded values. This computation runs in a loop, producing and printing each term until reaching the specified count, generating the series progressively .

The `three` function uses conditional logic to evaluate which of the three numbers `a`, `b`, or `c` is the greatest. First, it checks if `a` is greater than both `b` and `c`. If true, `a` is the greatest. If not, it checks if `b` is greater than `c`; if so, `b` is the greatest. Otherwise, it concludes that `c` is the greatest .

The `prime` function verifies a number's primality by checking if it is divisible by any number from 2 up to half of the number (effectively excluding even multiples). If it finds any divisors, the count is incremented, indicating the number is not prime. If no divisors are found, the function concludes the number is a prime .

The number 371 is determined to be an Armstrong number because the sum of the cubes of its digits equals the number itself. The `armstrong` method computes this by extracting each digit of `371`, cubing it, summing the results (3³ + 7³ + 1³ = 371), and comparing it to the original number. Since they are equal, 371 qualifies as an Armstrong number .

Each function handling negative inputs—`two`, `three`, and arithmetic—starts with a check for negative values among their parameters. When a negative input is detected, an ArithmeticException is thrown, halting the function's execution and propagating control back to the caller, which should handle it. The consistent use of exception handling across these functions showcases a robust strategy to prevent invalid computations and enforces the invariant of using only non-negative numbers, ensuring logical integrity in program operations .

The `swap` function exchanges the values of two numbers `a` and `b` without using a temporary variable. It achieves this by first adding both numbers (`a = a + b`), then assigning the new value of `a` minus `b` to `b` (`b = a - b`), and finally assigning the new value of `a` minus `b` back to `a` (`a = a - b`). This series of operations effectively swaps the values .

You might also like