24
1)To print Hello on console. class Hello { public static void main(String args[]) { System.out.println("Hello World!!"); } } output:- C:\Users\Desktop\Java Prgs\java Hello Hello World!! 2)To print first 20 terms of fibonacci series. package helloworld; import java.io.*; public class Fibonacci { public static void main(String[] args) { System.out.println("Representing the Fibonacci series.."); int[] seq=new int[21]; seq[1]=0; seq[2]=1; for(int i=3;i<=20;i++) { seq[i]=seq[i-1]+seq[i-2]; } for(int j=1;j<=20;j++) { System.out.print(seq[j]+" "); } }

java file

Embed Size (px)

DESCRIPTION

java

Citation preview

1)To print Hello on console.

class Hello{

public static void main(String args[]){

System.out.println("Hello World!!");}

}

output:-

C:\Users\Desktop\Java Prgs\java HelloHello World!!

2)To print first 20 terms of fibonacci series.package helloworld;import java.io.*;

public class Fibonacci{ public static void main(String[] args) { System.out.println("Representing the Fibonacci series.."); int[] seq=new int[21]; seq[1]=0; seq[2]=1; for(int i=3;i<=20;i++) { seq[i]=seq[i-1]+seq[i-2]; } for(int j=1;j<=20;j++) { System.out.print(seq[j]+" "); }

}}

Output::

Representing the Fibonacci series..0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181

BUILD SUCCESSFUL (total time: 0 seconds)

3)To check the primality of a number.

package helloworld;import java.util.Scanner;

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

{ Scanner scan=new Scanner(System.in); System.out.println("Checking for primality of a number..."); System.out.println("Enter the number"); double num=scan.nextDouble(); System.out.println("number= "+num);

int flag=0; double srt=Math.sqrt(num); System.out.println("Sqrt is: "+srt); for(int i=2;i<=srt;i++) { if(num%i!=0) {flag=0; continue; } else{flag=1; break; } }

if(flag==1){ System.out.println("Not a prime number"); } else { System.out.println("It is a prime number"); }

}

output::

run:for primality of a number...Enter the number69number= 69.0Sqrt is: 8.306623862918075Not a prime number

BUILD SUCCESSFUL (total time: 1 second)

4)To print the reverse of the number, the number is taken as input

from the user.import java.util.Scanner;

class Reverse

{

public static void main(String args[])

{

System.out.print("enter the number ::");

Scanner scan=new Scanner(System.in);

int a,i=0;

a=scan.nextInt();

int[] digit=new int[3];

while(a>=10)

{

digit[i]=a%10;

a=a/10;

i++;

}

digit[i]=a;

int s=0;

for(int j=0;j<=i;j++)

s=((s*10)+(digit[j]));

}

System.out.println(s);

}

}

OUTPUT

5)To calculate the occurrences of every digit in a number,the number is taken as an input from the user.

import java.util.Scanner;

class Occurences

{

public static void main(String args[])

{

System.out.print("enter the number ::");

Scanner scan=new Scanner(System.in);

int num;

num=scan.nextInt();

int[] digit=new int[10];

int a;

while(num>=10)

{

a=num%10;

switch(a)

{

case 0:digit[0]=(digit[0]+1);

break;

case 1:digit[1]=(digit[1]+1);

break;

case 2:digit[2]=(digit[2]+1);

break;

case 3:digit[3]=(digit[3]+1);

break;

case 4:digit[4]=(digit[4]+1);

break;

case 5:digit[5]=(digit[5]+1);

break;

case 6:digit[6]=(digit[6]+1);

break;

case 7:digit[7]=(digit[7]+1);

break;

case 8:digit[8]=(digit[8]+1);

break;

case 9:digit[9]=(digit[9]+1);

break;

}

num=num/10;

}

switch(num)

{

case 0:digit[0]=(digit[0]+1);

break;

case 1:digit[1]=(digit[1]+1);

break;

case 2:digit[2]=(digit[2]+1);

break;

case 3:digit[3]=(digit[3]+1);

break;

case 4:digit[4]=(digit[4]+1);

break;

case 5:digit[5]=(digit[5]+1);

break;

case 6:digit[6]=(digit[6]+1);

break;

case 7:digit[7]=(digit[7]+1);

break;

case 8:digit[8]=(digit[8]+1);

break;

case 9:digit[9]=(digit[9]+1);

break;

}

for(int i=0;i<10;i++)

{

System.out.println("digit "+i+" has been repeated "+digit[i]+" times");

}

}

}

OUTPUT

6)To maintain bank account. Extend Bank account details to current

and saving account.importjava.util.Scanner;

class Account

{

Account()

{

Accountno=0;

balance=0;

name=" ";

}

intAccountno;

float balance;

String name;

public void deposit(float dep)

{

balance=(balance+dep);

}

voidgetdata()

{

System.out.println("Your Account number is:"+Accountno);

System.out.println("The name of the account holder is:"+name);

System.out.println("Your balance is:"+balance);

}

}

class Saving extends Account

{

Saving()

{

Accountno=0;

balance=0;

name=" ";

}

Saving(intnum,floatbal,String s)

{

Accountno=num;

balance=bal;

name=s;

}

public voidswithdrawl(float wdraw)

{

if((balance-wdraw)>1000)

{

balance=balance-wdraw;

}

else

{

System.out.println("There is not enough balance in your account to withdraw the given amount");

}

}

}

class Current extends Account

{

Current()

{

Accountno=0;

balance=0;

name=" ";

}

Current(intnum,floatbal,String s)

{

Accountno=num;

balance=bal;

name=s;

}

public void cwithdrawl(float wdraw)

{

if((balance-wdraw)>-10000)

{

balance=balance-wdraw;

}

else

{

System.out.println("There is not enough balance in your account to withdraw the given amount");

}

}

}

class Bank

{

public static void main(String args[])

{

Saving ob1=new Saving();

Current ob2=new Current();

intch,num,choice,p;

floatbal,dep,with;

int c;

do

{

System.out.println("Enter the type of Account:(1 for savings and 2 for current):");

Scanner scan=new Scanner(System.in);

ch=scan.nextInt();

if(ch==1)

{

do

{

System.out.println("Enter your choice:1 for creating a new account,2 for deposit and 3 for

withdrawl:");

c=scan.nextInt();

if(c==1)

{

System.out.println("Enter the Account number:");

num=scan.nextInt();

System.out.println("Enter the name of the Account Holder:");

String str= scan.next();

System.out.println("Enter the Account Balance:");

bal=scan.nextFloat();

ob1=new Saving(num,bal,str);

ob1.getdata();

}

if(c==2)

{

System.out.println("Enter the amount to be deposited:");

dep=scan.nextFloat();

ob1.deposit(dep);

ob1.getdata();

}

if(c==3)

{

System.out.println("Enter the amount to be withdrawn:");

with=scan.nextFloat();

ob1.swithdrawl(with);

ob1.getdata();

}

System.out.println("Do you want to continue:(press 0 for yes and 1 for no):");

choice=scan.nextInt();

}while(choice==0);

}

else

{

do

{

System.out.println("Enter your choice:1 for creating a new account,2 for deposit and 3 for

withdrawl:");

c=scan.nextInt();

if(c==1)

{

System.out.println("Enter the Account number:");

num=scan.nextInt();

System.out.println("Enter the name of the Account Holder:");

String str= scan.next();

System.out.println("Enter the Account Balance:");

bal=scan.nextFloat();

ob2=new Current(num,bal,str);

ob2.getdata();

}

if(c==2)

{

System.out.println("Enter the amount to be deposited:");

dep=scan.nextFloat();

ob2.deposit(dep);

ob2.getdata();

}

if(c==3)

{

System.out.println("Enter the amount to be withdrawn:");

with=scan.nextFloat();

ob2.cwithdrawl(with);

ob2.getdata();

}

System.out.println("Do you want to continue:(press 0 for yes and 1 for no):");

choice=scan.nextInt();

}while(choice==0);

}

System.out.println("Do you want to deal with another account:");

p=scan.nextInt();

} while(p==0);

} }

OUTPUT

7)To maintain Bank Account using packages.package pack;

importjava.util.Scanner;

public class Account

{

public Account()

{

Accountno=0;

balance=0;

name=" ";

}

intAccountno;

float balance;

String name;

public void deposit(float dep)

{

balance=(balance+dep);

}

public void getdata()

{

System.out.println("Your Account number is:"+Accountno);

System.out.println("The name of the account holder is:"+name);

System.out.println("Your balance is:"+balance);

}

}

package pack;

importjava.util.Scanner;

public class Current extends Account

{

public Current()

{

Accountno=0;

balance=0;

name=" ";

}

public Current(intnum,floatbal,String s)

{

Accountno=num;

balance=bal;

name=s;

}

public void cwithdrawl(float wdraw)

{

if((balance-wdraw)>-10000)

{

balance=balance-wdraw;

}

else

{

System.out.println("There is not enough balance in your account to withdraw the given amount”);

}

}

}

package pack;

importjava.util.Scanner;

public class Saving extends Account

{

public Saving()

{

Accountno=0;

balance=0;

name=" ";

}

public Saving(intnum,floatbal,String s)

{

Accountno=num;

balance=bal;

name=s;

}

public voidswithdrawl(float wdraw)

{

if((balance-wdraw)>1000)

{

balance=balance-wdraw;

}

else

{

System.out.println("There is not enough balance in your account to withdraw the given amount");

}

}

}

import pack.*;

importjava.util.*;

class Packages

{

public static void main(String args[])

{

Saving ob1=new Saving();

Current ob2=new Current();

intch,num,choice,p;

floatbal,dep,with;

int c;

do

{

System.out.println("Enter the type of Account:(1 for savings and 2 for current):");

Scanner scan=new Scanner(System.in);

ch=scan.nextInt();

if(ch==1)

{

do

{

System.out.println("Enter your choice:1 for creating a new account,2 for deposit and 3

forwithdrawl:”);

c=scan.nextInt();

if(c==1)

{

System.out.println("Enter the Account number:");

num=scan.nextInt();

System.out.println("Enter the name of the Account Holder:");

String str= scan.next();

System.out.println("Enter the Account Balance:");

bal=scan.nextFloat();

ob1=new Saving(num,bal,str);

ob1.getdata();

}

if(c==2)

{

System.out.println("Enter the amount to be deposited:");

dep=scan.nextFloat();

ob1.deposit(dep);

ob1.getdata();

}

if(c==3)

{

System.out.println("Enter the amount to be withdrawn:");

with=scan.nextFloat();

ob1.swithdrawl(with);

ob1.getdata();

}

System.out.println("Do you want to continue:(press 0 for yes and 1 for no):");

choice=scan.nextInt();

}while(choice==0);

}

else

{

do

{

System.out.println("Enter your choice:1 for creating a new account,2 for deposit and 3

forwithdrawl:”);

c=scan.nextInt();

if(c==1)

{

System.out.println("Enter the Account number:");

num=scan.nextInt();

System.out.println("Enter the name of the Account Holder:");

String str= scan.next();

System.out.println("Enter the Account Balance:");

bal=scan.nextFloat();

ob2=new Current(num,bal,str);

ob2.getdata();

}

if(c==2)

{

System.out.println("Enter the amount to be deposited:");

dep=scan.nextFloat();

ob2.deposit(dep);

ob2.getdata();

}

if(c==3)

{

System.out.println("Enter the amount to be withdrawn:");

with=scan.nextFloat();

ob2.cwithdrawl(with);

ob2.getdata();

}

System.out.println("Do you want to continue:(press 0 for yes and 1 for no):");

choice=scan.nextInt();

}while(choice==0);

}

System.out.println("Do you want to deal with another account:");

p=scan.nextInt();

} while(p==0);

}

}

OUTPUT

8)To create a main thread and perform operations on it.

classMythread{

public static void main(String args[]){

Thread t=Thread.currentThread();System.out.println("Current thread: "+t);try{

for(int i=1;i<=5;i++){

System.out.println(i);Thread.sleep(1000);

}}catch(InterruptedException e){System.out.println("Main thread Interrupted");}

}}

output:-

C:\Users\Desktop\java prgs>java Mythread

Current thread: Thread[main,5,main]12345

C:\Users\Desktop\java prgs>