Some Java Programs to practice.(Part 5)

Codynn
5 Min Read

I’ve included a list of some of the Java programs below. You may also view all of the Java programs on our mobile app. I strongly advise you to use our mobile app for a better experience.

Please click the link to get our app–> https://play.google.com/store/apps/details?id=com.allbachelor.javaapp&hl=en&gl=US

46. Write a program in java to convert decimal number to octal number.

import java.util.*;

class DecimalToOctal {
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter the decimal number");
       int decimal = sc.nextInt();
       int octal = 0;
       int remainder = 0;
       while (decimal > 0) {
           remainder = decimal % 8;
           decimal = decimal / 8;
           octal = octal * 10 + remainder;
       }
       System.out.println("Octal number is " + octal);
   }
}

47. Write a program in java to ask number and find sum of cube of odd digits.


import java.util.*;
class SumOfCubeOfOddDigit {
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter the number");
       int number = sc.nextInt();
       int sum = 0;
       while (number > 0) {
           int remainder = number % 10;
           number = number / 10;
           if (remainder % 2 != 0) {
               sum = sum + remainder * remainder * remainder;
           }
       }
       System.out.println("Sum of cube of odd digits is " + sum);
   }
}

48. Write a program in java using Sub procedure to print 1, 10, 101, 1010, 10101.

class Pattern {
       public static void main(String[] args) {
          int n=5;
           for (int i = 1; i <= n; i++) {
               for (int j = 1; j <= i; j++) {
                   if (j % 2 == 1) {
                       System.out.print("1 ");
                   } else {
                       System.out.print("0 ");
                   }
               }
               System.out.print(" ");
           }
       }

   }

49. Write a program in java to find the sum of all natural numbers from 1 to 100.

import java.util.*;

class NaturalNumbers{
           public static void main(String[] args) {
       int sum = 0;
       for (int i = 1; i <= 100; i++) {
           sum = sum + i;
       }
       System.out.println("Sum of all natural numbers from 1 to 100 is " + sum);
   }



}
 

50. Write a program in java to generate the following series 1,121,12321,1234321,123454321.

class Program {
   public static void main(String[] args) {

       int n = 5;

       for (int i = 1; i <= n; i++) {
           for (int j = i; j <= n - 1; j++) {
               System.out.print(" ");
           }
           for (int j = 1; j <= i; j++) {
               System.out.print(j);
           }
           for (int j = i - 1; j >= 1; j--) {
               System.out.print(j);
           }
           System.out.print(" ");
       }
   }
}

51. Write a program in java to print sum of cube of any two ask numbers.

import java.util.*;

class AskNumber{
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter the number");
       int number = sc.nextInt();
       int sum = 0;
       while (number > 0) {
           int remainder = number % 10;
           number = number / 10;
           sum = sum + remainder * remainder * remainder;
       }
       System.out.println("Sum of cube of any two ask numbers is " + sum);
   }







}

52. Write a program in java to generate the series .1,0.01,0.001,0.0001,0.00001.

class Series {
  public static void main(String[] args) {

      double n = 1;

      for (double i = 1; i <= 5; i++) {
              System.out.print(n/Math.pow(10,i)+ " ");
          }
      }
  }


53. Write a program in java using Sub procedure to print 50,75,100,125,150.

class Series {
  public static void main(String[] args) {

      int n = 50;

      for (int i = 1; i <= 5; i++) {
          System.out.print(n+ " ");
          n=n + 25;

          }
      }
  }

54.Write a program in java to ask string and print the ask string in alternate character.

import java.util.*;

class AlternateCharacter{
           public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter the string");
       String string = sc.nextLine();
       for (int i = 0; i < string.length(); i++) {
           if (i % 2 == 0) {
               System.out.print(string.charAt(i));
           }
       }
   }

}

55. Write a program in java to print cube of all numbers from 1 to 50.

import java.util.*;

class Cube{
       public static void main(String[] args) {
       for (int i = 1; i <= 50; i++) {
           System.out.println(i * i * i);
       }
   }

}
Share This Article
Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *