Program 1: Print hello world
class Main{
public static void main(String[] args) {
[Link]("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) {
[Link](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) {
[Link](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) {
[Link](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) {
[Link](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) {
[Link](sqr(9));
}
}
Output
Program 7: sqrt of a number
import [Link];
class Main{
public static void main(String[] args) {
[Link]([Link](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;
}
[Link](fact);
}
}
Output:
Program 9: Fibonacci Series
class Main{
public static void main(String[] args) {
int n1=0, n2=1, n3,count=10;
[Link](n1+ " "+n2);
for(int i =2; i<count;i++){
n3 = n1+n2;
[Link](" "+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){
[Link]("Palindrome Number");
}else{
[Link]("Not a Palindrome number");
}
}
}
Output:
Program 11: Generate random number
import [Link];
class Main{
public static void main(String[] args) {
double num = [Link]()*(69-0+1)+0;
[Link]("num: "+(int)num);
}
}
Output:
Program 12: Typecasting Variables
import [Link];
class Main{
public static void main(String[] args) {
double num = 10.0;
int n = (int)num;
[Link](n);
}
}
Output
Program 13: ASCII value
import [Link];
class Main{
public static void main(String[] args) {
char c = 'A';
int ascii = (int)c;
[Link](ascii);
}
}
Output:
Program 14: Reverse of a word
import [Link];
class Main{
public static void main(String[] args) {
String word = "DPM", rev_word="";
char ch;
for(int i=0; i<[Link](); i++){
ch = [Link](i);
rev_word = ch+rev_word;
}
[Link](rev_word);
}
}
Output
Program 15: Automorphic
import [Link];
class Main{
public static void main(String[] args) {
int i = 76;
int sq = i*i;
while(i>0){
if (i % 10 != sq % 10) {
[Link]("Not an Automorphic");
}
i = i/10;
sq = sq/10;
[Link]("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++){
[Link]("*");
}
[Link]();
}
}
}
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++){
[Link](i+" ");
}
[Link]();
}
}
}
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 = [Link](number);
return ((sqrt - [Link](sqrt)) == 0);
}
public static void main(String[] args) {
double number = 49;
[Link]("Given number is "+number);
if (checkPerfectSquare(number))
[Link]("Yes, the given number is perfect square.");
else
[Link]("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)
[Link](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)
[Link](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;
}
[Link](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<[Link](); i++){
count++;
}
[Link](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 = [Link]();
for(int i = 0; i < [Link](); i++){
if([Link](i) == 'a' || [Link](i) == 'e' || [Link](i) == 'i' ||
[Link](i) == 'o' || [Link](i) == 'u') {
vCount++;
}
else if([Link](i) >= 'a' && [Link](i)<='z') {
cCount++;
}
}
[Link]("Number of vowels: " + vCount);
[Link]("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 = [Link]("\\s", "");
[Link](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 < [Link](); i++) {
if([Link]([Link](i))) {
[Link](i, [Link]([Link](i)));
}
else if([Link]([Link](i))) {
[Link](i, [Link]([Link](i)));
}
}
[Link](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 = [Link]();
for(int i = 0; i < [Link]()/2; i++){
if([Link](i) != [Link]([Link]()-i-1)){
flag = false;
break;
}
}
if(flag)
[Link]("Given string is palindrome");
else
[Link]("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 = [Link]()-1; i >= 0; i--){
reversedStr = reversedStr + [Link](i);
}
[Link]("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[] = [Link]();
for(int i = 0; i <[Link]; i++) {
count = 1;
for(int j = i+1; j <[Link]; j++) {
if(string[i] == string[j] && string[i] != ' ') {
count++;
string[j] = '0';
}
}
if(count > 1 && string[i] != '0')
[Link](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 = [Link]();
String words[] = [Link](" ");
for(int i = 0; i < [Link]; i++) {
count = 1;
for(int j = i+1; j < [Link]; j++) {
if(words[i].equals(words[j])) {
count++;
words[j] = "0";
}
}
if(count > 1 && words[i] != "0")
[Link](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[[Link]()];
int i, j;
char string[] = [Link]();
for(i = 0; i <[Link](); i++) {
freq[i] = 1;
for(j = i+1; j <[Link](); j++) {
if(string[i] == string[j]) {
freq[i]++;
string[j] = '0';
}
}
}
for(i = 0; i <[Link]; i++) {
if(string[i] != ' ' && string[i] != '0')
[Link](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)
[Link](j);
else
[Link]("*");
}
[Link]("");
}
}
}
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++) {
[Link](i * j + " ");
}
[Link]("");
}
}
}
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++)
{
[Link](" "+(char)(65 + i));
}
[Link]("");
}
}
}
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++)
{
[Link]("69");
}
[Link]("");
}
}
}
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[[Link]];
for (int i = 0; i < [Link]; i++) {
arr2[i] = arr1[i];
}
[Link]("Elements of original array: ");
for (int i = 0; i < [Link]; i++) {
[Link](arr1[i] + " ");
}
[Link]();
[Link]("Elements of new array: ");
for (int i = 0; i < [Link]; i++) {
[Link](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 [[Link]];
int visited = -1;
for(int i = 0; i < [Link]; i++){
int count = 1;
for(int j = i+1; j < [Link]; j++){
if(arr[i] == arr[j]){
count++;
fr[j] = visited;
}
}
if(fr[i] != visited)
fr[i] = count;
}
for(int i = 0; i < [Link]; i++){
if(fr[i] != visited)
[Link](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){
[Link](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 < [Link]; i++) {
for (int j = i+1; j < [Link]; j++) {
if(arr[i] > arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
[Link]();
[Link]("Elements of array sorted in ascending order: ");
for (int i = 0; i < [Link]; i++) {
[Link](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};
[Link]();
[Link]("Array in reverse order: ");
for (int i = [Link]-1; i >= 0; i--) {
[Link](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};
[Link]();
[Link]("Array in reverse order: ");
for (int i = [Link]-1; i >= 0; i--) {
[Link](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 < [Link]; i++) {
sum = sum + arr[i];
}
[Link]("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 < [Link]; i++) {
for (int j = i+1; j < [Link]; j++) {
if(arr[i] < arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
[Link]();
[Link]("Elements of array sorted in descending order: ");
for (int i = 0; i < [Link]; i++) {
[Link](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 < [Link]; i++) {
if(arr[i] > max)
max = arr[i];
}
[Link]("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 };
[Link]("Number of elements in the array: " + [Link]);
}
}
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 = [Link];
length = removeDuplicateElements(arr, length);
for (int i=0; i<length; i++)
[Link](arr[i]+" ");
}
}
Output