Skip to content

Latest commit

 

History

History
94 lines (85 loc) · 2.01 KB

SECTION 2 (DATA TYPES AND OPERATORS).md

File metadata and controls

94 lines (85 loc) · 2.01 KB

1).Counting number of digits

import java.util.Scanner;
public class TestClass {
  public static void main(String args[]){
      Scanner sc = new Scanner(System.in);
      int count = 0;
      int num = sc.nextInt();
      while(num!=0){
         num = num/10;
         count++;
      }
      System.out.println(count);
   }
}

2).Even or Odd using division '/' operator

 import java.io.*;
import java.util.Scanner;
public class TestClass {
   public static void main(String[] args) { 
       Scanner sc=new Scanner(System.in);
       int a=sc.nextInt();
       if(a%2==0)
       {
         System.out.print("Even");
       }
       else
       {
         System.out.print("Odd");
       }

  }
}

3).Strong Number

     import java.util.*;
    class TestClass
    {
    static int factorial(int rem)
    {
    int fact=1;
    int i;
    for(i=1;i<=rem;i++)
    {
    fact=fact*i;	
    }	
    return fact;
    }
    public static void main(String [] args)
    {
    Scanner sc=new Scanner(System.in);
    int n;
    int sum=0,rem=0;
    int num;
    n=sc.nextInt();
    num=n;
    while(num>0)
    {
    rem=num%10;
    sum=sum+factorial(rem);	
    num=num/10;
    }
    if(n==sum)
    {
    System.out.print("Yes");	
    }
    else
    {
    System.out.print("No");	
    }
    }
    }

4).Swapping two numbers without using third variable

     import java.io.*;
    import java.util.Scanner;
    public class TestClass {
         public static void main(String[] args) { 
            Scanner sc=new Scanner(System.in);
           int a,b;
           a=sc.nextInt();
           b=sc.nextInt();
           a=a+b;
           b=a-b;
           a=a-b;
           System.out.println(a+" "+b);
        }
    }

**5).