Easy understandable codes are without sample input & output, so please understand the basic logic's and work it out. some codes are given with sample input and output. Next-level to basic programs are available in PART-II module.1) Java Program to Check if a Given Integer is Positive or Negative.
import java.util.*;public class Hello{ public static void main(String[] args) { int n; Scanner s = new Scanner(System.in); System.out.print("Enter the number you want to check:"); n = s.nextInt(); if(n > 0) { System.out.println("Positive"); } else if(n < 0) { System.out.println("Negative"); } else { System.out.println("neither Positive nor Negative "); } }}2) Java Program to Check if a Given Integer is Odd or Even.
import java.util.*;public class Hello { public static void main(String[] args) { int n; Scanner s = new Scanner(System.in); System.out.print("Enter the number you want to check:"); n = s.nextInt(); if(n % 2 == 0) { System.out.println("Even "); } else { System.out.println("Odd "); } }}3) Java Program to Find the Biggest of 3 Numbers.import java.util.*;public class Hello { public static void main(String[] args) { int x, y, z; Scanner s = new Scanner(System.in); System.out.print("Enter the first number:"); x = s.nextInt(); System.out.print("Enter the second number:"); y = s.nextInt(); System.out.print("Enter the third number:"); z = s.nextInt(); if(x > y && x > z) { System.out.println("Largest number is:"+x); } else if(y > z) { System.out.println("Largest number is:"+y); } else { System.out.println("Largest number is:"+z); } }}4) Java Program to Calculate the Sum of Odd & Even Numbers.
import java.util.*;public class Hello { public static void main(String[] args) { int n, sumEven = 0, sumOdd = 0; Scanner s = new Scanner(System.in); System.out.print("Enter the number of elements in array:"); n = s.nextInt(); int[] a = new int[n]; //to declare an array System.out.println("Enter the elements of the array:"); for(int i = 0; i < n; i++) { a[i] = s.nextInt(); } for(int i = 0; i < n; i++) { if(a[i] % 2 == 0) { sumEven = sumEven + a[i]; } else { sumOdd = sumOdd + a[i]; } } System.out.println("Sum of Even Numbers:"+sumEven); System.out.println("Sum of Odd Numbers:"+sumOdd); }}5) Java Program to Read Two Integers M and N & Swap their Values.import java.util.*;public class Swap_Integers { public static void main(String args[]) { int m, n, temp; Scanner s = new Scanner(System.in); System.out.print("Enter the first number:"); m = s.nextInt(); System.out.print("Enter the second number:"); n = s.nextInt(); temp = m; m = n; n = temp; System.out.println("After Swapping"); System.out.println("First number:"+m); System.out.println("Second number:"+n); }}6) Java Program to Reverse a Given Number. import java.util.*;public class Reverse_Number { public static void main(String args[]) { int m, n, sum = 0; Scanner s = new Scanner(System.in); System.out.print("Enter the number:"); m = s.nextInt(); System.out.println("Reverse of the number is:"); while(m != 0) { n = m % 10; System.out.println(n); m = m / 10; } }}7) Java Program to Accept two Integers and Check if they are Equal.import java.util.*;public class Equal_Integer { public static void main(String[] args) { int m, n; Scanner s = new Scanner(System.in); System.out.print("Enter the first number:"); m = s.nextInt(); System.out.print("Enter the second number:"); n = s.nextInt(); if(m == n) { System.out.println(m+" and "+n+" are equal "); } else { System.out.println(m+" and "+n+" are not equal "); } }}8) Java Program to Compute the Sum of Digits in a given Integer.import java.util.*;public class Digit_Sum { public static void main(String args[]) { int m, n, sum = 0; Scanner s = new Scanner(System.in); System.out.print("Enter the number:"); m = s.nextInt(); while(m > 0) { n = m % 10; sum = sum + n; m = m / 10; } System.out.println("Sum of Digits:"+sum); }}9) Java Program to Extract Digits at that position from a Given Integerimport java.util.*;public class Extract_Digits { public static void main(String args[]) { int n, m, a, i = 1, counter = 0; Scanner s=new Scanner(System.in); System.out.print("Enter any number:"); n = s.nextInt(); m = n; while(n > 0) { n = n / 10; counter++; } while(m > 0) { a = m % 10; System.out.println("Digits at position "+counter+":"+a); m = m / 10; counter--; } }}10 ) Java Program to Print Binary Equivalent of an Integer using Recursion. import java.util.*;public class Binary_Recursion { public static void main(String[] args) { int n; Scanner s = new Scanner(System.in); System.out.print("Enter the number:"); n = s.nextInt(); Binary_Recursion obj = new Binary_Recursion(); String m = obj.Binary(n); System.out.println("Answer:"+m); } String Binary(int x) { if(x > 0) { int a = x % 2; x = x / 2; return a + "" + Binary(x); } return ""; }}Sample input:Enter the number:19Sample output:Answer:1100111) Java Program to Increment by 1 All the Digits of a given Integer import java.util.*;public class Increment_Digits{ public static void main(String[] args) { int n, m = 0, a; Scanner s = new Scanner(System.in); System.out.print("Enter any number:"); n = s.nextInt(); while(n > 0) { a = n % 10; a++; m = m * 10 + a; n = n / 10; } n = m; m = 0; while(n > 0) { a = n % 10; m = m * 10 + a; n = n / 10; } System.out.println("Result:"+m); }}Sample input:Enter any number:4567Sample output:Result:567812) Java Program to Convert Integer Values into Binary. import java.util.*;public class Decimal_Binary { public static void main(String[] args) { int n, m; String x = ""; Scanner s = new Scanner(System.in); System.out.print("Enter the Decimal Number:"); n = s.nextInt(); while(n > 0) { int a = n % 2; x = a + x; n = n / 2; } System.out.println(x); }}Sample input:Enter the Decimal Number:19Sample Output:1001113) Java Program to Convert a Given Number of Days in terms of Years, Weeks & Days. import java.util.*;public class Year_Week_Day { public static void main(String args[]) { int m, year, week, day; Scanner s = new Scanner(System.in); System.out.print("Enter the number of days:"); m = s.nextInt(); year = m / 365; m = m % 365; System.out.println("No. of years:"+year); week = m / 7; m = m % 7; System.out.println("No. of weeks:"+week); day = m; System.out.println("No. of days:"+day); }}Sample input:Enter the number of days:756Sample output:No. of years:2No. of weeks:3No. of days:514) Java Program to Convert Integer Values into Byte, Character, Float. import java.util.*;public class Integer_Conversion{ public static void main(String[] args) { int a; byte b; char c; float d; Scanner s = new Scanner(System.in); System.out.print("Enter any integer:"); a = s.nextInt(); b = (byte) a; System.out.println("Conversion into byte:"+b); c = (char) a; System.out.println("Conversion into char:"+c); d = a; System.out.println("Conversion into float:"+d); }}15) Java Program to Check if a Given Character is Vowel or Consonant using buffer reader functionimport java.io.*;import java.util.*;public class Vowel_Consonant { public static void main(String[] args) { char n; BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter the character you want to check:"); n = (char) bf.read(); n=n.toLowerCase(); if(n=='a'||n=='e'||n=='i'||n=='o'||n=='u') { System.out.print("Vowel"); } else { System.out.ptint("Consonant"); } }}16) Java Program to Check if given Alphabets are Uppercase or Lowercase or Digits. import java.io.*;public class Alphabet_Check { public static void main(String args[]) { char m; BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter any alphabet:"); m = (char) bf.read(); if(m >= 97 && m <= 123) { System.out.println("Lower Case"); } else if(m >= 65 && m <= 96) { System.out.println("Upper Case"); } else if(m >= 48 && m <= 57) { System.out.println("Digit"); } }}17) Java Program to Find Largest Between Three Numbers Using Ternary Operator. import java.util.*;public class Largest_Ternary { public static void main(String[] args) { int a, b, c, d; Scanner s = new Scanner(System.in); System.out.println("Enter all three numbers:"); a = s.nextInt(); b = s.nextInt(); c = s.nextInt(); d = c > (a > b ? a : b) ? c : ((a > b) ? a : b); System.out.println("Largest Number:"+d); }}18) Java Program to Display Numbers from 1 to 10 Using For Loop. import java.util.*;public class Use_For_Loop{ public static void main(String[] args) { for(int i = 1; i <= 10; i++) { System.out.println(i); } }}19) Java Program to Check Whether Given Number is Divisible by 5. import java.util.*;public class Check_Divisiblity{ public static void main(String[] args) { int n; Scanner s = new Scanner(System.in); System.out.print("Enter any number:"); n = s.nextInt(); if(n % 5 == 0) { System.out.println(n+" is divisible by 5"); } else { System.out.println(n+" is not divisible by 5"); } }}20 ) Java Program to Print Multiplication Table for any Number.
import java.util.*;public class Multiplication_Table { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.print("Enter number:"); int n=s.nextInt(); for(int i=1; i <= 10; i++) { System.out.println(n+" * "+i+" = "+n*i); } }}Sample Input/output:Enter number:77 * 1 = 77 * 2 = 147 * 3 = 217 * 4 = 287 * 5 = 357 * 6 = 427 * 7 = 497 * 8 = 567 * 9 = 637 * 10 = 7021) Java Program to Reverse a Number and find the Sum of its Digits Using do-while Loop. import java.util.*;public class Use_Do_While{ public static void main(String[] args) { int n, a, m = 0, sum = 0; Scanner s = new Scanner(System.in); System.out.print("Enter any number:"); n = s.nextInt(); do { a = n % 10; m = m * 10 + a; sum = sum + a; n = n / 10; } while( n > 0); System.out.println("Reverse:"+m); System.out.println("Sum of digits:"+sum); }}22) java program to print without main function.import java.util.*;public class Without_Main { static { System.out.println("Hello World!!"); System.exit(0); } /*Does not work with JDK7*/}Sample output:Hello World!!23) java program to return value using a method.import java.util.*;public class Return_Value{ void input() { int x, y, z; Scanner s = new Scanner(System.in); System.out.print("Enter first integer:"); x = s.nextInt(); System.out.print("Enter second integer:"); y = s.nextInt(); z = add(x, y); System.out.println("Result:"+z); } int add(int a, int b) { int c; c = a + b; return c; } public static void main(String[] args) { Return_Value obj = new Return_Value(); obj.input(); }}sample Input:Enter first integer:5Enter second integer:6sample output:Result:1124) java code to find the byte sum of two integer values.import java.util.*;public class Byte_Sum{ public static void main(String[] args) { byte a, b; int x, y, z; Scanner s = new Scanner(System.in); System.out.print("Enter first byte value:"); a = s.nextByte(); x = a; System.out.print("Enter second byte value:"); b = s.nextByte(); y = b; z = x + y; System.out.println("Result:"+z); }}Sample input:Enter first byte value:124Enter second byte value:115Sample output:Result:23925) java program to print the statement without using semi-colon at the end.import java.util.*;public class Print_Without_Semicolon { public static void main(String[] args) { int i = 0; if(System.out.printf("Hi people!! ") == null) {} for(i = 1; i < 2; System.out.println("the text is Hello World.")) { i++; } }}Sample output:Hi people!! the text is Hello World.25) Java Program to Compute GCD.import static java.lang.StrictMath.min;import java.util.*;public class GCD{ public static void main(String args[]) { int a, b, GCD = 1; Scanner s = new Scanner(System.in); System.out.print("Enter First Number:"); a = s.nextInt(); System.out.print("Enter Second Number:"); b = s.nextInt(); int n = min(a,b); for(int i = 2; i < n; i++) { while(a % i == 0 && b % i==0) { GCD = GCD * i; a = a / i; b = b / i; } } System.out.println("Greatest Common Divisor:"+GCD); }}Sample input:Enter First Number:24Enter Second Number:16Sample output:Greatest Common Divisor:826) java program to print sum of n natural numbersimport java.util.*;public class Natural { public static void main(String args[]) { int x, i = 1 ; int sum = 0; System.out.println("Enter Number of items :"); Scanner s = new Scanner(System.in); x = s.nextInt(); while(i <= x) { sum = sum +i; i++; } System.out.println("Sum of "+x+" numbers is :"+sum); } } sample input:Enter Number of items :55Sample output:Sum of 55 numbers is :154027) java program to print the perfect squares between 1 to nclass CountSquares {
static int countSquares(int a, int b)
{ int cnt = 0;
for (int i = a; i <= b;
for (int j = 1; j * j <= i; j++)
if (j * j == i)
cnt++; return cnt;
} } public class PerfectSquares {
public static void main(String[] args)
{ int a = 1, b = 25;
CountSquares obj = new CountSquares();
System.out.print("Count of squares is " + obj.countSquares(a, b));
} } Head count of birds and animalsimport java.util.*;public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int heads=sc.nextInt(); int legs=sc.nextInt(); int birds=0; for(birds=0;birds<=heads;birds++) { if(birds*2+(heads-birds)*4==legs) { System.out.print(birds+" "); System.out.print(heads-birds); } } }}input 1:27 84output 1:12 15Interlace between even and odd integers:import java.util.*;public class MyClass { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int num1=sc.nextInt(); int num2=sc.nextInt(); for(int index1=num1,index2=num2;index1<=num2;index1++,index2--) { if(index1%2==1) System.out.print(index1+" "); if(index2%2==0) System.out.print(index2+" "); } }}input 1:511output 1:5 10 7 8 9 6 11 Print days in a Month:import java.util.*;public class Main{ public static void main(String[] args) {Scanner sc=new Scanner(System.in);int n=sc.nextInt();if(n>=13)System.out.print("Invalid");else if(n==1)System.out.print("31");else if(n==2)System.out.print("28");else if(n==3)System.out.print("31");else if(n==4)System.out.print("30");else if(n==5)System.out.print("31");else if(n==6)System.out.print("30");else if(n==7)System.out.print("31");else if(n==8)System.out.print("31");else if(n==9)System.out.print("30");else if(n==10)System.out.print("31");else if(n==11)System.out.print("30");else if(n==12)System.out.print("31");}} input 1:5output 1:31input 2:15output 2:InvalidPrint the sum of ASCII values:import java.util.*;public class Hello { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String str=sc.next(); int l=str.length(); int sum=0; for(int index=0;index<l;index++) { sum+=(int)str.charAt(index); } System.out.print(sum); }}input 1:abcdeoutput 1:495input 2:abcdoutput 2:394Print equivalent numerical values of characters of a string:import java.util.*;public class Main { public static void main(String args[]) { Scanner sc=new Scanner(System.in); String str=sc.next(); for(int index=0;index<str.length();index++) { System.out.print(Character.getNumericValue(str.charAt(index))+" "); } }}input 1:abcdeoutput 1:10 11 12 13 14 input 2:mnopoutput 2:22 23 24 25Count number of flips to convert A to B:import java.util.*;public class Main{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); int a=sc.nextInt(); int b=sc.nextInt(); int num=a^b,count=0; while(num!=0) { count+=num&1; num>>=1; } System.out.print(count); }}input 1:10 20output 1:4input 2:12 10output 2:2explanation:binary representation of 12 is 1100 and 10 is 1010. hece two bits are to be flipped to convert A to B.Number series Ap progression - find Nth term:import java.util.Scanner;public class Main{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); int a=sc.nextInt(); int b=sc.nextInt(); int c=sc.nextInt(); int d=sc.nextInt(); int k=sc.nextInt(); int diff1=c-a; int diff2=d-b; int num1=a,num2=b; for(int index=1;index<=k-2;index++) { if(index%2==1) { num1=num1+diff1; } else { num2=num2+diff2; } } if(k%2==0) System.out.print(num2); else System.out.print(num1); }}input 1:1 1 5 410output 1:13input 2:2 1 4 77output 2:5