0% found this document useful (0 votes)
1 views9 pages

Java Assignment1

Uploaded by

Abhi Chavan
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)
1 views9 pages

Java Assignment1

Uploaded by

Abhi Chavan
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

Abhishek A Chavan

Assignment No.: 01

1. Implement the program of addition, subtraction, division and multiplication of


two [Link] the input from user.
Ans.
Source Code:
import [Link];

public class MathOperation


{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("Enter two numbers");
int a = [Link]();
int b = [Link]();
int c = a + b;
[Link]("Addition of two numbers is : " + c);
int d = a - b;
[Link]("Subtraction of two numbers is : " + d);
int e = a * b;
[Link]("Multiplication of two numbers is : " + e);
int f = a / b;
[Link]("Division of two numbers is : " + f);
}
}

Output:

2. Calculate the areas of different shapes using Switch Case.


Ans.
Source Code:
import [Link];
class FindAreaUsingSwitchStatement
{
public static void main(String[] args)

1
Abhishek A Chavan

{
Scanner sc = new Scanner([Link]);
[Link]("MENU:");
[Link]("[Link] of circle");
[Link]("[Link] of triangle");
[Link]("[Link] of rectangle");
[Link]("Please enter any of the above option: ");
int num = [Link]();
switch(num)
{
case 1: [Link]("Please enter radius of circle: ");
int radius = [Link]();
int areaCircle = (22 * radius * radius) / 7;
[Link]("Area of circle is: " + areaCircle);
break;
case 2: [Link]("Please enter base and height of triangle: ");
int base = [Link]();
int height = [Link]();
int areaTriangle = (base* height) / 2;
[Link]("Area of triangle is: " + areaTriangle);
break;
case 3: [Link]("Please enter length and breadth of rectangle: ");
int length = [Link]();
int breadth = [Link]();
int areaRectangle = length * breadth;
[Link]("Area of ractangle is: " + areaRectangle);
break;
default:[Link](0);
}
[Link]();
}
}

Output:

2
Abhishek A Chavan

3. What will be the output of following program?


public class PostIncrement
{
public static void main(String[] args)
{
int num1 = 1;
int num2 = 1;
num1++;
num2++;
[Link]("num1 = " + num1);
[Link]("num2 = " + num2);
}
}
Ans. Output

3
Abhishek A Chavan

4. What will be the output of following program


public class Demo
{
public static void main(String[] args)
{
int num1 = 1;
int num2 = 1;
--num1;
--num2;
[Link]("num1 = " + num1);
[Link]("num2 = " + num2);
}
}
Ans. Output

5. Implement a program on bitwise operators.


Ans.
Source Code:
class BitwiseOperator
{
public static void main(String[] args)
{

int a = 10;
int b = 5;
[Link]("a&b = " + (a & b));
[Link]("a|b = " + (a | b));
[Link]("a^b = " + (a ^ b));
[Link]("~a = " + ~a);
[Link]("a<<2 = " + (a << 2));

}
}
Output:

4
Abhishek A Chavan

6. Implement a program on logical operators.


Ans.
Source Code:
public class LogicalOperators {
public static void main(String[] args) {
int a = 10;
int b = 15;
int c = 20;
[Link]("Result of (a > b) && (c > b) is :" + ((a > b) && (c > b)));
[Link]("Result of (a > b) || (c > b) is :" + ((a > b) || (c > b)));
[Link]("Result of !(a > b) is :" + !(a > b));
}
}

Output:

7. Find out given number is divisible by 5 as well as 7 or not.


Ans.
Source Code:
public class DivisibleByFiveSeven
{
public static void main(String[] args)
{
int number = 55;
if(number % 5 == 0 && number % 7 == 0){
[Link]("Number is divisible by 5 and 7.");
}
else
{
[Link]("Number is not divisible by 5 and 7.");
}
}
}

Output:

5
Abhishek A Chavan

8. Display the elements from an array which are divisible by 5 or 7.


Ans.
Source Code:
public class DisplayElementsDivisibleBy5_7 {
public static void main(String[] args) {

int[] arr = { 5, 10, 15, 20, 25, 30, 35, 40, 45, 50 };

[Link]("Elements of array divisible by 5 or 7:");

for (int i = 0; i < [Link]; i++) {


if (arr[i] % 5 == 0 || arr[i] % 7 == 0) {
[Link](arr[i] + " ");
}
}
}
}

Output:

9. Display the elements from array which are not divisible by 3.


Ans.
Source Code:
public class NotDivisibleBy3
{
public static void main(String[] args)
{
int arr[] = {2, 4, 5, 6, 7, 8, 9, 10, 11};
[Link]("The elements in the array which are not divisible by 3 are: ");
for (int i = 0; i < [Link]; i++)
{
if (arr[i] % 3 != 0)
[Link](arr[i] + " ");
}
}
}

Output:

6
Abhishek A Chavan

10. Display the smallest element from an array.


Ans.
Source Code:
public class SmallestElement
{
public static void main(String[] args)
{
int[] numbers = {5, 11, 20, 3, 7};
int smallest = numbers[0];
for(int i=1; i< [Link]; i++)
{
if(numbers[i] < smallest)
{
smallest = numbers[i];
}
}
[Link]("Smallest element from the array is : " + smallest);
}
}

Output:

11. Display the greatest element from an array.


Ans.
Source Code:
public class GreatestElement {

public static void main(String[] args) {


int[] numbers = {5, 11, 20, 3, 7};

int greatest = numbers[0];

for(int i=1; i< [Link]; i++)


{
if(numbers[i] > greatest)
{
greatest = numbers[i];
}
}

[Link]("Greatest element from the array is : " + greatest);


}
}
Output:

7
Abhishek A Chavan

12. Display the smallest even number from an array.


Ans.
Source Code:
public class SmallestEvenNumber
{
public static void main(String[] args)
{
int [] arr = {4, 5, 7, 9, 6, 8};
int smallest = arr[0];
for (int i = 0; i < [Link]; i++)
{
if (arr[i] % 2 == 0 && arr[i] < smallest)
{
smallest = arr[i];
}
}
[Link]("The smallest even number is: " + smallest);
}
}
Output:

13. Display the greatest odd number from an array.


Ans.
Source Code:
public class GreatestOddNumber
{
public static void main(String[] args)
{
int[] array = {2, 3, 5, 6, 7, 8, 9, 11, 13};
int greatestOddNumber = 0;
for (int i = 0; i < [Link]; i++)
{
if (array[i] % 2 != 0 && array[i] > greatestOddNumber)
{
greatestOddNumber = array[i];
}
}
[Link]("The greatest odd number is: " + greatestOddNumber);
}
}
Output:

8
Abhishek A Chavan

14. Write any program which shows the use of ternary operator.
Ans.
Source Code:
public class TernaryOperatorExample
{
public static void main(String[] args)
{
int number1 = 10;
int number2 = 20;
int result;
result = (number1 > number2) ? number1 : number2;
[Link]("The result is: " + result);
}
}

Output:

You might also like