You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Algorithm that finding perfect square numbers between of 10, 1000
publicstaticvoidmain(String[] args) {
for ( inti = 10 ; i <= 1000; i++){
for ( intj = 4; j < 100; j++){
intsqr = j*j;
if ( sqr == i){
System.out.println(i);
}
}
}
}
Algorithm to find whether the entered number is a power of 5
publicstaticvoidmain(String[] args) {
Scanners = newScanner (System.in);
intnumber = s.nextInt();
if ( number % 5 == 0){
System.out.println(number + " is power of 5");
}
else {
System.out.println(number + " isn't power of 5");
}
}
Algorithm that check friendly number to entered two numbers.
publicstaticvoidmain(String[] args) {
// friendly number = If two numbers are equal to the sum of their divisors excluding each other, these numbers are called friend numbers.Scanners = newScanner (System.in);
intx = s.nextInt() , y = s.nextInt();
intsumX =0, sumY =0;
for ( inti = 1; i < x; i++){
if ( x % i == 0){
sumX += i;
}
}
for ( inti = 1; i < y; i++){
if ( y % i == 0){
sumY += i;
}
}
if ( sumX == y && sumY == x){
System.out.println("They're friendly number.");
}
else {
System.out.println("They aren't friendly number.");
}
}
Algortihm that check abundant number or deficient number entered from keyboard.
publicstaticvoidmain(String[] args) {
// abundant number = the sum of its multipliers is bigger than itself// deficient number = the sum of its multipliers is less than itselfScanners = newScanner (System.in);
intsum =0;
intnumber = s.nextInt();
for ( inti =1 ; i < number; i++){
if( number % i == 0){
sum += i;
}
}
if ( sum > number){
System.out.println(number + " is a abundant number.");
}
elseif ( sum < number){
System.out.println(number + " is a deficient number.");
}
else {
System.out.println(number +"is a normal number.");
}
}
Algorithm that 6 steps for finding random number of between 1 - 63 integer numbers.
publicstaticvoidmain(String[] args) {
Scanners = newScanner ( System.in);
intnumber = s.nextInt();
intfloor = 1 , ceiling = 63;
intkeep;
if ( number > 62 || number < 2 ){
System.out.println("Please select number between of 1 and 63");
System.exit(0);
}
for ( inti =1; i <= 6; i++){
keep =(ceiling + floor)/2;
System.out.println("keep =" + keep);
if ( keep == number ){
System.out.println("I finded number = " + number);
System.exit(0);
}
elseif ( number > keep ){
floor = keep;
System.out.println("Bigger than " + keep);
}
else {
ceiling = keep;
System.out.println("Less than");
}
}
}
Algrotihm that decimal convert to binary number.
publicstaticvoidmain(String[] args) {
Scanners = newScanner(System.in);
intnumber = s.nextInt();
intkeep = number;
intcounter = 0 ;
while ( keep > 0 ){
keep /= 2;
counter++;
}
intbinary [] = newint [counter];
for ( inti = counter-1; i >= 0; i--){
keep = number % 2;
number /= 2;
if ( keep == 0){
binary[i] = 0;
}
else {
binary[i] = 1;
}
}
for ( inti =0 ; i < binary.length; i++){
System.out.print(binary[i]);
}
}
Algortihm that binary number convert to decimal number
publicstaticvoidmain(String[] args) {
Scanners = newScanner ( System.in);
doublecounter =0;
Stringbinary = s.next();
doublepwr;
intsum =0;
for ( inti = binary.length()-1 ; i >= 0; i--){
charno = binary.charAt(i);
if ( no == '1' ){
pwr = Math.pow(2, counter);
sum += pwr;
}
else {
}
counter++;
}
System.out.println(sum);
}
Algorithm that finding t time of an airplane accelerates smoothly for 15 minutes and its speed becomes 480 km/h. Then at constant speed for 20 min. It goes and slows down smoothly for 15 minutes, and its speed becomes zero.
publicstaticvoidmain(String[] args) {
Scanners = newScanner ( System.in );
intt = s.nextInt();
intplane = 0;
intlist[] = newint [51];
list[0] = 0;
for (inti = 1 ; i <= 15; i++){
plane += 32;
list[i] = plane;
}
for ( inti = 16; i <= 35; i++){
plane += 0;
list[i] = plane;
}
for ( inti= 36; i < 50; i++){
plane -= 32;
list[i] = plane;
}
if ( t < 0 || t > 50){
System.out.println("Plane is standing");
}
else {
System.out.println("Plane's speed = " + list[t]);
}
}
Algorithm that check number original number or not of 4 digit numbers
publicstaticvoidmain(String[] args) {
// original number = square of sum of its digits first two digits and last two digits equals its numberintfirstDigits , lastDigits, keep , sum;
doublepwr;
for ( inti =1000 ; i <10000; i++){
keep = i ;
firstDigits = i /100;
keep -= (firstDigits*100);
lastDigits = keep;
sum = firstDigits + lastDigits;
pwr = Math.pow(sum , 2);
if ( pwr == i){
System.out.println(i);
}
}
}
Algrorithm that check smith number or not of 4 digit numbers
publicstaticvoidmain(String[] args) {
// smith number = If the sum of the digits of a non-prime integer greater than 1 is equal to the sum of the digits of all prime numbers in that spelling when the number is written by factoring the prime.intthousands , hundreds, tens, ones;
intkeep ;
intsum = 0 , sum2 = 0;
for ( inti = 1000; i < 10000; i++){
keep = i;
thousands = i / 1000;
i -= thousands*1000;
hundreds = i / 100;
i -= hundreds*100;
tens = i / 10;
i -= tens*10;
ones = i/1;
sum += (thousands + hundreds + tens + ones);
i = keep;
for( intj = 2 ; j < i ; ){
if ( keep % j == 0){
sum2 += j;
keep /= j;
}
else {
j++;
}
}
if ( sum == sum2){
System.out.println(i);
}
sum = 0;
sum2 = 0;
}
}
Algorithm that finding biggest of number's digits.
publicstaticvoidmain(String[] args) {
Scanners = newScanner (System.in);
intnumber = s.nextInt();
System.out.println("Biggest digit of number = " + biggest(number));
}
publicstaticintbiggest ( intx){
intdigit = 0;
intsum = 0;
intkeep = x;
while ( x > 0){
digit++;
x /= 10;
}
x = keep;
intlist [] = newint [digit];
intbig = list[0];
for ( inti =0 ; i < digit; i++){
list[i] = x % 10;
x /= 10;
if ( big < list[i]){
big = list[i];
}
}
returnbig;
}
Algorithm that calculate series of 1+ x / 1! + x^2 / 2! + x^3 / 3! ... n and x entering from keyboard
publicstaticvoidmain(String[] args) {
Scanners = newScanner ( System.in);
System.out.println("Please enter the x value");
doublenumber1 = s.nextInt();
System.out.println("Please enter the n value");
doublenumber2 = s.nextInt();
System.out.println( calculate( number1 , number2 ));
}
publicstaticdoublecalculate ( doublex , doublen){
doublefactorial = 1 ;
doublesum = 1;
doublepwr, keep;
for ( inti = 1; i <= n ; i++){
pwr = Math.pow( x , i);
for ( intf = 1; f <= i; f++){
factorial *= f;
}
keep = pwr / factorial;
sum += keep;
factorial = 1 ;
}
returnsum;
}
Algorithm that calculate series of 1-1 / 3+1 / 5-1 / 7+1 / 9-1 / .... / n +- 1 n enter from keyboard
publicstaticvoidmain(String[] args) {
Scanners = newScanner (System.in);
System.out.println("Please enter the n value");
intnumber = s.nextInt();
if ( number % 2 == 0){
System.out.println("Please enter the odd number. ");
System.exit(1);
}
System.out.println( calculate (number));
}
publicstaticdoublecalculate ( intx){
doublesum =0 ;
for ( inti = 1; i <= x; i += 4){
sum += (i -1);
}
for ( inti = 3; i <= x; i += 4){
sum += (i+1);
}
returnsum;
}
Algorithm that calculate series of 1 - x^2 / 2! + x^4 / 4! - x^6 / 6! + x^8 / 8! / ... +- x^n / n! n enter form keyboard
// this series calcutaing of cos(x)publicstaticvoidmain(String[] args) {
Scanners = newScanner (System.in);
System.out.println("Please enter x value");
doublenumber = s.nextDouble();
System.out.println("Please enter n value");
doublenumber2 = s.nextDouble();
System.out.println( calculate (number , number2));
}
publicstaticdoublecalculate ( doublex, doublen){
doublesum = 1;
doublefactorial = 1;
doublepwr, keep;
for ( inti = 2; i <= n; i += 4 ){
pwr = Math.pow( x , i);
for ( intf = 1; f <= n; f++){
factorial *= f;
}
keep = pwr/factorial;
factorial =1;
sum -= keep;
}
for ( inti = 4; i <= n; i += 4){
pwr = Math.pow( x, i );
for ( intf= 1; f <= n; f++){
factorial *= f;
}
keep = pwr/factorial;
factorial =1;
sum += keep;
}
returnsum;
Algorithm that calclate ∑ (1/i! + i/ (n-i)!. n enter from keyboard.
publicstaticvoidmain(String[] args) {
Scanners = newScanner (System.in);
doublen = s.nextDouble();
doublesum =0;
doublekeep;
doublefactorial =1 ;
doublefactorial2 = 1;
for ( inti = 1; i <= n; i++){
for ( intf = 1; f <= i ; f++){
factorial *= f;
}
for ( intf = 1; f < (n-i); f++){
factorial2 *= f;
}
keep = (1/factorial) + ( i / factorial2 );
sum += keep;
factorial =1;
factorial2 = 1;
}
System.out.println(sum);
}
Algorithm that writing backwards of a word.
publicstaticvoidmain(String[] args) {
Scanners = newScanner (System.in);
Stringword = s.next();
Stringturn = writeBackwards(word);
System.out.println( turn);
}
publicstaticStringwriteBackwards ( Stringx){
Stringy = "" ;
for ( inti =x.length()-1 ; i >= 0; i--){
y += x.charAt(i);
}
returny;
}
There is an array with N elements. Algorithm that places the numbers entered from the keyboard into the array, one from the beginning and one from the end.
publicstaticvoidmain(String[] args) {
Scanners = newScanner (System.in);
System.out.println("Please enter the array's length");
intx = s.nextInt();
String [] list = newString[x];
intlng = list.length;
for ( inti = 0 , j = list.length-1; i < lng/2 ; i++, j--){
list[i] = s.next();
list[j] = s.next();
}
if ( lng % 2 == 1){
System.out.println("Please enter middle and last value");
list[lng/2] = s.next();
}
for ( inti =0; i < list.length; i++){
System.out.print(list[i] + " ");
}
}
Algorithm that gives the following output
/* computer omputerc mputerco . . . computer*/publicstaticvoidmain(String[] args) {
Stringword = "computer";
charc [] = convert(word);
swiftAndWrite(c);
}
publicstaticvoidswiftAndWrite(charc[]){
for ( inti =0 ; i < c.length;i++){
System.out.print(c[i] + " ");
}
System.out.println(" ");
charkeep = c[0];
for ( intj =0 ; j < c.length; j++){
keep = c[0];
for ( inti = 0; i < c.length-1; i++){
c[i] = c[i+1];
}
c[c.length-1] = keep;
for ( inti =0 ; i < c.length; i++){
System.out.print(c[i] + " ");
}
System.out.println("");
}
}
publicstaticchar[] convert (Stringx){
charc [] = newchar [x.length()];
for ( inti =0 ; i < x.length(); i++){
c[i] = x.charAt(i);
}
returnc;
}
Algotihm that sorts an int array from largest to smallest.
publicstaticint[] input(intx[]){
Scanners = newScanner (System.in);
for ( inti = 0; i < x.length; i++){
x[i] = s.nextInt();
}
toSort(x);
returnx;
}
publicstaticvoidtoSort(intx[]){
intkeep;
for ( inti =0 ; i < x.length;i++){
for ( intj = i+1; j < x.length; j++){
if ( x[j] > x[i]){
keep = x[i];
x[i] = x[j];
x[j] = keep;
}
}
}
output(x);
}
publicstaticvoidoutput (intx[]){
for ( inti = 0; i < x.length; i++){
System.out.print(x[i] + " ");
}
}
publicstaticvoidmain(String[] args) {
int [] list = newint[10];
list = input(list);
}
Algortihm that sum of two binary number
publicstaticdouble[] convertDecimal ( Stringx, Stringy){
intsumX = 0, sumY = 0;
doubledecimal[] = newdouble[2];
for ( inti =0, j = x.length()-1; i < x.length(); i++, j--){
if ( x.charAt(i) == '1' ){
sumX += Math.pow(2 , j);
}
}
for ( inti =0 , j = y.length()-1; i < y.length(); i++, j--){
if ( y.charAt(i) == '1'){
sumY += Math.pow(2 , j);
}
}
decimal[0] = sumX;
decimal[1] = sumY;
convertBinary(sumX+sumY);
returndecimal;
}
publicstaticvoidconvertBinary(intx){
intcounter = 0;
intkeep = x;
while ( keep > 0 ){
keep /= 2;
counter++;
}
charbnry [] = newchar [counter];
keep = x;
for ( inti =0; i < bnry.length; i++){
keep = x % 2;
x /= 2;
if ( keep == 0 ){
bnry[i] = '0';
}
else {
bnry[i] = '1';
}
}
output(bnry);
}
publicstaticvoidoutput( charx[]){
for ( inti =x.length-1; i >=0; i--){
System.out.print(x[i] + " ");
}
}
publicstaticvoidmain(String[] args) {
StringtwentyThree = "10111";
Stringnine = "1001";
convertDecimal(twentyThree , nine);
}
Algorithm that shifts it back to the beginning by removing the ones other than the first written number of the repeated numbers in a 7-element number array with the element values given.
publicstaticvoidmain(String[] args) {
intlist [] = {10 , 20, 30, 40 , 30 , 20 , 50};
swift(list);
}
publicstaticint[] swift ( intx[] ){
intkeep , counter=0;
for ( inti = 0; i < x.length; i++){
for ( intj = i+1; j < x.length; j++){
if ( x[i] == x[j]){
keep = x[j];
for (ints = j; s >counter; s--){
x[s] = x[s-1];
}
x[counter] = keep;
counter++;
}
}
}
output(x);
returnx;
}
publicstaticvoidoutput ( intx[]){
for ( inti =0; i < x.length; i++ ){
System.out.println(x[i]);
}
}
Algorithm that finding how much one digit, how much two digit, how much three digit of number array
publicstaticvoidmain(String[] args) {
intlist [] = { 10 , 100, 1, 200, 20};
find(list);
}
publicstaticint [] find ( intx[]){
intcounter =0, keep;
intdigits [] = newint [x.length];
for ( inti = 0; i < x.length; i++){
keep = x[i];
while ( x[i] > 0){
x[i] /= 10;
counter++;
}
digits[i] = counter;
counter = 0;
x[i] = keep;
}
output(digits);
returndigits;
}
publicstaticvoidoutput (intx[]){
for ( inti =0; i < x.length; i++){
System.out.println(" Digit of element " + (i+1) + " = " + x[i] );
}
}
Half of the sum of the 1st and 2nd elements of a 10-element array is the first element of another array, and half of the sum of the 3rd and 4th elements is the 2nd element.
publicstaticvoidmain(String[] args) {
intlist [] = newint [10];
elementsOfList(list);
}
publicstaticint[] elementsOfList (intx[]){
Scanners = newScanner(System.in);
for ( inti =0; i < x.length; i++ ){
x[i] = s.nextInt();
}
sumAndAssign(x);
returnx;
}
publicstaticvoidsumAndAssign ( intx[]){
intsum =0;
intlastArray [] = newint[x.length/2];
for ( inti =0 , j = 1, c =0; i < x.length; i += 2, j +=2, c++ ){
sum += (x[i] + x[j])/2;
lastArray[c] = sum;
sum = 0;
}
output(lastArray);
}
publicstaticvoidoutput ( intx[]){
for ( inti =0; i < x.length; i++){
System.out.println(x[i]);
}
}
Algorithm that gives the transpose of a square matrix of type [2x2].
Write method that returns check whether prime number or not that comes to it as a parameter
publicstaticbooleancheck ( intx){
for ( inti = 2 ; i < x; i++){
if ( x % i == 0){
returnfalse;
}
}
returntrue;
}
publicstaticvoidmain(String[] args) {
Scanners = newScanner(System.in);
intnumber = s.nextInt();
booleancondition = check(number);
if ( condition == true ){
System.out.println("Number is a prime number");
}
else {
System.out.println("Number isn't a prime number");
}
}
Write method that return calculate average of odd numbers of a integer array that comes to it as a parameter
publicstaticdoubleaverageOdd ( int []x) {
doubleavg , counter = 0 , sum =0;
for ( inti =0; i < x.length; i++){
if ( x[i] % 2 != 0){
sum += x[i];
counter++;
}
}
if ( counter == 0){
return0;
}
else {
avg = sum/counter;
}
returnavg;
}
publicstaticvoidmain(String[] args) {
intlist[] = { 1,2,3,4,5,6,7,8,9,10};
System.out.println( averageOdd(list));
}
Write method that returns finding longest word and indexs this of a double dimension array that comes to it as a parameter
100 strings are entered from the keyboard and these strings are combined. 20 words with 5 letters are randomly generated from these 100 strings. algorithm that prints the equal of these 20 words to the screen.
publicstaticvoidmain(String[] args) {
StringlongString = "";
longString = elements(longString);
elementsOfList(longString);
}
publicstaticStringelements(Stringx) {
Scanners = newScanner(System.in);
for (inti = 0; i < 100; i++) {
x += s.next();
}
returnx;
}
publicstaticvoidelementsOfList(Stringx) {
Randomr = newRandom();
Stringlist[] = newString[20];
Stringkeep = "";
intrnd;
for (inti = 0; i < list.length; i++) {
rnd = r.nextInt(x.length() - 4);
for (intj = rnd; j < rnd + 5; j++) {
keep += x.charAt(j);
}
list[i] = keep;
keep = "";
}
allPrint(list);
check(list);
}
publicstaticvoidallPrint(String[] x) {
// that method generated for checking list.for (inti = 0; i < x.length; i++) {
System.out.println(x[i]);
}
}
publicstaticvoidcheck(String[] x) {
for (inti = 0; i < x.length; i++) {
for (intj = i + 1; j < x.length; j++) {
if (x[i].equals(x[j])) {
System.out.println("same elements of random list =" + x[i]);
}
}
}
}