Program 1: Print hello world
class Main{
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Output:
Program 2: Add 2 numbers
class Main{
static int add(int x, int y){
return x+y;
}
public static void main(String[] args) {
System.out.println(add(6,9));
}
}
Output:
Program 3 : Subtract 2 numbers
class Main{
static int subtract(int x, int y){
return x-y;
}
public static void main(String[] args) {
System.out.println(subtract(9,6));
}
}
Output:
Program 4: Multiply 2 numbers
class Main{
static int multiply(int x, int y){
return x*y;
}
public static void main(String[] args) {
System.out.println(multiply(9,6));
}
}
Output:
Program 5: Divide 2 numbers
class Main{
static float divide(float x, float y){
return x/y;
}
public static void main(String[] args) {
System.out.println(divide(9,6));
}
}
Output:
Program 6: Square a number
class Main{
static float sqr(float x){
return x*x;
}
public static void main(String[] args) {
System.out.println(sqr(9));
}
}
Output
Program 7: sqrt of a number
import java.lang.Math;
class Main{
public static void main(String[] args) {
System.out.println(Math.sqrt(9));
}
}
Output:
Program 8: Factorial
class Main{
public static void main(String[] args) {
int fact=1;
for(int i=1; i<=5; i++){
fact=fact*i;
}
System.out.println(fact);
}
}
Output:
Program 9: Fibonacci Series
class Main{
public static void main(String[] args) {
int n1=0, n2=1, n3,count=10;
System.out.print(n1+ " "+n2);
for(int i =2; i<count;i++){
n3 = n1+n2;
System.out.print(" "+n3);
n1=n2;
n2=n3;
}
}
}
Output:
Program 10: Palindrome number
class Main{
public static void main(String[] args) {
int r, sum=0, temp;
int n = 1234321;
temp = n;
while(n>0){
r=n%10;
sum=(sum*10)+r;
n/=10;
}
if(temp==sum){
System.out.println("Palindrome Number");
}else{
System.out.println("Not a Palindrome number");
}
}
}
Output:
Program 11: Generate random number
import java.lang.Math;
class Main{
public static void main(String[] args) {
double num = Math.random()*(69-0+1)+0;
System.out.println("num: "+(int)num);
}
}
Output:
Program 12: Typecasting Variables
import java.lang.Math;
class Main{
public static void main(String[] args) {
double num = 10.0;
int n = (int)num;
System.out.println(n);
}
}
Output
Program 13: ASCII value
import java.lang.Math;
class Main{
public static void main(String[] args) {
char c = 'A';
int ascii = (int)c;
System.out.println(ascii);
}
}
Output:
Program 14: Reverse of a word
import java.lang.Math;
class Main{
public static void main(String[] args) {
String word = "DPM", rev_word="";
char ch;
for(int i=0; i<word.length(); i++){
ch = word.charAt(i);
rev_word = ch+rev_word;
}
System.out.println(rev_word);
}
}
Output
Program 15: Automorphic
import java.lang.Math;
class Main{
public static void main(String[] args) {
int i = 76;
int sq = i*i;
while(i>0){
if (i % 10 != sq % 10) {
System.out.println("Not an Automorphic");
}
i = i/10;
sq = sq/10;
System.out.println("Automorphic");
}
}
}
Output:
Program 16: Pattern
class Main {
public static void main(String[] args) {
int i, j, row=3;
for(i=0;i<row;i++){
for(j=0;j<=i;j++){
System.out.print("*");
}
System.out.println();
}
}
}
Output:
Program 17: pattern
class Main {
public static void main(String[] args) {
int i, j, row=3;
for(i=1;i<=row;i++){
for(j=1;j<=i;j++){
System.out.print(i+" ");
}
System.out.println();
}
}
}
Output
Program 18: Peterson number (A number is said to be Peterson if the sum of factorials of
each digit is equal to the sum of the number itself.)
public class Main {
static boolean checkPerfectSquare(double number) {
double sqrt = Math.sqrt(number);
return ((sqrt - Math.floor(sqrt)) == 0);
}
public static void main(String[] args) {
double number = 49;
System.out.println("Given number is "+number);
if (checkPerfectSquare(number))
System.out.print("Yes, the given number is perfect square.");
else
System.out.print("No, the given number is not perfect square.");
}
}
Output:
Program 19: Even Number
public class Main {
public static void main(String[] args) {
int num = 10;
for(int i = 1; i<=num; i++){
if(i%2==0)
System.out.print(i+ " ");
}
}
}
Output
Program 20: Odd Numbers
public class Main {
public static void main(String[] args) {
int num = 10;
for(int i = 1; i<=num; i++){
if(i%2!=0)
System.out.print(i+ " ");
}
}
}
Output
Program 21: Sum of 10 Natural Numbers
public class Main {
public static void main(String[] args) {
int num = 10, sum=0;
for(int i = 1; i<=num; i++){
sum += i;
}
System.out.println(sum);
}
}
Program 22: Count the total number of characters in a string
public class Main {
public static void main(String[] args) {
String str = "Santosh sir";
int count = 0;
for(int i = 0; i<str.length(); i++){
count++;
}
System.out.println(count);
}
}
Output
Program 23: Count the total number of vowels and consonants in a String
public class Main {
public static void main(String[] args) {
int vCount = 0, cCount = 0;
String str = "I am the best programmer";
str = str.toLowerCase();
for(int i = 0; i < str.length(); i++){
if(str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i' ||
str.charAt(i) == 'o' || str.charAt(i) == 'u') {
vCount++;
}
else if(str.charAt(i) >= 'a' && str.charAt(i)<='z') {
cCount++;
}
}
System.out.println("Number of vowels: " + vCount);
System.out.println("Number of consonants: " + cCount);
}
}
Output
Program 24: Replace all spaces from a string
public class Main {
public static void main(String[] args) {
String str = "Hello World";
str = str.replaceAll("\\s", "");
System.out.println(str);
}
}
Program 25: Replace Uppercase letters with lowercase and vice versa
public class Main {
public static void main(String[] args) {
String str1="Great Power";
StringBuffer newStr=new StringBuffer(str1);
for(int i = 0; i < str1.length(); i++) {
if(Character.isLowerCase(str1.charAt(i))) {
newStr.setCharAt(i, Character.toUpperCase(str1.charAt(i)));
}
else if(Character.isUpperCase(str1.charAt(i))) {
newStr.setCharAt(i, Character.toLowerCase(str1.charAt(i)));
}
}
System.out.println(newStr);
}
}
Output
Program 26: Check whether a String is a palindrome
public class Main {
public static void main(String[] args) {
String string = "Kayak";
boolean flag = true;
string = string.toLowerCase();
for(int i = 0; i < string.length()/2; i++){
if(string.charAt(i) != string.charAt(string.length()-i-1)){
flag = false;
break;
}
}
if(flag)
System.out.println("Given string is palindrome");
else
System.out.println("Given string is not a palindrome");
}
}
Output
Program 27: Reverse of a String
public class Main {
public static void main(String[] args) {
String string = "Computer is fun";
String reversedStr = "";
for(int i = string.length()-1; i >= 0; i--){
reversedStr = reversedStr + string.charAt(i);
}
System.out.println("Reverse: " + reversedStr);
}
}
Output:
Program 28: Display Duplicate words
public class Main {
public static void main(String[] args) {
String str = "aa bb c ddd e";
int count;
char string[] = str.toCharArray();
for(int i = 0; i <string.length; i++) {
count = 1;
for(int j = i+1; j <string.length; j++) {
if(string[i] == string[j] && string[i] != ' ') {
count++;
string[j] = '0';
}
}
if(count > 1 && string[i] != '0')
System.out.println(string[i]);
}
}
}
Output:
Program 29: Display duplicate words in a string
public class Main {
public static void main(String[] args) {
String string = "Big black bug bit a big black dog on his big black nose";
int count;
string = string.toLowerCase();
String words[] = string.split(" ");
for(int i = 0; i < words.length; i++) {
count = 1;
for(int j = i+1; j < words.length; j++) {
if(words[i].equals(words[j])) {
count++;
words[j] = "0";
}
}
if(count > 1 && words[i] != "0")
System.out.println(words[i]);
}
}
}
Output
Program 30: Find Frequency of characters
public class Main {
public static void main(String[] args) {
String str = "picture perfect";
int[] freq = new int[str.length()];
int i, j;
char string[] = str.toCharArray();
for(i = 0; i <str.length(); i++) {
freq[i] = 1;
for(j = i+1; j <str.length(); j++) {
if(string[i] == string[j]) {
freq[i]++;
string[j] = '0';
}
}
}
for(i = 0; i <freq.length; i++) {
if(string[i] != ' ' && string[i] != '0')
System.out.print(string[i] + "-" + freq[i]+" ");
}
}
}
Output:
Program 31: Pattern
public class Main {
public static void main(String[] args) {
int i, j, lines = 5;
for (i = 1; i <= lines; i++) {
for (j = lines; j >= 1; j--) {
if (j != i)
System.out.print(j);
else
System.out.print("*");
}
System.out.println("");
}
}
}
Output:
Program 32: Pattern
public class Main {
public static void main(String[] args) {
int lines = 3;
int i = 1;
int j;
for (i = 1; i <= lines; i++) {
for (j = 1; j <= i; j++) {
System.out.print(i * j + " ");
}
System.out.println("");
}
}
}
Output:
Program 33: Pattern
public class Main {
public static void main(String[] args) {
int n = 3;
for(int i = 0 ; i < n ; i++)
{
for(int j = 0 ; j <= i ; j++)
{
System.out.print(" "+(char)(65 + i));
}
System.out.println("");
}
}
}
Output
Program 34: Pattern
public class Main {
public static void main(String[] args) {
int i ,j;
int n = 6;
for(i = n; i>0 ; i-- )
{
for(j = 0; j<i ; j++)
{
System.out.print("69");
}
System.out.println("");
}
}
}
Program 35: Copy all elements of one array to another
public class Main {
public static void main(String[] args) {
int [] arr1 = new int [] {1, 2, 3, 4, 5};
int arr2[] = new int[arr1.length];
for (int i = 0; i < arr1.length; i++) {
arr2[i] = arr1[i];
}
System.out.println("Elements of original array: ");
for (int i = 0; i < arr1.length; i++) {
System.out.print(arr1[i] + " ");
}
System.out.println();
System.out.println("Elements of new array: ");
for (int i = 0; i < arr2.length; i++) {
System.out.print(arr2[i] + " ");
}
}
}
Output:
Program 36: Frequency of Elements of an array
public class Main {
public static void main(String[] args) {
int [] arr = new int [] {1, 2, 8, 3, 2, 2, 2, 5, 1};
int [] fr = new int [arr.length];
int visited = -1;
for(int i = 0; i < arr.length; i++){
int count = 1;
for(int j = i+1; j < arr.length; j++){
if(arr[i] == arr[j]){
count++;
fr[j] = visited;
}
}
if(fr[i] != visited)
fr[i] = count;
}
for(int i = 0; i < fr.length; i++){
if(fr[i] != visited)
System.out.print(arr[i] + "X" + fr[i]+" | ");
}
}
}
Output
Program 37: Print Elements of an array
public class Main {
public static void main(String[] args) {
int num[] = {6,69,96,9,699,6699};
for(int n:num){
System.out.print(n+" ");
}
}
}
Output
Program 38: Arrange elements of an array in ascending order
public class Main {
public static void main(String[] args) {
int [] arr = new int [] {5, 2, 8, 7, 1};
int temp = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = i+1; j < arr.length; j++) {
if(arr[i] > arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
System.out.println();
System.out.println("Elements of array sorted in ascending order: ");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
Output:
Program 39: Print elements of array in ascending order
public class Main {
public static void main(String[] args) {
int [] arr = new int [] {1, 2, 3, 4, 5};
System.out.println();
System.out.println("Array in reverse order: ");
for (int i = arr.length-1; i >= 0; i--) {
System.out.print(arr[i] + " ");
}
}
}
Output:
Program 40: Print elements of an array in reverse order
public class Main {
public static void main(String[] args) {
int [] arr = new int [] {1, 2, 3, 4, 5};
System.out.println();
System.out.println("Array in reverse order: ");
for (int i = arr.length-1; i >= 0; i--) {
System.out.print(arr[i] + " ");
}
}
}
Program 41: Sum of all elements of an array:
public class Main {
public static void main(String[] args) {
int [] arr = new int [] {1, 2, 3, 4, 5};
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum = sum + arr[i];
}
System.out.println("Sum of all the elements of an array: " + sum);
}
}
Output
Program 42: Print elements of an array in descending order
public class Main {
public static void main(String[] args) {
int [] arr = new int [] {5, 2, 8, 7, 1};
int temp = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = i+1; j < arr.length; j++) {
if(arr[i] < arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
System.out.println();
System.out.println("Elements of array sorted in descending order: ");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
Output:
Print 43: Print Largest number of an array:
public class Main {
public static void main(String[] args) {
int [] arr = new int [] {25, 11, 7, 75, 56};
int max = arr[0];
for (int i = 0; i < arr.length; i++) {
if(arr[i] > max)
max = arr[i];
}
System.out.println("Largest element in the array: " + max);
}
}
Output:
Print 44: Number of elements present In an array
public class Main {
public static void main(String[] args) {
int[] arr = new int[] { 1, 2, 3, 4, 5 };
System.out.println("Number of elements in the array: " + arr.length);
}
}
Output:
Program 45: Remove duplicate elements from an array
public class Main {
public static int removeDuplicateElements(int arr[], int n){
if (n==0 || n==1){
return n;
}
int[] temp = new int[n];
int j = 0;
for (int i=0; i<n-1; i++){
if (arr[i] != arr[i+1]){
temp[j++] = arr[i];
}
}
temp[j++] = arr[n-1];
for (int i=0; i<j; i++){
arr[i] = temp[i];
}
return j;
}
public static void main (String[] args) {
int arr[] = {10,20,20,30,30,40,50,50};
int length = arr.length;
length = removeDuplicateElements(arr, length);
for (int i=0; i<length; i++)
System.out.print(arr[i]+" ");
}
}
Output