Simple Loops
Sum of Digits
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt(); // 5 // 25
int sum = 0;
while (n > 0)
{
int lastDigit = n % 10;
sum = sum + lastDigit;
n = n / 10;
}
System.out.println(sum); // 5 // 7
}
}Sum of digits of all numbers from 1 to N.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int sum = 0;
for(i = 1; i <= n; i++)
{
sum = sum + i;
}
System.out.println(sum);
}
}Sum of Prime Numbers
// Print sum of all prime numbers
// TC - O(n√n)
import java.util.Scanner;
public class Main {
public static boolean isPrime(int n)
{
if(n < 2)
{
return false;
}
for(int i = 2; i * i <= n; i++)
{
if(n % i == 0)
{
return false;
}
}
return true;
}
public static void sumPrime(int st, int end)
{
long sum = 0;
for(int i = Math.max(st, 2); i <= end; i++)
{
if(isPrime(i))
{
sum += i;
}
}
System.out.println(sum);
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int st = in.nextInt();
int end = in.nextInt();
sumPrime(st,end);
}
}
// Sieve Summation
// TC - O(n log log n)
import java.util.*;
public static void sumPrime(int st, int end)
{
if (end < 2 || st > end) {
System.out.println(0);
return;
}
boolean[] isPrime = new boolean[end + 1];
Arrays.fill(isPrime, true);
isPrime[0] = false;
isPrime[1] = false;
// even numbers are not prime
for (int i = 4; i <= end; i+=2) {
isPrime[i] = false;
}
// not all multipliers are prime
for (int i = 3; i <= end; i+=2) {
// i = 3, j = 9, 12, ... n
// i = 5, j = 25, .... n
// i = 7, j = 49, ... n
if (isPrime[i]) {
for (int j = i * i; j <= end; j += i) {
isPrime[j] = false;
}
}
}
long sum = 0;
for (int i = Math.max(st, 2); i <= end; i++) {
if (isPrime[i]) {
sum += i;
}
}
System.out.println(sum);
}Palindrome or Not
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
// Reverse the order and see if it is same as original or not
// 701 =? 107
// 101 =? 101
Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt();
int x = N;
int rev = 0;
while (x > 0)
{
int lastDigit = x % 10;
rev = rev * 10 + lastDigit;
x = x / 10;
}
if(N == rev)
{
System.out.println("Palindrome");
}
else
{
System.out.println("Not Palindrome");
}
}
}Armstrong or Not
import java.util.Scanner;
public class Main{
public static int powerFn(int a, int b)
{
int ans = 1;
for(int i = 1; i <= b; i++)
{
ans = ans * a;
}
return ans;
}
public static void main(String[] args)
{
// If sum is same as original number or not
// Step 1 - Count number of digits present - 704 = 3 -> d
// Step 2 - (lastDigit)^d = n
// Step 3 - sum == n ?
// Example
// 7^3 + 0^3 + 4^3 =? 704
// 1^4 + 6^4 + 3^4 + 4^4 =? 1634
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int copyN = n;
int numberOfDigits = 0;
while(n > 0)
{
// Calculate how many digits we have
numberOfDigits ++;
n = n / 10;
}
int sum = 0;
copyN = n;
while(n > 0)
{
int lastDigit = copyN % 10;
sum = sum + powerFn(lastDigit, numberOfDigits); // Main logic
copyN = copyN / 10;
}
if(sum == n)
{
System.out.println("Armstrong");
}
else
{
System.out.println("Not Armstrong");
}
}
}Find Armstrong numbers between 1 and N
import java.util.Scanner;
public class Main {
public static int powerFn(int a , int b)
{
int ans = 1;
for(int i = 1; i <= b; i++)
{
ans = ans * a;
}
return ans;
}
public static boolean Armstrong(int N)
{
int x = N;
int count = 0;
int sum = 0;
while(x > 0)
{
count++;
x /= 10;
}
x = N;
while(x > 0)
{
int lastDigit = x % 10;
sum += powerFn(lastDigit, count);
x /= 10;
}
return sum == N;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int st = in.nextInt();
int end = in.nextInt();
for(int i = st; i <= end; i++)
{
if(Armstrong(i))
{
System.out.print(i + " ");
}
}
}
}Prime Factors
// TC - O(n√n)
import java.util.Scanner;
public class Main {
public static boolean isPrime(int n) {
if (n < 2) return false;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt(); // 24
System.out.print("Prime factors of " + n + ": ");
boolean first = true;
for (int i = 1; i <= n; i++) {
if (n % i == 0 && isPrime(i)) {
if (!first) {
System.out.print(", ");
}
System.out.print(i);
first = false;
}
}
System.out.println(); // 2, 3
}
}Print all factors of a number.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
for (int i = 1; i <= n; i++) {
if (n % i == 0) {
System.out.print(i + " ");
}
}
}
}Fibonacci series up to N terms
public class Main {
public static void main(String[] args) {
int n = 10;
if (n <= 0) {
return;
}
int fTerm = 0;
int sTerm = 1;
if (n >= 1) System.out.print(fTerm);
if (n >= 2) System.out.print(" " + sTerm);
for (int i = 3; i <= n; i++) {
int tTerm = fTerm + sTerm;
System.out.print(" " + tTerm);
fTerm = sTerm;
sTerm = tTerm;
}
// 0 1 1 2 3 5 8 13 21 34
}
}Binary to Decimal
public class Main{
public static void main(String[] args)
{
int n = 1010; // Binary
int power = 1;
int sum = 0;
while(n > 0)
{
int lastDigit = n % 10;
sum = sum + lastDigit * power;
power = power * 2;
n = n / 10;
}
System.out.println(sum); // 10 -> Decimal
}
}
// Optimised
public class Main{
public static void main(String[] args)
{
String binary = "1010";
int sum = 0;
int power = 1;
for(int i = binary.length() - 1; i >= 0; i--) {
int digit = binary.charAt(i) - '0';
sum += digit * power;
power *= 2;
}
System.out.println(sum); // 10 -> Decimal
}
}Decimal To Binary
public static void decimalToBinary(int n) {
StringBuilder sb = new StringBuilder();
while (n > 0) {
sb.append(n % 2); // remainder (0 or 1)
n /= 2;
}
System.out.println(sb.reverse().toString());
}
// Another approach
public static void decimalToBinaryBitwise(int n) {
StringBuilder sb = new StringBuilder();
while (n > 0) {
sb.append(n & 1); // extract last bit
n = n >> 1; // shift right
}
System.out.println(sb.reverse().toString());
}Perform input output of all basic data types
import java.util.Scanner;
class Main {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
char ch = in.next().charAt(0);
int n = in.nextInt();
float f = in.nextFloat();
System.out.println(ch);
System.out.println(n);
System.out.println(f);
}
}Perform all arithmetic operations
import java.util.Scanner;
class Main {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n1 = in.nextInt();
int n2 = in.nextInt();
System.out.println(n1 + n2);
System.out.println(n1 - n2);
System.out.println(n1 * n2);
System.out.println(n1 / n2);
System.out.println(n1 % n2);
}
}Find the area and perimeter of the rectangle.
import java.util.Scanner;
class Main {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n1 = in.nextInt();
int n2 = in.nextInt();
System.out.println("Area " + (n1 * n2));
System.out.println("Perimeter " + (2 * (n1 + n2)));
}
}Get Kirti Konka’s stories in your inbox
Join Medium for free to get updates from this writer.
Enter temperature in Fahrenheit and convert to Celsius
import java.util.Scanner;
class Main {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
double F = in.nextDouble();
double C = ((F - 32) * 5.0) / 9.0;
System.out.println(C);
}
}Calculate total average and percentage of five subjects
import java.util.Scanner;
class Main {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int a = in.nextInt();
int b = in.nextInt();
int c = in.nextInt();
int d = in.nextInt();
int e = in.nextInt();
System.out.println("Sum " + (a + b + c + d + e));
System.out.println("Perimeter " + ((a + b + c + d + e) / 5));
}
}Program to check whether a number is negative, positive or zero.
import java.util.Scanner;
class Main {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
if(n > 0)
System.out.println("Number is positive");
else if(n < 0)
System.out.println("Number is negative");
else if(n == 0)
System.out.println("Number is zero");
else
System.out.println("Number is invalid");
}
}Program to check whether a number is divisible by 5 and 11 or not.
import java.util.Scanner;
class Main {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
if(n % 5 == 0 || n % 11 == 0)
System.out.println("Number is divisible");
else
System.out.println("Number is not divisible");
}
}Program to check whether a number is even or odd.
import java.util.Scanner;
class Main {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
if(n % 2 == 0)
System.out.println("Number is even");
else
System.out.println("Number is odd");
}
}Program to find sum of all even numbers between 1 to n.
import java.util.Scanner;
class Main {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
for(int i = 2; i <= n; i+2)
System.out.println(i + " ");
}
}Program to find sum of all odd numbers between 1 to n.
import java.util.Scanner;
class Main {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
for(int i = 1; i <= n; i+2)
System.out.println(i + " ");
}
}Program to find HCF (GCD) of two numbers.
import java.util.Scanner;
class Main {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int n1 = in.nextInt();
int hcf = findHCF(n, n1);
System.out.println("HCF = " + hcf);
}
public static int findHCF(int a, int b)
{
if(b == 0)
return a;
return (b, a % b);
}
}Program to find LCM of two numbers
import java.util.Scanner;
class Main {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int n1 = in.nextInt();
int lcm = findLCM(n, n1);
System.out.println("LCM = " + hcf);
}
public static int findLCM(int a, int b)
{
if(a == 0 || b == 0)
return 0;
return Math.abs(a*b)/ findGCD(a, b);
}
public static int findGCD(int a, int b)
{
while(b != 0)
{
int temp = b;
b = a;
a = temp;
}
return a;
}
}Check whether a year is a leap year or not.
import java.util.Scanner;
class Main {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
if((n % 4 == 0 && n % 100 != 0)|| n % 400 == 0)
System.out.println("Year is a leap year");
else
System.out.println("Year is not a leap year");
}
}Check whether a character is alphabet or digit.
import java.util.Scanner;
class Main {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n = in.nextChar(0);
if((ch > = 'a' && ch <= 'z') || (ch > = 'A' && ch <= 'Z'))
System.out.println("Character is an alphabet");
if(ch > = '0' && ch <= '9')
System.out.println("Character is a digit");
else
System.out.println("Character is special");
}
}Enter week number and print day of the week.
import java.util.Scanner;
class Main {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
String dayN = switch(n)
{
case 1 -> "Sunday";
case 1 -> "Monday";
case 1 -> "Tueday";
case 1 -> "Wednesday";
case 1 -> "Thursday";
case 1 -> "Friday";
case 1 -> "Saturday";
default -> "Invalid";
}
System.out.println(dayN);
}
}Print multiplication table of a given number.
import java.util.Scanner;
class Main {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
for(int i = 1; i <= 10; i++)
System.out.println(n + " * " + i + " = " + (a * i));
}
}Print the first and last digit of a number.
import java.util.Scanner;
class Main {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int last = n % 10;
int first = n;
while(first >= 10)
first = first/10;
System.out.println(first);
System.out.println(last);
}
}Key takeaways
For Loops: To travel from 1 point to another.
Do While Loops: When talking about digits.
Division: To remove last digit
Modular: Find last digit







