Core java
20 JanuaryJava program to count positive, zero and negative numbers
In this post, we will see Java program to count positive, zero and negative numbers import java.util.Scanner; public class JavaProgram { public static void main(String args[]) { int contp=0, cntn=0, cntz=0, i; int arr[] = new int[10]; Scanner scan = new Scanner(System.in); System.out.print("Enter 10 Numbers : "); for(i=0; i<10; i++){ arr[i] = scan.nextInt(); } for(i=0; […]
20 JanuaryProgram to find frequency of characters in a string in java
In this tutorial, we will see simple program to find frequency of characters in a string in java. There are multiple ways to solve this proble. Let’s see one by one. [toc] Using counter array Here is the algorithm for the same. Initialize counter array of 256 length Iterate over String and increase count by […]
20 JanuaryJava program to count number of words in sentence
In this tutorial, we will see a simple java program to count number of words in sentence. import java.util.Scanner; public class StringOperator { public static int countWordsinStr(String str){ int count = 1; for(int i=0; i<=str.length()-1; i++){ if(str.charAt(i) == ' ' && str.charAt(i+1)!=' '){ count++; } } return count; } public static void main(String args[]){ String […]
20 JanuaryJava program to print Diamond pattern
In this tutorial, we will see java program to print diamond pattern. import java.util.Scanner; public class DiamondPattPrinter { public static void main(String args[]) { int cnt, c, k, space=1; Scanner scan = new Scanner(System.in); System.out.print("Enter count/number of Rows for Diamond pattern : "); cnt = scan.nextInt(); space = cnt-1; for(k=1; k<=cnt; k++){ for(c=1; c<=space; c++){ […]
20 JanuaryJava program to print Fibonacci Series
In this tutorial, we will print fibonacci series. According to wikipedia: In mathematics, the Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence, and characterized by the fact that every number after the first two is the sum of the two preceding ones:[1][2] 0,1,1,2,3,5,8,13,21,34,55,89,144.. Here is simple program to print […]
19 JanuaryHow to Compare Two Strings in Java
In this post, we will see how to compare two String in java. As you might know, String implements Comparable interface, you can use compareTo method to compare two String in java. import java.util.Scanner; public class StringComparator { public static void main(String args[]) { String str1, str2; Scanner scan = new Scanner(System.in); System.out.print("Enter First String […]
19 JanuaryJava program for sum of digits of number
In this post, we will see how to find sum of digits of number in java. You can find unit’s place digit by number%10, add it to the total and divide the number by 10 to remove the unit’s place. import java.util.Scanner; public class DigitAdditionCalc { public static void main(String args[]) { int number, remdr=0, […]
19 JanuaryJava program to calculate average marks
In this post, we will see simple java program to find average and percentage marks. We have taken average and percentage of five subject marks below. import java.util.Scanner; public class MarksCalculator { public static void main(String args[]) { int markArray[] = new int[5]; int i; float sum=0; float average, percentage; Scanner scan = new Scanner(System.in); […]
19 JanuaryJava program to make simple calculator
In this post, we will see how to create a simple calculator in java. This calculator program performs 4 basic operatios – Addition, Substraction, Multiplication and division.It uses Switch case to choose operation you want to perform. import java.util.Scanner; public class Calculator { public static void main(String args[]) { float num1, num2, result; char operation, […]