class PrimeNo{
public static void main(String ar[]){
int n=50;
boolean isPrime=false;
for(int i=3;i<=n;i++){
for(int j=2;j<i;j++){
if(i%j==0){
isPrime=false;
break;
}
else
isPrime=true;
}
if(isPrime)
System.out.println(i+" is prime");
}
}
}
Printing Prime number in java
Pattern [* square ]
class Pattern2{
public static void main(String ar[]){
int n=20;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(i==1||i==n){
System.out.print(" *");
}else if(j==1||j==n){
System.out.print(" *");
}else{
System.out.print(" ");
}
}
System.out.println("");
}
}
}
/*
* * * * * * * * * * * * * * * * * * * * *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* * * * * * * * * * * * * * * * * * * *
*/
Pattern
class Pattern1{
public static void main(String ar[]){
char []chrs={'a','b','c','d','e','f','g','h','i','j','k','l','m'};
for(int i=0;i<chrs.length;i++){
for(int j=0;j<2*chrs.length;j++){
if(j<=i||(2*chrs.length-j)<=i+1){
System.out.print(chrs[i]);
}else{
System.out.print(" ");
}
}
System.out.println("");
}
}
}
/* OUTPUT
a a
bb bb
ccc ccc
dddd dddd
eeeee eeeee
ffffff ffffff
ggggggg ggggggg
hhhhhhhh hhhhhhhh
iiiiiiiii iiiiiiiii
jjjjjjjjjj jjjjjjjjjj
kkkkkkkkkkk kkkkkkkkkkk
llllllllllll llllllllllll
mmmmmmmmmmmmmmmmmmmmmmmmmm
*/
GCD of two numbers
class Q1iPage139{ static int a=20,b=30; public static void main(String ar[]){ int x=test(6,18); System.out.println(x); } static int test(int a,int b){ int gcd=0; int c=(a<b)?a:b; for(int i=1;i<=c;i++){ if((a%i==0)&&(b%i==0)){ gcd=i; } } return gcd; } }
Program to check palindrom of string
//Program to check palindrom of string class Q4ipage154{ public static void main(String ar[]){ boolean b=pal("nitin"); System.out.println(b); } static boolean pal(String s) { boolean p=true; int start=0,end=s.length()-1; while(start<end){ if(!(s.charAt(start)==s.charAt(end))){ p=false; break; } start++; end--; } return p; } }
Pattern
class Q3apage246{ public static void main(String ar[]){ int n=4; int a[][]=new int [n][]; int c=0; for(int i=0;i<n;i++){ a[i]=new int[i+1]; for(int j=0;j<n;j++){ if(j<=i){ a[i][j]=c; c++; } } } for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ if(j<=i){ System.out.print(a[i][j]); } } System.out.println(""); } } }
OUTPUT
0 12 345 6789
Fibnacci series (rec)
class Fib{ static int fib(int a) { if (a==1||a==2) return 1; else{ int n=fib(a-1)+fib(a-2); return n; } } public static void main(String ar[])throws Exception { int a=10,i; for(i=1;i<=a;i++) { System.out.printf("%5d",fib(i)); } } }
Single digit sum
/*189=9
* 1+8+9=18
* 1+8=9 (output)
*/
class Main{
static int rem=0,s=0;
static void sum(int num){
s=0;
while(num>0){
rem=num%10;
num=num/10;
s=s+rem;
}
if(s>=10){
sum(s);
}
}
public static void main(String ar[]){
int d=1891;
sum(d);
System.out.println("Single Digit sum ="+s);
}
}
* 1+8+9=18
* 1+8=9 (output)
*/
class Main{
static int rem=0,s=0;
static void sum(int num){
s=0;
while(num>0){
rem=num%10;
num=num/10;
s=s+rem;
}
if(s>=10){
sum(s);
}
}
public static void main(String ar[]){
int d=1891;
sum(d);
System.out.println("Single Digit sum ="+s);
}
}
Patterns
class Pattern{
public static void main(String ar[]){
int n=5;
for(int i=0;i<n;i++){
for(int j=1;j<=(2*n);j++){
int start=n-i;
int end=n+i;
if((j>=start)&&(j<=(end))){
System.out.print(i+1);
}
else{
System.out.print(" ");
}
}
System.out.println("");
}
}
}
Patterns
class Pattern{
public static void main(String ar[]){
int n=20;
for(int i=0;i<n;i++){
for(int j=1;j<=(2*n);j++){
int start=n-i;
int end=n+i;
if((j>=start)&&(j<=(end))){
System.out.printf("%2d",i+1);
}
else{
System.out.printf("%2s"," ");
}
}
System.out.println("");
}
}
}
Printing 2D array using recurrence method
class Pattern2DArray{
import java.io.*;
class Matrix{
static int i=0,j=0;
public static void main(String ar[])throws IOException{
int a[][]={{1,2,3},{2,2,2},{1,2,3}};
printArray(a);
}
public static void printArray(int arr[][]){
if(i>arr.length-1)
{
return;
}
else
{
System.out.print(" "+arr[i][j]);
if(i<arr.length && j==arr[i].length-1){
i++;
j=0;
System.out.println();
printArray(arr);
}else{
j++;
printArray(arr);
}
}
}
}
Printing array using recurrence method
import java.io.*;
class PrintArray{
static int i=0,j=0;
public static void main(String ar[])throws IOException{
int a[]={23,23,23,5,45,4,65,6,34,7,56,7,88,78,7,5,45,6,4};
printArray(a);
}
public static void printArray(int arr[]){
if(i>arr.length-1)
{
return;
}
else
{
System.out.println(" "+arr[i]);
i++;
printArray(arr);
}
}
}
Matrix operations
import java.io.*;
class Matrix{
public static void main(String ar[])throws IOException{
int a[][]={{1,2,3},{2,2,2},{1,2,3}};
int b[][]={{1,2,3},{2,2,2},{1,2,3}};
int c[][]=new int[3][3];
/*
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("First Array");
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
System.out.println("Enter element of "+i+","+j);
a[i][j]=Integer.parseInt(br.readLine());
}
}
System.out.println("Second Array");
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
System.out.println("Enter element of "+i+","+j);
b[i][j]=Integer.parseInt(br.readLine());
}
}
*/
//adding
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
c[i][j]=a[i][j]+b[i][j];
}
}
System.out.println("First Array");
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
System.out.print(a[i][j]+" ");
}
System.out.println("");
}
System.out.println("Second Array");
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
System.out.print(b[i][j]+" ");
}
System.out.println("");
}
System.out.println("Addition of Arrays");
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
System.out.print(c[i][j]+" ");
}
System.out.println("");
}
}
}
LCM
class LCM{
public static void main(String ar[]){
int a=8,b=12;
double x=((a*b)/gcd(a,b));
System.out.println("LCM = "+x);
}
static int gcd(int a,int b)
{
if (a>=b&&a%b==0)
return(b);
else if(a<b)
return(gcd(b,a));
else
return(gcd(b,a%b));
}
}
GCD
GCD
class GCD{
public static void main(String ar[]){
int x=gcd(12,16);
System.out.println("gcd = "+x);
}
static int gcd(int a,int b)
{
if (a>=b&&a%b==0)
return(b);
else if(a<b)
return(gcd(b,a));
else
return(gcd(b,a%b));
}
}
Program to represent complex numbers and adding them
class ComplexNumber {
int r=0;
int i=0;
String c;
ComplexNumber(int r,int i){
this.r=r;
this.i=i;
if(i>=0)
c=""+r+"+"+i+"i";
else
c=""+r+i+"i";
}
public String toString(){
return c;
}
public ComplexNumber add(ComplexNumber c){
int r=this.r+c.r;
int i=this.i+c.i;
return new ComplexNumber(r,i);
}
}
class Complex{
public static void main(String ar[]) {
ComplexNumber c1=new ComplexNumber(2,3);
ComplexNumber c2=new ComplexNumber(1,-2);
ComplexNumber c3=c1.add(c2);
System.out.println(c1);
System.out.println(c2);
System.out.println(c3);
}
}
Subscribe to:
Posts (Atom)
Popular Posts
-
HSQLDB is a portable RDBMS implemented in pure java. It can be embedded with your application as well as can be used separately. It is very ...
-
If you want to use the database into your web application, you can use the HSQLDB in In_Process mode. In this mode, you can embed the HSQLD...
-
HTML Page < html > < head > < title > welcome </ title > < script language = "javascript"...
-
In web application ApplicationContext is created using Context Loaders. there are two implementations of context loader. ContextLoaderLis...
-
HSQLDB database it a very small database that can be controlled from you application. you can start and stop the database by writing the ...
-
JSP code <%@page import="java.sql.*"%> <%! int prod_id=1; String prod_name="Laptop"; int qty=2; float p...
-
Some time you will see the form containing the button " Add More " . This facility is provided for the user to get the values ...
-
StudentRegistration │ index.html │ login.html │ └───WEB-INF │ web.xml │ └───classes login.class lo...
-
package process; import javax.swing. * ; import javax.swing.table. * ; import java.sql. * ; /** * This class create JTable from Da...
-
Hibernate ORM framework does not provide any approch to connect to MS Access database. Unfortunately, Access is not supported officially...