Some Java Programs to practice.(Part 7)

Codynn
6 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

66. Write a program in java to print sum of square of any two ask numbers.

import java.util.Scanner;

class SumOfSquareNumber {
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter the first number");
       int a = sc.nextInt();
       System.out.println("Enter the second number");
       int b = sc.nextInt();
       int sum = 0;
       for (int i = a; i <= b; i++) {
           sum = sum + i * i;
       }
       System.out.println("The sum of square of numbers is " + sum);
   }
}

67. Write a program in java to print all even numbers from 2 to 100 also print its sum.

class Sum{
   public static void main(String[] args) {
               int sum = 0;
               for (int i = 2; i <= 100; i++) {
                   if (i % 2 == 0) {
                       System.out.println(i);
                       sum += i;
                   }
               }
               System.out.println("Sum of even numbers is: " + sum);
           }
       }
      

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

class SumOfCubeOfDigit {
   public static void main(String[] args) {
       int num = 0;
       int sum = 0;
       System.out.println("Enter a number: ");
       num = Integer.parseInt(System.console().readLine());

       while (num != 0) {
           sum += (num % 10) * (num % 10) * (num % 10);
           num = num / 10;
       }
       System.out.println("Sum of cube of digits is: " + sum);
   }
}

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

class SumOfOddDigit {
   public static void main(String[] args) {
       int num = 0;
       int sum = 0;
       System.out.println("Enter a number: ");
       num = Integer.parseInt(System.console().readLine());

       while (num != 0) {
           if (num % 2 != 0) {
               sum += num % 10;
           }
           num = num / 10;
       }
       System.out.println("Sum of odd digits is: " + sum);
   }

   }

70. Write a program in java to enter any 10 numbers and find the sum of numbers.

class SumOfNumbers {
   public static void main(String[] args) {
       int num = 0;
       int sum = 0;
       System.out.println("Enter 10 numbers: ");
       for (int i = 0; i < 10; i++) {
           num = Integer.parseInt(System.console().readLine());
           sum += num;
       }
       System.out.println("Sum of numbers is: " + sum);
   }

   }

71. Write a program in java TO ENTER ANY DIGIT AND PRINT CUBE OF ODD DIGITS.

class SumOfCubeOfOddDigit {
   public static void main(String[] args) {
       int num = 0;
       int sum = 0;
       System.out.println("Enter a number: ");
       num = Integer.parseInt(System.console().readLine());

       while (num != 0) {
           if (num % 2 != 0) {
               System.out.println(num * num * num);
               sum += num * num * num;
           }
           num = num / 10;
       }
       System.out.println("Sum of cube of odd digits is: " + sum);
   }
   }

72. Write a program in java to enter any ten strings and print the longest strings.

class LongestString {
   public static void main(String[] args) {
       String str = "";
       String longestStr = "";
       System.out.println("Enter 10 strings: ");
       for (int i = 0; i < 10; i++) {
           str = System.console().readLine();
           if (str.length() > longestStr.length()) {
               longestStr = str;
           }
       }
       System.out.println("Longest string is: " + longestStr);
   }

   }

73. Write a program in java to generate the series 0,00003,0.0003,0.003,0.03,0.3.

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

       double num = 0.00003;
       for (int i = 0; i < 5; i++) {
           System.out.println(num);
            num= num*10;

       }
   }

   }

74. Write a program in java to ask any number and print the prime factorial of a given number.

class Factorial {
   public static void main(String[] args) {
       int num = 0;
       int factorial = 1;
       System.out.println("Enter a number: ");
       num = Integer.parseInt(System.console().readLine());

       for (int i = 2; i <= num; i++) {
           if (num % i == 0) {
               factorial *= i;
           }
       }
       System.out.println("Factorial of " + num + " is: " + factorial);
   }



   }

75. Write a program to enter any 10 numbers and display the smallest one.

class SmallestNumber {
   public static void main(String[] args) {
       int num = 0;
       int smallestNum = 0;
       System.out.println("Enter 10 numbers: ");
       for (int i = 0; i < 10; i++) {
           num = Integer.parseInt(System.console().readLine());
           if (num < smallestNum || smallestNum == 0) {
               smallestNum = num;
           }
       }
       System.out.println("Smallest number is: " + smallestNum);
   }

   }

76. Write a program in java to ask any number and check whether the given number is palindrome or not.


class Palindrome {
       public static void main(String[] args) {
       int num = 0;
       int reverseNum = 0;
       System.out.println("Enter a number: ");
       num = Integer.parseInt(System.console().readLine());

       while (num != 0) {
           reverseNum = (reverseNum * 10) + (num % 10);
           num = num / 10;
       }
       if (reverseNum == num) {
           System.out.println("Number is palindrome");
       } else {
           System.out.println("Number is not palindrome");
       }
   }

}
Share This Article
Leave a comment

Leave a Reply

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