Wednesday, July 8, 2020

Write a program to input a number and find the sum of First and Last digit of this number?(2784=2+4=6first and last digit sum)

45)  Write a program to input a number and find the sum of First and Last digit of this number?(2784=2+4=6first and last digit sum)


import java.util.*;
public class First_last_digit_sum
{
   public static void main(String args[ ])
   {
      Scanner sc = new Scanner (System.in);
      System.out.println("Enter a number");
      int a = sc.nextInt();
      int p =a;
      int c = 0;
      while(p>0)
      {
          p=p/10;
          c++;
      }
      int x = (int)Math.pow(10,(c-1));
      int l = a/x;
      int f = a%10;
      int s = l+f;
      System.out.println("SUM of FIRST and LAST digit of a entered number = "+s);
    }
}

Output :  

sum-of-first-and-last-digit-of-a-number


Write a program to input a number and find the Sum and Count the all digit of this number.(2784=2+7+8+4=21…no. of digit=4)


Write a program to input a number and find the Sum and Count the all digit of this number.(2784=2+7+8+4=21…no. of digit=4)

44)   Write a program to input a number and find the Sum and Count the all digit of this number.  (2784=2+7+8+4=21…no. of digit=4)

import java.util.*;
public class sum_count_digits
{
   public static void main(String args[ ])
   {
      Scanner sc = new Scanner (System.in);
      System.out.println("Enter a number");
      int a = sc.nextInt();
      int c = 0;
      int s = 0;
      while(a>0)
      {
          int d = a%10;
          c++;
          s=s+d;
          a=a/10;
      }
      System.out.println("SUM of all digits of a entered number = "+s);
      System.out.println("Number of digits in a entered number = "+c);
    }
}
 

 Output : 


Write a program to input a number and find it’s all Prime Factor and print it?(12==2,2,3….all factors are prime)

43)  Write a program to input a number and find it’s all Prime Factor and print it?(12==2,2,3….all    factors are prime)

import java.util.*;
public class Prime_factor
{
    public static void main(String args[ ])
   {
      Scanner sc = new Scanner (System.in);
      System.out.println("Enter a number");
      int a = sc.nextInt();
      int t = a/2;
      System.out.print("PRIME FACTOR of "+a+" = ");
 if(a==1)
 {
   System.out.println(a);
 } 
     
      for(int i = 2;i<=t;)
      {
          if(a%i==0)
          {
             System.out.print(i+" ");
             a=a/i;
          }
          else
          {
             i++;
          }
      }
 
   }
   }

Output : 

Monday, July 6, 2020

Write a program to input a number and find the Primorial of this number? (6= 2, 3, 5 =2*3*5=30)

42)   Write a program to input a number and find the Primorial of this number? (6= 2, 3, 5        à2*3*5=30)

import java.util.*;
public class Primorial
{
   public static void main(String args[ ])
   {
      Scanner sc = new Scanner (System.in);
      System.out.println("Enter a number");
      int a = sc.nextInt();
      int p=1;
      for(int i=2;i<=a;i++)
      {
        int c = 0;
        for(int j=1;j<=i;j++)
        {
            if(i%j==0)
            c++;
        }
        if(c==2)
        {
            p = p*i;
        }                 
      }
      System.out.println("PRIMORIAL of "+a+" = "+p);
    }
}

output :  


Write a program to input two integers and find the GCD/HCF of two numbers (using division method)? (10,12, HCF=2)

41)   Write a program to input two integers and find the GCD/HCF of two numbers (using division method)? (10,12,   HCF=2)

import java.util.*;
public class GCD
{
    public static void main(String args[ ])
   {
      Scanner sc = new Scanner (System.in);
      System.out.println("Enter two numbers");
      int a = sc.nextInt();
      int b = sc.nextInt();
      int k = Math.min(a,b);
      b = Math.max(a,b);
      a=k;
      while(k!=0)
      {
          k=b%k;
          if(k!= 0)
          {
              b=a;
              a=k;
          }
      }
      System.out.println("G.C.D = "+a);
   }
}




Write a program to input two integers and find the HCF of two numbers? (10,12. HCF=2)

40)  Write a program to input two integers and find the HCF of two numbers? (10,12. HCF=2)

import java.util.*;
public class HCF
{
   public static void main(String args[ ])
   {
      Scanner sc = new Scanner (System.in);
      System.out.println("Enter two numbers");
      int a = sc.nextInt();
      int b = sc.nextInt();
      for(int i = Math.min(a,b);i>=1;i--)
      {
              if(a%i==0&&b%i==0)
              {
                  System.out.println("H.C.F of "+a+" and "+b+" = "+i);
                  break;
              }
      }
   }
}

Sunday, July 5, 2020

Write a program to input two integers and find the LCM of two numbers?(10,12..LCM=60 )

39)  Write a program to input two integers and find the LCM of two numbers?(10,12..LCM=60 )

import java.util.*;
public class LCM
{
   public static void main(String args[ ])
   {
      Scanner sc = new Scanner (System.in);
      System.out.println("Enter two numbers");
      int a = sc.nextInt();
      int b = sc.nextInt();
      for(int i = Math.max(a,b);i<a*b;i++)
      {
              if(i%a==0&&i%b==0)
              {
                  System.out.println("L.C.M of "+a+" and "+b+" = "+i);
                  break;
              }
      }
   }
}

output : 


 jhvghvgk ;nkjn ,nkjhj