You are on page 1of 11

CONSOLE PROGRAMMING OF JAVA

1. Program of printing a Hello World message


Sample Output:

Code:
public class HelloWorld {
public static void main(String[] args) {
System.out.print("Hello World");
}
}




























2. Program of printing of a triangle made of asterisk ( * ) of the desired size by the user
Sample Output

Code:
import java.util.Scanner;
public class InvTriangle {
public static void main(String[] args) {
int i=0, j=0;
System.out.print("Enter Size: ");
Scanner ob = new Scanner(System.in);
int size= ob.nextInt();
for(i=0; i<size; i++)
{
for(j=i;j<size;j++)
{
System.out.print("*");
}
System.out.print("\n");
}
}
}









Sample Output



Code:
import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
int i=0, j=0;
System.out.print("Enter Size: ");
Scanner ob = new Scanner(System.in);
int size= ob.nextInt();
for(i=0; i<=size; i++)
{
for(j=0;j<i;j++)
{
System.out.print("*");
}
System.out.print("\n");
}
}
}











Sample Output:

Code:
import java.util.Scanner;
public class Triangle2 {
public static void main(String[] args) {
int i=0, j=0;
Scanner input = new Scanner(System.in);
System.out.print("Enter Size: ");
int size= input.nextInt();
for(i=0; i<size; i++)
{
for(j=0;j<i;j++)
{
System.out.print("*");
}
System.out.print("\n");
}
for(i=0; i<size; i++)
{
for(j=i;j<size;j++)
{
System.out.print("*");
}
System.out.print("\n");
}
}
}






3. Program of printing of a rectangle made of asterisk ( * ) of the desired size by the user
Sample Output:

Code:
import java.util.Scanner;
public class Rectangle {
public static void main(String[] args) {
int i=0, j=0;
Scanner input = new Scanner(System.in);
System.out.print("Enter length: ");
int length= input.nextInt();
System.out.print("Enter Width: ");
int width=input.nextInt();
System.out.print("\n");
for(i=0; i<width; i++)
{
for(j=0;j<length;j++)
{
System.out.print("*");
}
System.out.print("\n");
}
}
}






4. Create a program that can accept 5 integers and arrange it in ascending order.

Sample Output:













Code:
import java.util.Scanner;
public class BubbleSortAscending {
public static void main(String[] args) {
int i=0, j=0;
int container[] = new int[5];
int temp;
Scanner input = new Scanner(System.in);
for(i=0; i<5;i++)
{
System.out.println("Enter Element " + (i+1) + ": ");
container[i]= input.nextInt();
}

for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(container[i]>container[j])
{
temp=container[i];
container[i]=container[j];
container[j]=temp;
}
}
}
System.out.println("Ascending order:\n");
for(i=0; i<5;i++)
{
System.out.println(container[i]);
}
}
}
5. Create a program that can accept 5 integers and arrange it in descending order.

Sample Output:











Code:
import java.util.Scanner;
public class BubbleSortDescending {
public static void main(String[] args) {
int i=0, j=0;
int container[] = new int[5];
int temp;
Scanner input = new Scanner(System.in);
for(i=0; i<5;i++)
{
System.out.println("Enter Element " + (i+1) + ": ");
container[i]= input.nextInt();
}

for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(container[i]<container[j])
{
temp=container[i];
container[i]=container[j];
container[j]=temp;
}
}
}
System.out.println("Desscending order:\n");
for(i=0; i<5;i++)
{
System.out.println(container[i]);
}
}
}
6. Create a program that can convert an input decimal number to Roman numeral
Sample Output:

Code:
import java.util.Scanner;
public class DecimaltoRoman {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Input a number to be converted to roman numeral: ");
int number = input.nextInt();
if(number<=3000 && number>0)
{
switch(number/1000)
{
case 1: System.out.print("M"); break;
case 2: System.out.print("MM"); break;
case 3: System.out.print("MMM"); break;
default: break;
}
switch((number%1000)/100)
{
case 1: System.out.print("C"); break;
case 2: System.out.print("CC"); break;
case 3: System.out.print("CCC"); break;
case 4: System.out.print("CD"); break;
case 5: System.out.print("D"); break;
case 6: System.out.print("DC"); break;
case 7: System.out.print("DCC"); break;
case 8: System.out.print("DCCC"); break;
case 9: System.out.print("CM"); break;
default: break;
}
switch(((number%1000)%100)/10)
{
case 1: System.out.print("X"); break;
case 2: System.out.print("XX"); break;
case 3: System.out.print("XXX"); break;
case 4: System.out.print("XL"); break;
case 5: System.out.print("L"); break;
case 6: System.out.print("LX"); break;
case 7: System.out.print("LXX"); break;
case 8: System.out.print("LXXX"); break;
case 9: System.out.print("XC"); break;
default: break;
}
switch(number%10)
{
case 1: System.out.print("I"); break;
case 2: System.out.print("II"); break;
case 3: System.out.print("III"); break;
case 4: System.out.print("IV"); break;
case 5: System.out.print("V"); break;
case 6: System.out.print("VI"); break;
case 7: System.out.print("VII"); break;
case 8: System.out.print("VIII"); break;
case 9: System.out.print("IX"); break;
default: break;
}
}
else System.out.println("Sorry, the program can't convert it");

}
}





















7. Create a simple game of rock-paper-scissors playing versus a computer(AI).
Sample Output:

Code:
import java.util.Random;
import java.util.Scanner;
public class RPS {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Random rand = new Random();
int randnumber = rand.nextInt(1000)%3;
System.out.println("[r]ock - [p]aper - [s]cissor: ");
char choice = input.next().charAt(0);
System.out.println(choice);
if(choice=='r' || choice =='p' || choice =='s')
{
switch(randnumber)
{
case 0://rock
switch(choice){
case 'r' : System.out.println("The computer chose rock, it is a DRAW"); break;
case 's' : System.out.println("The computer chose rock, you LOSE"); break;
case 'p' : System.out.println("The computer chose rock, you WIN"); break;
} break;
case 1://scissors
switch(choice){
case 'r' : System.out.println("The computer chose scissors, you WIN"); break;
case 's' : System.out.println("The computer chose scissors, it is a DRAW");
break;
case 'p' : System.out.println("The computer chose scissors, you LOSE"); break;
} break;
case 2://paper
switch(choice){
case 'r' : System.out.println("The computer chose paper, you LOSE"); break;
case 's' : System.out.println("The computer chose paper, you WIN"); break;
case 'p' : System.out.println("The computer chose paper, it is a DRAW");
break;
} break;
}
}
else System.out.println("You have entered wrong choice");

}
}











8. u