Lab Practical-1

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

ARITHMETIC OPERATIONS

AIM:

To write a java program to perform the arithmetic operations for the given numbers.

SOURCE CODE:

public class MyClass {

public static void main(String args[]) {

int b,c;

int x=25;

int y=10;

int z=x+y;

int a=x-y;

b=x*y;

c=x/y;

System.out.println(" x+y = " + z);

System.out.println(" x-y = " + a);

System.out.println(" x*y = " + b);

System.out.println(" x/y = " + c);

OUTPUT:

x+y = 35

x-y = 15

x*y = 250

x/y = 2

RESULT:

Thus the arithmetic operations are performed successfully.

FINDING THE BIGGEST NUMBER


AIM:

To write a java program to perform the find the biggest number.

SOURCE CODE:

public class MyClass {

public static void main(String args[]) {

int x=25;

int y=100;

if(x>y)

System.out.println(" Biggest Number: " + x);

else

System.out.println(" Biggest Number: " + y);

OUTPUT:

Biggest Number: 100

RESULT:

Thus the biggest number is displayed successfully.

FINDING ODD OR EVEN NUMBER

AIM:
To write a java program to perform the find the given number is odd or even.

SOURCE CODE:

public class MyClass {

public static void main(String args[]) {

int x=25;

if(x%2==0)

System.out.println("Given Number is Even ");

else

System.out.println("Given Number is Odd ");

OUTPUT:

Given Number is Odd

RESULT:

Thus the given number is found as odd or even and it is displayed successfully.

SIMPLE INTEREST CALCULATION

AIM:

To write a java program to calculate the simple interest.


SOURCE CODE:

public class HelloWorld {

public static void main(String[] args) {

double si,ci,p,n,r;

p=50000;

n=2;

r=5;

si=(p*n*r)/100;

System.out.println("Simple interest: "+si);

OUTPUT:

Simple interest: 5000.0

RESULT:

Thus the simple interest is calculated and displayed successfully.

FINDING POSITIVE, NEGATIVE OR ZERO NUMBER

AIM:

To write a java program to find the given number is positive, negative or zero..
SOURCE CODE:

class Number{

public static void main(String[] args) {

int n=-20;

if (n>0)

System.out.println("Given number is positive");

else if(n<0)

System.out.println("Given number is negative");

else

System.out.println("Given number is zero");

OUTPUT:

Given number is negative

RESULT:

Thus the given number is found positive, negative or zero and displayed successfully.

CALCULATING FACTORIAL VALUE

AIM:

To write a java program to calculate the factorial value for the given number.

SOURCE CODE:

class HelloWorld {
public static void main(String[] args) {

int fact =1, n=5;

while(n>0)

fact=fact*i;

n=n-1;

System.out.println("Factorial value of "+n +":"+fact);

OUTPUT:

Factorial value of 5:120

RESULT:

Thus the factorial value for the given number is calculated and displayed.

SWAPPING OF TWO NUMBERS

AIM:

To write a java program to swap the given two numbers.

SOURCE CODE:

class Swap {

public static void main(String[] args) {

int a=40, b=30;


a=a-b;

b=a+b;

a=b-a;

System.out.println("Swapping of two numbers");

System.out.println("Value in a:"+a);

System.out.println("Value in b:"+b);

OUTPUT:

Swapping of two numbers

Value in a:30

Value in b:40

RESULT:

Thus the given numbers are swapped successfully.

SUM OF 10 NATURAL NUMBERS

AIM:

To write a java program to calculate the sum of 10 natural numbers.

SOURCE CODE:

class Sum {

public static void main(String[] args) {

int i, sum=0;

for (i=1;i<=10;i++)
sum=sum+i;

System.out.println("Sum of 10 natural numbers:"+sum);

OUTPUT:

Sum of 10 natural numbers:55

RESULT:

Thus the sum of 10 natural numbers are calculated and displayed.

DISPLAYING ELEMENTS USING ARRAY

AIM:

To write a java program to display the elements using array.

SOURCE CODE:

class Array {

public static void main(String args[]) {

int a[]=new int[5];

a[0]=10;

a[1]=20;
a[2]=70;

a[3]=40;

a[4]=50;

System.out.println("Values are: ");

for(int i=0;i<a.length;i++)

System.out.println(a[i]);

OUTPUT:

Values are: 10
20
70
40
50
RESULT:

Thus the array elements are displayed successfully.

FINDING LEAP YEAR OR NOT

AIM:

To write a java program to find the given year is a leap year or not.

SOURCE CODE:

class Leap {

int year=2022;

public void display(){

if(year%4==0)

System.out.println(year+" is a Leap Year");

else
System.out.println(year+" is not a Leap Year");

class year{

public static void main(String[] args) {

Leap y = new Leap();

y.display();

OUTPUT:

2022 is not a Leap Year

RESULT:

Thus the given year is found whether leap year or not and displayed successfully.

PARAMETERIZED CONSTRUCTOR

AIM:

To write a java program to implement parameterized constructor.

SOURCE CODE:

class Constructor {
int a, b;
Constructor(int x, int y)
{
a=x;
b=y;
System.out.println("Parameterized Constructor");
}
public void display(){
System.out.println("Value of a: "+a);
System.out.println("Value of b: "+b);
}
}

class Mainobj{
public static void main(String[] args) {
Constructor c = new Constructor(100,200);
c.display();
}
}

OUTPUT:

Parameterized Constructor

Value of a: 100

Value of b: 200

RESULT:

Thus the program is executed successfully.

DISPLAY PERSONAL DETAILS

AIM:

To write a java program to get input from the user and display the details.

SOURCE CODE:

import java.util.Scanner;

class DataInput {

public static void main(String args[]) {

Scanner user_input = new Scanner(System.in);

System.out.print("Enter your name:");

String name = user_input.next();

System.out.print("Enter your age:");

String age_string = user_input.next( );

int age = Integer.parseInt(age_string);

System.out.println("Hello "+ name);


System.out.print("In 5 years you will be "+ (age+5));

System.out.println(" years old.");

OUTPUT:

Enter your name:Ajay


Enter your age:16
Hello Ajay
In 5 years you will be 21years old.
RESULT:

Thus the input from the user is obtained and the details are displayed successfully.

You might also like