Recent Post
Home
All posts
Thursday, January 26, 2017
National Flag ("Tiranga") Using Java
Following Code will create Our National Flag("Tiranga") using Java Applet
Code::
import java.applet.*;
import java.awt.*;
public class Republic extends Applet
{
public void paint(Graphics fl)
{ Color c1=new Color(255,140,0); //saffron color
Color c2=new Color(139,0,0); // dark red color
//for pole
fl.setColor(c2);
fl.fillRect(250,100,5,400);
fl.setColor(Color.black);
fl.drawRect(250,100,5,400);
//for saffron
fl.setColor(c1);
fl.fillRect(255,102,180,40);
fl.setColor(Color.black);
fl.drawRect(255,102,180,40);
//for white
fl.setColor(Color.WHITE);
fl.fillRect(255,142,180,40);
fl.setColor(Color.black);
fl.drawRect(255,142,180,40);
//for green
fl.setColor(Color.GREEN);
fl.fillRect(255,182,180,40);
fl.setColor(Color.black);
fl.drawRect(255,182,180,40);
// for background
Color c4= new Color(173,216,230);
setBackground(c4);
// for stairs
int j[]={250,245,245,225,225,280,280,260,260,255};
int k[]={500,500,505,505,515,515,505,505,500,500};
fl.setColor(c2);
fl.fillPolygon(j,k,10);
fl.setColor(Color.BLACK);
fl.drawPolygon(j,k,10);
// for ashok chakra
fl.setColor(Color.blue);
fl.drawOval(325,142,39,39);
// for lines in ashok chakra
int n1=345;
int d1=162;
double n2,d2;
double angle= 0.0; //for angle determination
double line=0.0;
int r=20;
for(int i=1;i<=24;i++)
{
angle=(double)line*(3.14/180);
n2=n1+(double)r*Math.cos(angle);
d2=d1+(double)r*Math.sin(angle);
fl.drawLine(n1,d1,(int)n2,(int)d2); //drawing line between (n1,d1) and (n2,d2)
line=line+(360/24);
}
// for text
Font f=new Font("Arial",Font.BOLD,36);
fl.setFont(f);
Color c3=new Color(0,100,0);
fl.setColor(c3);
fl.drawString("Happy Republic Day",240,595);
}
}
Output:
Happy Republic Day To All !!!
Code::
import java.applet.*;
import java.awt.*;
public class Republic extends Applet
{
public void paint(Graphics fl)
{ Color c1=new Color(255,140,0); //saffron color
Color c2=new Color(139,0,0); // dark red color
//for pole
fl.setColor(c2);
fl.fillRect(250,100,5,400);
fl.setColor(Color.black);
fl.drawRect(250,100,5,400);
//for saffron
fl.setColor(c1);
fl.fillRect(255,102,180,40);
fl.setColor(Color.black);
fl.drawRect(255,102,180,40);
//for white
fl.setColor(Color.WHITE);
fl.fillRect(255,142,180,40);
fl.setColor(Color.black);
fl.drawRect(255,142,180,40);
//for green
fl.setColor(Color.GREEN);
fl.fillRect(255,182,180,40);
fl.setColor(Color.black);
fl.drawRect(255,182,180,40);
// for background
Color c4= new Color(173,216,230);
setBackground(c4);
// for stairs
int j[]={250,245,245,225,225,280,280,260,260,255};
int k[]={500,500,505,505,515,515,505,505,500,500};
fl.setColor(c2);
fl.fillPolygon(j,k,10);
fl.setColor(Color.BLACK);
fl.drawPolygon(j,k,10);
// for ashok chakra
fl.setColor(Color.blue);
fl.drawOval(325,142,39,39);
// for lines in ashok chakra
int n1=345;
int d1=162;
double n2,d2;
double angle= 0.0; //for angle determination
double line=0.0;
int r=20;
for(int i=1;i<=24;i++)
{
angle=(double)line*(3.14/180);
n2=n1+(double)r*Math.cos(angle);
d2=d1+(double)r*Math.sin(angle);
fl.drawLine(n1,d1,(int)n2,(int)d2); //drawing line between (n1,d1) and (n2,d2)
line=line+(360/24);
}
// for text
Font f=new Font("Arial",Font.BOLD,36);
fl.setFont(f);
Color c3=new Color(0,100,0);
fl.setColor(c3);
fl.drawString("Happy Republic Day",240,595);
}
}
Output:
Happy Republic Day To All !!!
Tuesday, January 24, 2017
Number Pattern 4 Java :)
Following Java Program prints the Following Pattern:
0
1 0
0 1 0
1 0 1 0
0 1 0 1 0
import java.util.*;
public class patt5 {
public static void main(String args[]) {
int i, j;
Scanner in = new Scanner(System.in);
System.out.println("Enter the Length of Pattern: ");
int n = in.nextInt();
System.out.println("\nPattern is:");
for (i = 1; i <= n; i++) {
for (j = 1; j <= i; j++) {
if ((i + j) % 2 == 0) {
System.out.print("0 ");
} else {
System.out.print("1 ");
}
}
System.out.println();
}
}
}
0
1 0
0 1 0
1 0 1 0
0 1 0 1 0
import java.util.*;
public class patt5 {
public static void main(String args[]) {
int i, j;
Scanner in = new Scanner(System.in);
System.out.println("Enter the Length of Pattern: ");
int n = in.nextInt();
System.out.println("\nPattern is:");
for (i = 1; i <= n; i++) {
for (j = 1; j <= i; j++) {
if ((i + j) % 2 == 0) {
System.out.print("0 ");
} else {
System.out.print("1 ");
}
}
System.out.println();
}
}
}
Amount Investment Java
Following Code will calculate the number of years required to get Desired Amount in an Investment
Code::
import java.util.*;
public class Investment
{
public static void main(String[] nt)
{
Scanner in=new Scanner(System.in);
System.out.println("Enter Your Desired Amount");
int a=in.nextInt();
System.out.println("Enter Amount to Contribute Every year");
int b=in.nextInt();
System.out.println("Enter Rate of Interest in percent");
int r=in.nextInt();
int amount=0,year=0,interest=0;
while(amount<a)
{
amount=amount+b;
interest=amount*r/100;
amount=amount+interest;
year++;
}
System.out.printf("You will get Amount=%d in %d years\n",amount,year);
}
}
Output:
Code::
import java.util.*;
public class Investment
{
public static void main(String[] nt)
{
Scanner in=new Scanner(System.in);
System.out.println("Enter Your Desired Amount");
int a=in.nextInt();
System.out.println("Enter Amount to Contribute Every year");
int b=in.nextInt();
System.out.println("Enter Rate of Interest in percent");
int r=in.nextInt();
int amount=0,year=0,interest=0;
while(amount<a)
{
amount=amount+b;
interest=amount*r/100;
amount=amount+interest;
year++;
}
System.out.printf("You will get Amount=%d in %d years\n",amount,year);
}
}
Output:
Alphabet Pattern Java :)
This Program prints the following Pattern:
A
BB
CCC
DDDD
EEEEE
package patterns;
import java.util.*;
public class pattern1 {
public static void main(String args[]){
int i,j;
char c='A';
Scanner in=new Scanner(System.in);
System.out.println("Enter the Length of Pattern: ");
int n=in.nextInt();
System.out.println("\nPattern is:");
for(i=1;i<=n;i++){
for(j=1;j<=i;j++){
System.out.print(c);
}
c++;
System.out.println();
}
}
}
A
BB
CCC
DDDD
EEEEE
package patterns;
import java.util.*;
public class pattern1 {
public static void main(String args[]){
int i,j;
char c='A';
Scanner in=new Scanner(System.in);
System.out.println("Enter the Length of Pattern: ");
int n=in.nextInt();
System.out.println("\nPattern is:");
for(i=1;i<=n;i++){
for(j=1;j<=i;j++){
System.out.print(c);
}
c++;
System.out.println();
}
}
}
Star "*" Pyramid Java
Following pattern will be printed by the following code:
1
1 1
1 * 1
1 * * 1
1 1 1 1 1
Code::
import java.util.*;
public class Gndec {
public static void main(String nt[])
{
Scanner in = new Scanner(System.in);
System.out.println("Input the number of rows: ");
int n = in.nextInt();
int d=n; //for space
System.out.println("Pattern is :");
for (int i = 1; i <= n; i++)
{
for (int spc =d;spc > 0; spc--)
//print space
{
System.out.print(" ");
}
d--;
if(i==n) //last row
{
for(int j=0;j<i;j++)
{
System.out.print(1+" ");
}
}
else
{for (int j = 0; j <i; j++)
{
if (j==0||j==i-1)
{
System.out.print(1+" ");
} else
{
System.out.print("* ");
}
}
}
System.out.println();
}
}
}
Output:
1
1 1
1 * 1
1 * * 1
1 1 1 1 1
Code::
import java.util.*;
public class Gndec {
public static void main(String nt[])
{
Scanner in = new Scanner(System.in);
System.out.println("Input the number of rows: ");
int n = in.nextInt();
int d=n; //for space
System.out.println("Pattern is :");
for (int i = 1; i <= n; i++)
{
for (int spc =d;spc > 0; spc--)
//print space
{
System.out.print(" ");
}
d--;
if(i==n) //last row
{
for(int j=0;j<i;j++)
{
System.out.print(1+" ");
}
}
else
{for (int j = 0; j <i; j++)
{
if (j==0||j==i-1)
{
System.out.print(1+" ");
} else
{
System.out.print("* ");
}
}
}
System.out.println();
}
}
}
Output:
Alphabet Pyramid Java :)
Following code will print the following pattern::
A
A B A
A B C B A
Code::
import java.util.*;
public class Gndec
{
public static void main(String nt[])
{
Scanner in = new Scanner(System.in);
System.out.println("Input the number of rows: ");
int n = in.nextInt();
int d=n; //for space
int count = 1; // for no of characters in a row
char c = 'A';
System.out.println("Pattern is :");
for (int i = 1; i < n; i++)
{
for (int spc =d+3 ;spc > 0; spc--)
//print space
{
System.out.print(" ");
}
d--;
for (int j = 0; j < count; j++)
{
System.out.print(c);//print Character
if (j < count / 2)
{
c++;
} else
{
c--;
}
}
count=count+2;
c = 'A';
System.out.println();
}
}
}
Output:
A
A B A
A B C B A
Code::
import java.util.*;
public class Gndec
{
public static void main(String nt[])
{
Scanner in = new Scanner(System.in);
System.out.println("Input the number of rows: ");
int n = in.nextInt();
int d=n; //for space
int count = 1; // for no of characters in a row
char c = 'A';
System.out.println("Pattern is :");
for (int i = 1; i < n; i++)
{
for (int spc =d+3 ;spc > 0; spc--)
//print space
{
System.out.print(" ");
}
d--;
for (int j = 0; j < count; j++)
{
System.out.print(c);//print Character
if (j < count / 2)
{
c++;
} else
{
c--;
}
}
count=count+2;
c = 'A';
System.out.println();
}
}
}
Output:
Subscribe to:
Posts (Atom)