1.SUM of DIGITS in an integer
import java.util.Scanner;
public class AddDigits {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
int sum = 0;
while(number > 0){
int x = number % 10;
sum += x;
number /= 10;
}
System.out.println(sum);
}
}
2.ARMSTRONG NUMBER or NOT
import java.util.*;
import java.io.*;
public class ArmstrongNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
int temp = number;
int sum = 0;
String num = Integer.toString(number);
int size = num.length();
while(number>0){
int x = number % 10;
sum = sum + (int)Math.pow(x,size);
number /= 10;
}
if(sum==temp){
System.out.println("Armstrong Number");
}else{
System.out.println("Not a Armstrong Number");
}
}
}
3.ODD or EVEN number
import java.util.*;
public class CheckOddEven {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
if(number%2==0){
System.out.println("Even");
}else{
System.out.println("Odd");
}
}
}
4.PALINDROME number or NOT
import java.util.*;
public class CheckPalindrome {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
int temp = number;
int rev = 0;
while(number > 0 ){
int x = number%10;
rev = rev*10 + x;
number /= 10;
}
System.out.println(rev == temp ? "It is a palindrome" : "It is NOT a palindrome");
}
}
5.PRIME number or NOT
import java.util.*;
import java.io.*;
class CheckPrimeNumber{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
int count = 0;
for(int i = 2; i < number/2; i++){
if(number % i == 0){
count++;
}
}
System.out.println(count == 0 ? "Prime number" : "NOT a prime number");
}
}
6.COUNT DIGITS in an integer
import java.util.*;
import java.io.*;
public class CountDigits {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
int count = 0;
while(number > 0){
count++;
number /= 10;
}
System.out.println(count);
}
}
7.FACTORIAL of a number
import java.util.*;
public class Factorial {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
int fact = 1;
for(int i = 1; i <= number; i++){
fact = fact*i;
}
System.out.println(fact);
}
}
8.FIBONACCI Series
import java.util.*;
public class Fibonacci{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
int a = 0;
int b = 1, c;
for(int i = 1; i <= num; i++){
c = a + b;
System.out.print(c+" ");
a = b;
b = c;
}
}
}
9.NATURAL number SERIES
import java.util.*;
import java.io.*;
public class PrintNaturalNumbers {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
for(int i = 1; i <= number; i++){
System.out.print(i+" ");
}
}
}
10.REVERSE the DIGITS in an integer
import java.util.*;
public class ReverseDigits {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
int rev = 0;
while(number > 0){
int x = number % 10;
rev = rev*10 + x;
number /= 10;
}
System.out.println(rev);
}
}
11.STRONG NUMBER or NOT
import java.util.*;
import java.io.*;
public class StrongNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
int sum = 0;
int temp = number;
while(number > 0){
int x = number % 10;
sum = sum + fact(x);
number /= 10;
}
if(sum==temp){
System.out.println("Strong Number");
}else{
System.out.println("Not a Strong Number");
}
}
public static int fact(int number){
int fact = 1;
for(int i = 1; i <= number; i++){
fact = fact*i;
}
return fact;
}
}
12.SUM of NATURAL NUMBERS
import java.util.*;
class SumOfNaturalNumbers{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
int sum = 0;
for(int i=1 ;i <= number; i++){
sum += i;
}
System.out.println(sum);
}
}
13.SWAP 2 numbers using Temporary Variable
import java.util.*;
public class SwapNumbers1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter 2 numbers: ");
int number1 = sc.nextInt();
int number2 = sc.nextInt();
System.out.println("Numbers before swapping: "+number1+","+number2);
int temp = number1;
number1 = number2;
number2 = temp;
System.out.println("Numbers after swapping: "+number1+","+number2);
}
}
14.SWAP 2 numbers using Arithmetic Operations
import java.util.*;
public class SwapNumbers2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter 2 numbers:");
int num1 = sc.nextInt();
int num2 = sc.nextInt();
System.out.println("Numbers before swapping: "+num1+","+num2);
num1 = num1 + num2;
num2 = num1 - num2;
num1 = num1 - num2;
System.out.println("Numbers after swapping: "+num1+","+num2);
}
}
15.HCF of 2 numbers
import java.util.Scanner;
public class HCFOfTwoNumbers {
public static void main(String args[]){
int a, b, i, hcf = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter number 1 :");
a = sc.nextInt();
System.out.println("Enter number 2 : ");
b = sc.nextInt();
for(i = 1; i <= a || i <= b; i++) {
if( a%i == 0 && b%i == 0 )
hcf = i;
}
System.out.println("HCF of given two numbers is : "+hcf);
}
}
16.LCM of 2 numbers
public class LCMofTwoNumbers{
public static void main(String[] args) {
int lcm = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter number 1 :");
int a = sc.nextInt();
System.out.println("Enter number 2 : ");
int b = sc.nextInt();
lcm = (n1 > n2) ? n1 : n2;
while(true) {
if( lcm % n1 == 0 && lcm % n2 == 0 ) {
System.out.printf("The LCM of 2 numbers is : "+lcm);
break;
}
++lcm;
}
}
}