0% found this document useful (0 votes)
16 views

Java Lab2

Uploaded by

harsha14203
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)
16 views

Java Lab2

Uploaded by

harsha14203
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
You are on page 1/ 18

import java.util.

Scanner;

class Operations

public static void main(String args[])

int a,b,add,mul,sub,div,rem;

Scanner s=new Scanner(System.in);

System.out.println("Enter a value :");

a=s.nextInt();

System.out.println("Enetr b value :");

b=s.nextInt();

add=a+b;

sub=a-b;

mul=a*b;

div=a/b;

rem=a%b;

System.out.println("addition of a and b is "+"a +"+"b =" +add);

System.out.println("subtraction of a and b is "+"a -"+"b =" +sub);

System.out.println("multiplication of a and b is "+"a *"+"b =" +mul);

System.out.println("division of a and b is "+"a /"+"b =" +div);

System.out.println("remainder of a and b is "+"a %"+"b =" +rem);

}
import java.util.Scanner;

class Assign

public static void main(String args[])

int a,b,c,d;

Scanner s=new Scanner(System.in);

System.out.println("Enter the value of A:");

a=s.nextInt();

System.out.println("Enter the value of B:");

b=s.nextInt();

System.out.println("Enter the value of c:");

c=s.nextInt();

System.out.println("Enter the value of d:");

d=s.nextInt();

a+=b;

b-=c;

c*=d;
d/=a;

System.out.println("Value of a ="+a);

System.out.println("Value of b ="+b);

System.out.println("Value of c="+c);

System.out.println("Value of d="+d);

import java.util.Scanner;

class IncDec

public static void main(String args[])

int a,b;

Scanner s=new Scanner(System.in);

System.out.println("Enetr the value of a:");


a=s.nextInt();

System.out.println("Enetr the value of b:");

b=s.nextInt();

System.out.println("The Postincremented value of a="+ a++);

System.out.println(" the Pre incremented value of a="+ ++a);

System.out.println(" the Pre decremented value of b="+ --b);

System.out.println(" the Pre decremented value of b="+ b--);

import java.util.Scanner;

class Logics

public static void main(String args[])

int a,b,add,mul,sub,div,rem;
Scanner s=new Scanner(System.in);

System.out.println("Enter a value :");

a=s.nextInt();

System.out.println("Enetr b value :");

b=s.nextInt();

System.out.println((a>b)&&(a<b));

System.out.println((a<b)||(a>b));

System.out.println(!(a<b));

import java.util.Scanner;

class Relate

{ public static void main(String args[])

int a,b;

Scanner s=new Scanner(System.in);

System.out.println("Enter a value :");

a=s.nextInt();

System.out.println("Enetr b value :");


b=s.nextInt();

// value of a and b

System.out.println("a is " + a + " and b is " + b);

// == operator

System.out.println(a == b); // false

// != operator

System.out.println(a != b); // true

// > operator

System.out.println(a > b); // false

// < operator

System.out.println(a < b); // true

// >= operator

System.out.println(a >= b); // false

// <= operator

System.out.println(a <= b); // true

}
import java.util.Scanner;

class Ternary

public static void main(String[] args) {

int februaryDays = 29;

String result;

// ternary operator

result = (februaryDays == 28) ? "Not a leap year" : "Leap year";

System.out.println(result);

// II CONDITIONAL (if ifelse)

import java.util.Scanner;

class Conditional

public static void main(String s[])

{
int mark1,mark2,total;

Scanner inp=new Scanner(System.in);

System.out.print("Enter mark 1 : ");

mark1=inp.nextInt();

System.out.print("Enter mark 2 : ");

mark2=inp.nextInt();

total=mark1+mark2;

if(mark1>40&&mark2>40)

System.out.println("You are pass (using if statement)");

if(mark1>mark2)

System.out.println("mark1 is greater than mark2 (using if-else statement)");

else

System.out.println("mark1 is less than mark2 (using if-else statement)");

if(total>170)

System.out.println("You got A grade (using if-else-if)");

else

if(total>120)

System.out.println("You got B grade (using if-else-if statement)");

else

{
if(total>80)

System.out.println("You got C grade (using if-else-if)");

import java.util.Scanner;

class Switchcase

public static void main(String args[])

int num;

Scanner s=new Scanner(System.in);

System.out.println("Enter the value of N:");

num=s.nextInt();

switch(num)
{

case 1: System.out.println("MONDAY");

break;

case 2: System.out.println("TUESDAY");

break;

case 3: System.out.println("WEDNESDAY");

break;

case 4: System.out.println("THURSDAY");

break;

case 5: System.out.println("FRIDAY");

break;

case 6: System.out.println("SATURDAY");

break;

case 7: System.out.println("SUNDAY");

break;

}}
import java.util.Scanner;

class Natural

public static void main(String s[])

int num,sum=0,temp;

Scanner inp=new Scanner(System.in);

System.out.print("Enter a number (greater than 0): ");

num=inp.nextInt();

temp=num;

do

sum+=temp;

temp--;

}while(temp>0);

System.out.println("Sum of "+num+" natural number is "+sum);

}
import java.util.Scanner;

class Fact

public static void main(String s[])

int num,fact=1,temp;

Scanner inp=new Scanner(System.in);

System.out.print("Enter a number for finding factorial : ");

num=inp.nextInt();

temp=num;

while(num!=0)

fact*=num;

num--;

System.out.println("Factorial of "+temp+" is "+fact);

}
import java.util.Scanner;

class Fibo

public static void main(String s[])

int num1=0,num2=1,num3,lim;

Scanner inp=new Scanner(System.in);

System.out.print("Enter number of steps for fibonacci series : ");

lim=inp.nextInt();

System.out.print("\n\tFIBONACCI SERIES\n");

for(int i=1;i<=lim;i++)

if(i==1)

System.out.print(num1+" ");

else

if(i==2)

System.out.print(num2+" ");

else

num3=num1+num2;

System.out.print(num3+" ");

num1=num2;

num2=num3;

}
}

class Student

int regno;

String name;

Student(String n,int regnum)

this.name=n;

this.regno=regnum;

public static void main(String s[])

Student st1=new Student("VUSUVANDLA SRI HARSHA ",224003119);

Student st2=new Student("Praneet ",123456789);

System.out.println(st1.name+" "+st1.regno);

System.out.println(st2.name+" "+st2.regno);
}

Public class Constant

int regno;

String name;

Const()

this.name="Reddy";

this.regno=224003067;

Const(String n,int regnum)

this.name=n;

this.regno=regnum;

public static void main(String s[])

Const cst1=new Const();


Const cst2=new Const("Reddy ",200305599);

System.out.println(cst1.name+" "+cst1.regno);

System.out.println(cst2.name+" "+cst2.regno);

class Book

String BKName,BKAuthor;

int BKId;

Book(String name,int id,String author)

this.BKName=name;

this.BKId=id;

this.BKAuthor=author;

public void BKUpdateDetails(String name,int id,String author)

this.BKName=name;
this.BKId=id;

this.BKAuthor=author;

public void BKDisplay()

System.out.println(this.BKName+" "+this.BKId+" "+this.BKAuthor);

public class BookDemo

public static void main(String s[])

Book b=new Book("TITANIC ",4321,"FILSON YOUNG");

System.out.println("\n\tFIRST BOOK DETAIL");

b.BKDisplay();

b.BKUpdateDetails("WINGS OF FIRE",143,"A.P.J.KALAM");

System.out.println("\n\tUPDATED BOOK DETAIL");

b.BKDisplay();

}
}

You might also like