Some Java Programs to practice.(Part 10)

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

96. Write a program in java TO CHECK WHETHER THE ASK NUMBER IS PERFECT NUMBER OR NOT.

import java.util.*; 
    
   class PerfectNumber{ 
   public static void main(String[] args) { 
   int num = 0; 
   System.out.println("Enter number"); 
   Scanner MyScanner = new Scanner(System.in); 
   num = MyScanner.nextInt(); 
   int sum = 0; 
   for (int i = 1; i   < num; i++) { 
   if (num % i == 0) { 
   sum = sum + i; 
   } 
   } 
   if (sum == num) { 
   System.out.println("Number is perfect"); 
   } else { 
   System.out.println("Number is not perfect"); 
   } 
   } 
    
   } 

97. Write a program in java to print the sum of the numbers between 2 to 50.

class Sum{ 
   public static void main(String[] args) { 
   int sum = 0; 
   for (int i = 2; i   <= 50; i++) { 
   sum = sum + i; 
   } 
   System.out.println("Sum is " + sum); 
   } 
    
    
   } 

98. Write a program in java to generate the following numeric pattern 1,12,123,1234,12345.


   <string name="a98">  class Pattern{ 
   public static void main(String[] args) { 
   for (int i = 1; i   <= 5; i++) { 
   for (int j = 1; j   <= i; j++) { 
   System.out.print(j); 
   } 
   System.out.println(); 
   } 
   } 
    
    
    
   } 
    
    

99. Write a program in java to check whether the supplied character is alphabet or not.

import java.util.*; 
    
   class Alphabet{ 
   public static void main(String[] args) { 
   char ch = \' \'; 
   System.out.println("Enter character"); 
   Scanner MyScanner = new Scanner(System.in); 
   ch = MyScanner.next().charAt(0); 
   if (ch >= \'a\'  & & ch   <= \'z\') { 
   System.out.println("Character is alphabet"); 
   } else if (ch >= \'A\'  & & ch   <= \'Z\') { 
   System.out.println("Character is alphabet"); 
   } else { 
   System.out.println("Character is not alphabet"); 
   } 
   } 
    
   } 

100. Write a program to input any number and check whether the given no. is positive, negative or zero.

import java.util.*; 
    
   class Number{ 
   public static void main(String[] args) { 
   int num = 0; 
   System.out.println("Enter number"); 
   Scanner MyScanner = new Scanner(System.in); 
   num = MyScanner.nextInt(); 
   if (num > 0) { 
   System.out.println("Number is positive"); 
   } else if (num   < 0) { 
   System.out.println("Number is negative"); 
   } else { 
   System.out.println("Number is zero"); 
   } 
   } 
   } 
    
    

101. Write a program in java to generate the following series 55555,4444,333,22,1.

import java.util.Scanner; 
    
   class Pattern{ 
   public static void main(String[] args) 
   { 
   for(int r=5;r>=1;r--) 
   { 
   for(int c=1;c  <=r;c++) 
   { 
   System.out.print(r); 
   } 
   System.out.print(" "); 
   } 
   } 
   } 

102. Write a program in java to print all odd numbers from 1 to 100 in descending order.

import java.util.*; 
    
   class DescendingOrder{ 
   public static void main(String[] args) { 
   for (int i = 100; i >= 1; i--) { 
   if (i % 2 != 0) { 
   System.out.println(i); 
   } 
   } 
   } 
   public static void main(String[] args) { 
   for (int i = 100; i >= 1; i--) { 
   if (i % 2 != 0) { 
   System.out.println(i); 
   } 
   } 
   } 
    
   } 

103. Write a program to enter any two numbers and display the smaller one.

import java.util.*; 
    
   class Smallernumber{ 
   public static void main(String[] args) { 
   int num1 = 0; 
   int num2 = 0; 
   System.out.println("Enter first number"); 
   Scanner MyScanner = new Scanner(System.in); 
   num1 = MyScanner.nextInt(); 
   System.out.println("Enter second number"); 
   num2 = MyScanner.nextInt(); 
   if (num1 > num2) { 
   System.out.println("Smaller number is " + num2); 
   } else if (num1   < num2) { 
   System.out.println("Smaller number is " + num1); 
   } else { 
   System.out.println("Both numbers are equal"); 
   } 
   } 
    
    
   } 

104. Write a program in java to print numbers 1,3,5……..30.

import java.util.*; 
    
   class Series{ 
   public static void main(String[] args) { 
   for (int i = 1; i   <= 30; i++) { 
   if (i % 2 != 0) { 
   System.out.println(i); 
   } 
   } 
   } 
    
    
   } 
    
    
    
    

105. Write a program to input the age of a person and find out whether the person is eligible to vote or not.

import java.util.*; 
    
   class Vote{ 
   public static void main(String[] args) { 
   int age = 0; 
   System.out.println("Enter age"); 
   Scanner MyScanner = new Scanner(System.in); 
   age = MyScanner.nextInt(); 
   if (age >= 18) { 
   System.out.println("Person is eligible to vote"); 
   } else { 
   System.out.println("Person is not eligible to vote"); 
   } 
   } 
   } 
    
Share This Article
Leave a comment

Leave a Reply

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