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

Computer Project

The document describes an algorithm for sorting numbers in ascending order using bubble sort. It includes 4 steps: 1) Define an array with 10 elements for input numbers, 2) Use a loop to input numbers from the user, 3) Use nested loops to compare adjacent elements and swap them if out of order, 4) Repeat step 3 until all elements are sorted. The full Java code implementing this algorithm is also included.

Uploaded by

Hanishadowx
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Computer Project

The document describes an algorithm for sorting numbers in ascending order using bubble sort. It includes 4 steps: 1) Define an array with 10 elements for input numbers, 2) Use a loop to input numbers from the user, 3) Use nested loops to compare adjacent elements and swap them if out of order, 4) Repeat step 3 until all elements are sorted. The full Java code implementing this algorithm is also included.

Uploaded by

Hanishadowx
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

PROGRAM 6

ALGORITHM
Step 1: take variables and define an array with 10 elements in it.

Step 2: make the user input the elements in the array by a loop with variable 'i'.

Step 3:take another loop of variable 'j'.

Step 4:take an if statement to check if one number is less than the other. if yes then swap both the numbers, and in this way the loop keeps repeating
until all elements are sorted.

//program 6

import java.util.*;

class BubbleSort

{ public static void main()

Scanner sc=new Scanner(System.in);

int i,j,t;

int num[]=new int[10];

System.out.println("enter 10 different numbers in the array ");

for(i=0;i<10;i++)

{ num[i]=sc.nextInt();

} for(i=0;i<9;i++)

{ for(j=0;j<(9-i);j++)

if(num[j]<num[j+1])

t=num[j];

num[j]=num[j+1];

num[j+1]=t;

System.out.println("the elements sorted in array are num["+j+"]:"+num[j]);

} }} }
PROGRAM 7
ALGORITHM
Step 1: take variables and define an array with 10 elements in it.

Step 2: make the user input the elements in the array by a loop with variable 'i'.

Step 3:take another loop of variable 'j'.

Step 4:take an if statement to check if one number is less than the other. if yes then swap both the numbers, and in this way the loop keeps repeating
until all elements are sorted.

//program 7

import java.util.*;

class SelectionSort

public static void main() {

int i,j,t,min;

int num[]=new int[10];

Scanner sc=new Scanner (System.in);

System.out.println("enter 10 different numbers in the array");

for(i=0;i<10;i++){

num[i]=sc.nextInt();

for(i=0;i<9;i++){

min=i;

for(j=i+1;j<10;j++){

if(num[j]<num[min])

min=j; }

t=num[i]

num[i]=num[min];

num[min]=t;

System.out.println("Numbers arranged in ascending order are : ");

for(i=0;i<10;i++)

System.out.println(num[i]);

} }

2
PROGRAM 8
ALGORITHM
Step 1: take variables n and y and define them with day number as ‘n’ and year as ‘y’

Step 2: take an if statement where if ‘y’ is a leap year then,February should have 29 days, else 28

Step 3:take another if statement inside this and check where the day lies and between which month with those if staements

Step 4: print the out put along with the day ,month and year.

import java.util.*;

class DatewithNumberOfDays

public static void main()

Scanner sc=new Scanner(System.in);

int n,y;

System.out.println("enter the year :");

y=sc.nextInt();

System.out.println("enter the day number :");

n=sc.nextInt();

if(y%4==0)

if(n<=31)

{//jan

System.out.println("january "+n);

if(n<=31+29)//feb

System.out.println("feb "+(n-31));

if(n<=31+29+31)//march

System.out.println("march "+(n-(31+29)));

3
}

if(n<=31+29+31+31)//april

System.out.println("april "+(n-(31+29+31)));

if(n<=31+29+31+31+31)//may

System.out.println("may "+(n-(31+29+31+31)));

if(n<=31+29+31+31+31+31)//june

System.out.println("june "+(n-(31+29+31+31+31)));

if(n<=31+29+31+31+31+31+31)//july

System.out.println("july "+(n-(31+29+31+31+31+31)));

if(n<=31+29+31+31+31+31+31+31)//august

System.out.println("aug "+(n-(31+29+31+31+31+31+31)));

if(n<=31+29+31+31+31+31+31+31+31)//september

System.out.println("sept "+(n-(31+29+31+31+31+31+31+31)));

if(n<=31+29+31+31+31+31+31+31+31+31)//october

System.out.println("oct"+(n-(31+29+31+31+31+31+31+31+31)));

if(n<=31+29+31+31+31+31+31+31+31+31+31)//nov

System.out.println("nov "+(n-(31+29+31+31+31+31+31+31+31)));

4
if(n<=31+29+31+31+31+31+31+31+31+31+31+31)//dec

System.out.println("dec "+(n-(31+29+31+31+31+31+31+31+31+31)));

else if(y%4!=0)

if(n<=31)//jan

System.out.println("january "+n);

if(n<=31+28)//feb

System.out.println("february "+(n-31));

if(n<=31+28+31)//march

System.out.println("march "+(n-(31+28)));

if(n<=31+28+31+31)//april

System.out.println("april "+(n-(31+28+31)));

if(n<=31+28+31+31+31)//may

System.out.println("may "+(n-(31+28+31+31)));

if(n<=31+28+31+31+31+31)//june

System.out.println("june "+(n-(31+28+31+31+31)));

if(n<=31+28+31+31+31+31+31)//july

System.out.println("july "+(n-(31+28+31+31+31+31)));

if(n<=31+28+31+31+31+31+31+31)//august

System.out.println("augus "+(n-(31+28+31+31+31+31+31)));

if(n<=31+28+31+31+31+31+31+31+31)//september

System.out.println("septemebr "+(n-(31+28+31+31+31+31+31+31)));

if(n<=31+28+31+31+31+31+31+31+31+31)//october

System.out.println("october "+(n-(31+28+31+31+31+31+31+31+31)));

if(n<=31+28+31+31+31+31+31+31+31+31+31)//nov

System.out.println("november "+(n-(31+28+31+31+31+31+31+31+31)));

if(n<=31+28+31+31+31+31+31+31+31+31+31+31)//dec

System.out.println("december "+(n-(31+28+31+31+31+31+31+31+31+31))); } }}

5
PROGRAM 9
import java.util.*;

class WordToNumber

public static void main()

String s, enstr = "";

int n;

char ch;

Scanner sc = new Scanner(System.in);

System.out.print("Enter the Text to Encrypt : ");

s = sc.nextLine();

System.out.print("Enter Number of letters to shift: ");

n = sc.nextInt();

for(int i = 0; i < s.length(); ++i)

ch = s.charAt(i);

if(ch >= 'a' && ch <= 'z')

ch = (char)(ch + n);

if(ch > 'z')

ch = (char)(ch - 'z' + 'a' - 1);

enstr += ch;

else if(ch >= 'A' && ch <= 'Z')

ch = (char)(ch + n);

if(ch > 'Z')

ch = (char)(ch - 'Z' + 'A' - 1);

6
enstr += ch;

else

enstr += ch;

System.out.println("The Original Text is " + s);

System.out.println("The Encrypted Text is " + enstr);

You might also like