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

Programming in Java: Assignment I

This document contains 12 Java programming assignments including programs to calculate factorials, find prime numbers, print patterns, and create applets. The programs cover basic concepts like loops, conditionals, methods, and graphics. Each program is presented with its source code and comments to explain the logic.

Uploaded by

Ashish Verma
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
167 views

Programming in Java: Assignment I

This document contains 12 Java programming assignments including programs to calculate factorials, find prime numbers, print patterns, and create applets. The programs cover basic concepts like loops, conditionals, methods, and graphics. Each program is presented with its source code and comments to explain the logic.

Uploaded by

Ashish Verma
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

PROGRAMMING IN JAVA

ASSIGNMENT I

Ashish Verma
(SGM 909)

&

Kartika Thakur
(SGM 921)

Prof. Gurjit Singh


(DCSA SSG PURC)
INDEX

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

// Program to find the factoral of a number.


class factorial
{

// Following function calculates factorial using recursion.


public static int factorial(int num)
{
if(num == 1)
{
return 1;
}
else
{
return(num*(factorial(num-1)));
}
}

//Main Function
public static void main(String[] agrs)
{
int number = 5;
int factorial = factorial(number);
System.out.println(factorial);
}
}
PROGRAM

//PRIME NUMBERS FROM 1 TO 100

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
*/

public class Main


{

/**
* @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
*/

public class Main


{

/**
* @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
/*
*
* *
* * *
* * * *
* * * * *
*/

public class Main


{

/**
* @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

public class Main


{
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("*");
}
for(int stars1 = 1; stars1 < lines; stars1++)
{
System.out.print("*");
}
System.out.println("");
}

for (int lines = 1; lines <= 4; lines++)


{
for(int spaces = 1; spaces <= lines; spaces++)
{
System.out.print(" ");
}
for(int stars = 1; stars <= 5-lines; stars++)
{
System.out.print("*");
}
for(int stars1 = 1; stars1 < 5-lines; stars1++)
{
System.out.print("*");
}
System.out.println();
}
}

}
PROGRAM
// Printing A to Z.

public class Main


{
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
char x = 'A';
while (x != '[')
{
System.out.print(x);
System.out.print(" ");
x ++;
}
}
}
PROGRAM
//APPLET – PROGRAM TO PRINT 'HELLO FROM JAVA'

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");
}
}

You might also like