Java lab programs

Java basic programs



1.Write a java program to add two numbers



public class Add

{

    public static void main(String arg[])

        int num1=5, num2= 3, sum;

        sum =num1+num2;

    System.out.println("The addition of two numbers is:" +sum);

    }

}

output:

The addition of two numbers is 8

2.Write a java program to find the number even or odd.

import java.util.Scanner;

class Evenodd

{

    public static void main(string args[])

      {

        int =num;

        system.outprintln("Enter the number:" );

       scanner input = new scanner (system.in)

       num= input.nextInt();

       if (num %2 =0)

        system.out.println("The number is even:");

       else

        system.out.println("The number is odd:");

    }

}

output:

Enter the number: 10

The number is even

Enter the number:9

The number is odd


3.Write a java program to perform all arithmetic operations.

import java.util.scanner;

public class Arith

{

    public static void main (String args[]);

    scanner in =new.scanner (system.in);

    system.out.println("Enter first number:");

    int num1= in.nextInt();

    system.out.print("Enter second number:");

    int num2= in.nextInt();

    int sum=num1+num2;

    int sub=num1-num2;

    int product =num1*num2;

    int division =num1/num2;

    system.out.println("sum:" +sum);

    system.out.println("sub:" +sub);

    system.out.print("product:" +product);

    system.out..println("division:" +division);

    }

}

output:

Enter first number:10

Enter second number:5

sum:15

sub:5

product:50

division:2



4.Write a java program to find the largest number.

public class Largest

{

public static void main (String args[]);

{

    int num1=5,num2=4,num3=10,sum;

    if (num1 >= num2 && num1>=num3)

        system.out.println(num1 +"is the greatest number")

    else if (num2>=num1 && num2>=num3)

        system.out.println(num2 +" is  the greatest number")

    else 

        system.out.println(num3 +" is the greates number")

    }

}

output:

10 is the largest number


5.Write a java program to find the year is leap year or not.

import java.util.scanner;

public class Leapyear

{

    public static void main (String args[])

        int year;

        scanner scan =new scanner(system.in);

        system.ou.println("Enter the year:");

        year=scan.nextInt();

        scan.close();

        boolean isLeap = false;

        if (year%4 == 0)

        {

        if (year %100 == 0)

        {

            if (year %400 ==0)


            isLeap = true;


    else

    {

        isLeap =true;

    }

    else

       isleap=false;

       }

    if(isLeap=true)

    system.out.println (year +"is a leap year");

    else

    System.out.println(year +:is not a leap year");

    }

}

output:

Enter the year:

2005

is not a leap year

6.Write a java program to add two strings


class StringConcatenation

{

    public static void main(String args[])

    {

    string s="Sachin" + "tendulkar";

    system.out.println(s);

   }

}

output:

Sachin tendulkar

7.Write a program to swap two numbers using temporary variable

public class Swap

{

public static void main(String args[])

{

float first =1.20f,second =2.45f;

system.out.println("Before swap");

system.out.println("First number:"+first);

system.out.println("Second number:"+second);

float temproary =first;

first=second;

second=temproary;

system.out.println("After swap")

sytem.out.println("First number=" +first)

system.out.println("Second number=" +second)

}

}

output:

Before swap

First number =1.2

Second number =2.45

After swap

First number =2.45

Second number=1.2

8.Write a java program to swap two number without using temparory variable

public class Swapnum

{

public static void main(String args[]);

{

float first =12.0f,second =24.5f;

system.out.println("Before swap");

system.out.println("First number:"+first);

system.out.println("Second number:"+second);

first = first - second;

second =first +second;

first = second - first;

system.out.println("After swap")

sytem.out.println("First number=" +first)

system.out.println("Second number=" +second)

}

}

output:

Before swap

First number =12.0

second number= 24.5

After swap

First number =24.5

Second number=12.0

9.Write a java program to calculate simple interest

import java.util.scanner;

public class SimpleInterest

{

public static void main(String args[]);

{

float p,n,r,simple interest;

scanner input = new scanner (system.in)

sytem.out.println("Enter the pricipal:");

p=scan.nextFloat();

sytem.out.println("Enter rate of interest:");

r=scan.nextFloat();

sytem.out.println("Enter the time period:");

t=scan.nextFloat();

scan.close();

sinterest=(p*q*r)/100;

sytem.out.println("simple interest is"+sinterest);

}

}

output:

Enter the principal:2000

Enter the rate of interest:6

Enter the time period:3

simple interest:360.0

10.Write a java program to calculate compound interest

public class Compound

{

public void calculate(int p, int t ,double r , int n)

{

double amount =p*Math.pow(1+(r/n),n*t);

double cinterest=amount-p;

sytem.out.println("Compound interest after years" +t+ "years" +cinterest);

sytem.out.println("Amount after " +t+ "years"+ amount);

public static void main(String args[]);

Compound obj =new Compund();

obj.calculate(2000,5,.08,12);

}

}

output:

Compound interest after 5 years:979.69

Amount after 5 years:2979.69 



content: Education

Comments

Popular posts from this blog

R Language.

How to install R language and R studio in windows 10.

How does an IPL Player earn?