Programming in Java: Assignment I
Programming in Java: Assignment I
ASSIGNMENT I
Ashish Verma
(SGM 909)
&
Kartika Thakur
(SGM 921)
S. No. Name
1 Factorial of a Number
2 Prime Number from 1 to N
3 Day of given date
4 Pattern I
2 Pattern II
3 Pattern III
4 Pattern IV
5 Pattern V
6 Print A to Z.
7 Command Line Arguments.
8 Cryptography
9 Applet – Printing 'Hello Java'
10 Applet – Filling Color in.
11 Applet – Adding Number.
12 Applet – Pyramid in Three Colors.
PROGRAM I
//Main Function
public static void main(String[] agrs)
{
int number = 5;
int factorial = factorial(number);
System.out.println(factorial);
}
}
PROGRAM
class primenumber
{
public static void main(String[] args)
{
for(int i = 3; i <= 100; i++)
{
chk = 0;
for(int j = 2; j < i/2; j++)
{
if(i%j == 0)
{
chk = 1;
break;
}
}
if(chk == 0)
{
System.out.println(i);
}
}
}
}
PROGRAM
//Pattern I
/*
1
2 3
4 5 6
7 8 9 10
*/
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
// TODO code application logic here
int num = 1;
for(int lines = 1; lines <= 4; lines++)
{
for(int spaces = 1; spaces <= 5-lines; spaces++)
{
System.out.print(" ");
}
for(int stars = 1; stars <= lines; stars++)
{
System.out.print(num);
System.out.print(" ");
num ++;
}
System.out.println();
}
}
}
PROGRAM IV
// PATTERN II
/*
1
232
34543
4567654
*/
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
// TODO code application logic here
int num;
for(int lines = 1; lines <= 4; lines++)
{
num = lines;
for(int spaces = 1; spaces <= 5-lines; spaces++)
{
System.out.print(" ");
}
for(int stars = 1; stars <= lines; stars++)
{
System.out.print(num);
num ++;
}
num --;
for(int stars = 1; stars < lines; stars++)
{
num --;
System.out.print(num);
}
System.out.println();
}
}
}
PROGRAM
//PATTERN III
/*
*
* *
* * *
* * * *
* * * * *
*/
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
// TODO code application logic here
for(int lines = 1; lines <= 5; lines++)
{
for(int spaces = 1; spaces <= 5-lines; spaces++)
{
System.out.print(" ");
}
for(int stars = 1; stars <= lines; stars++)
{
System.out.print("* ");
}
System.out.println();
}
}
PROGRAM
// PATTERN IV
}
PROGRAM
// Printing A to Z.
import java.applet.Applet;
import java.awt.*;
/*<APPLET
CODE=applet.class
WIDTH=300
HEIGHT=200>
<PARAM NAME = string VALUE = "Hello from Java!">
</APPLET>*/
public class applet extends Applet
{
public void paint(Graphics g)
{
g.drawString(getParameter("string"), 60, 100);
System.out.println("Hello from Java");
}
}
PROGRAM
// Applet – Colored.
import java.applet.Applet;
import java.awt.*;
/*<APPLET
CODE=applet.class
WIDTH=300
HEIGHT=200>
<PARAM NAME = string VALUE = "Hello from Java!">
</APPLET>*/
public class applet extends Applet
{
public void init(){setBackground(Color.red);}
public void paint(Graphics g)
{
g.drawString(getParameter("string"), 60, 100);
System.out.println("Hello from Java");
}
}