Monday, 25 September 2017

java programs 2

===========================================================

PROGRAM : 5

===========================================================

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

import java.util.*;

public class SmallestNoDemo {

 
    public static void main(String[] args) {

        int iCount, j, counter;
        System.out.print("Roll No :IT16B06\nName :Khanjan Dharaiya                                              \nDivision :B\n\n");

        for (iCount = 1; iCount < 1000000000; iCount++) {
            counter = 0;
            for (j = 1; j <= 20; j++) {
                if (iCount % j == 0) {
                    counter++;
                }
            }
            if (counter == 20) {
                System.out.printf("%dis the smallest positive number that is evenly divisible by all of the numbers from 1 to 20 Is :", iCount);
                break;
            }
        }
    }
}



/*

232792560  is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20.

*/

===========================================================

PROGRAM : 6

===========================================================


The sum of the squares of the first ten natural numbers is,
12 + 22 + ... + 102 = 385
The square of the sum of the first ten natural numbers is,
(1 + 2 + ... + 10)2 = 552 = 3025
Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 385 = 2640.
Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.




package bankaccountdemo;
import java.util.*;

public class SumOfSquares
 {

 
    public static void main(String[] args)
    {
       
       
                        long iNo;
                        Scanner in=new Scanner(System.in);
                        System.out.print("Roll No :IT16B06\nName :Khanjan Dharaiya                                          \nDivision :B\n\n");
                        System.out.println("Enter The Prime Number :");
                        iNo=in.nextLong();
                        prime(iNo);
     }
     public static void prime(long iNo)
     {
                       
                        int iNum,iSqSum,iSquare;
                        Scanner sc=new Scanner(System.in);
                       
                        System.out.print("\nEnter any number :");
                        iNum=sc.nextInt();
                       
                        iSqSum=squareOfSum(iNum);
                        System.out.println("\nThe sum of the squares of the first ten natural                        numbers is  :"+iSqSum);
                       
                        iSquare=sumOfSquare(iNum);
                        System.out.println("\nThe square of the sum of the first ten natural                          numbers is : "+iSquare);
                       
                        System.out.println("\nDifference Between This Two Number                                               is :"+(iSquare-iSqSum));
         }
         public static int squareOfSum(int iNo)
         {
                        int iSum=0;
                        for(int iCount=1;iCount<=iNo;iCount++)
                        {
                                    iSum=iSum+(iCount*iCount);
                        }
                        return iSum;
            }
            public static int sumOfSquare(int iNo)
            {
                        int iSum=0,iSquare=1;
                        for(int iCount=1;iCount<=iNo;iCount++)
                        {
                                    iSum=iSum+iCount;
                        }
                        iSquare=iSum*iSum;
                        return iSquare;
            }
}




/*

                                          

Enter The Prime Number : 100

Enter any number :100

The sum of the squares of the first ten natural numbers is  :338350

The square of the sum of the first ten natural numbers is : 25502500

Difference Between This Two Number is :25164150


*/

===========================================================

PROGRAM : 7

===========================================================

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
What is the 10001st prime number?
import java.util.*;

public class PrimeFind {

 
 public static void main(String[] args)
 {

           int numberOfPrimes = 0;
           long prime = 1;
           long lNo = 10001;
            System.out.print("Roll No :IT16B06\nName :Khanjan Dharaiya                                          \nDivision :B\n\n");
 while (numberOfPrimes < lNo)
 {
            prime++;
            if (isPrime(prime))
             {
                numberOfPrimes++;
            }
   }
             System.out.println("The 10001st prime number is : " + prime);
    }

    public static boolean isPrime(long n)
    {
                    if (n < 2)
                        {
                                    return false;
                        }
                        else if (n == 2)
                        {
                                    return true;
                        }
                        for (int i = 2; i < Math.pow(n, 0.5) + 1; i++)
                        {
                                    if (n % i == 0)
                                     {
                                                return false;
                                     }
                        }
                         return true;
    }
}


/*

                                           

The 10001st prime number is : 104743

*/

===========================================================

PROGRAM : 8

===========================================================

215 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.
What is the sum of the digits of the number 230?
import java.util.*;

public class SumOfRestDigit
{

 
    public static void main(String[] args)
    {
           int iNo = 2, iAmbush = 30;
           int iReminder, iSum = 0;
           int iResult = (int) Math.pow(iNo, iAmbush);

            System.out.print("Roll No :IT16B06\nName :Khanjan Dharaiya\nDivision :B\n     \n");

            System.out.println("Answer Of 2 Rest To 30 Is = "+iResult);
            while (iResult > 0)
              {
            iReminder = iResult % 10;
                         iSum += iReminder;
                        iResult /= 10;
             }
             System.out.println("The Sum Of Digit Is = "+iSum);
      }
}


/*

                                          

Answer of 2 Rest To 30 Is = 1073741824
The sum of result is =37

*/

===========================================================

PROGRAM : 9

===========================================================

The Fibonacci sequence is defined by the recurrence relation:
Fn = Fn1 + Fn2, where F1 = 1 and F2 = 1.
Hence the first 12 terms will be:
F1 = 1
F2 = 1
F3 = 2
F4 = 3
F5 = 5
F6 = 8
F7 = 13
F8 = 21
F9 = 34
F10 = 55
F11 = 89
F12 = 144
The 12th term, F12, is the first term to contain three digits.
What is the index of the first term in the Fibonacci sequence to contain 10 digits?


import java.util.*;

public class FindFiboLocation
 {

 
    public static void main(String[] args)
 {
           Scanner sc=new Scanner(System.in);
           long a1=0,b1=1,c1,icount1=1;
           long a=0,b=1,c,icount=3;
        
           System.out.print("Roll No :IT16B06\nName :Khanjan Dharaiya\nDivision :B\n       \n");

            System.out.print("Enter Number For Fibo :");
            long fibo=sc.nextLong();
       
        
              System.out.print("f"+icount1+"  "+icount1+"\n");
              icount1++;
              System.out.print("f"+icount1+"  "+icount1+"\n");
              icount1++;
        
               for(int i=0;i<=fibo-2;i++)
                {  
                    c1=a1+b1;
                    a1=b1;
                    b1=c1;
                    System.out.print("f"+icount1+"  ");
                    System.out.println(c1);
                    icount1++;
                 }
       
                 System.out.print("Enter Value For find Start Digit :");
                 int digit=sc.nextInt();
       
       
       
                for(int i=0;i<=fibo-2;i++)
                {  
           
                      c=a+b;
                       a=b;
                       b=c;
                        switch(digit)
                        {
                            case 1:
                                 System.out.print("Start one digit at f0");
                                 fibo=-1;
                            break;

                            case 2:
                                    if(c>=10)
                                    {
                                        System.out.println("Start two digit at f"+icount);
                                         fibo=0;
                                    }
                             break;

                             case 3:
                                    if(c>=100)
                                    {
                                        System.out.println("Start three digit at f"+icount);
                                          fibo=0;
                                    }
                             break;

                             case 4:
                                    if(c>=1000)
                                    {
                                        System.out.println("Start four digit at f"+icount);
                                          fibo=0;
                                    }
                             break;
                             case 5:
                                    if(c>=10000)
                                    {
                                        System.out.println("Start five digit at f"+icount);
                                          fibo=0;
                                    }
                             break;

                             case 6:
                                    if(c>=100000)
                                    {
                                        System.out.println("Start six digit at f"+icount);
                                          fibo=0;
                                    }
                             break;
                             case 7:
                                    if(c>=1000000)
                                    {
                                        System.out.println("Start seven digit at f"+icount);
                                          fibo=0;
                                    }
                             break;
                             case 8:
                                    if(c>=10000000)
                                    {
                                        System.out.println("Start seven digit at f"+icount);
                                          fibo=0;
                                    }
                             break;
                             case 9:
                                    if(c>=100000000)
                                    {
                                        System.out.println("Start seven digit at f"+icount);
                                          fibo=0;
                                    }
                             break;
                             case 10:
                                    if(c>=1000000000)
                                    {
                                        System.out.println("Start seven digit at f"+icount);
                                          fibo=0;
                                    }
                             break;
                                                    
                                  
                             default:
                                     System.out.println("Please Enter Valied Digits........");
                                     fibo=-1;
                        }
           
                        icount++;
        }
       
    }
}


/*

Enter Number For Fibo :50
f1  1
f2  2
f3  1
f4  2
f5  3
f6  5
f7  8
f8  13
f9  21
f10  34
f11  55
f12  89
f13  144
f14  233
f15  377
f16  610
f17  987
f18  1597
f19  2584
f20  4181
f21  6765
f22  10946
f23  17711
f24  28657
f25  46368
f26  75025
f27  121393
f28  196418
f29  317811
f30  514229
f31  832040
f32  1346269
f33  2178309
f34  3524578
f35  5702887
f36  9227465
f37  14930352
f38  24157817
f39  39088169
f40  63245986
f41  102334155
f42  165580141
f43  267914296
f44  433494437
f45  701408733
f46  1134903170
f47  1836311903
f48  2971215073
f49  4807526976
f50  7778742049
f51  12586269025

Enter Value For find Start Digit :10

Start seven digit at f46

*/

===========================================================

PROGRAM : 10

===========================================================

145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145.
Find the sum of all numbers which are equal to the sum of the factorial of their digits.
Note: as 1! = 1 and 2! = 2 are not sums they are not included.


import java.util.*;

public class CuriousNoDemo
{

 
             public static void main(String[] args)
            {
                        Scanner scanner = new Scanner(System.in); 

                     System.out.print("Roll No :IT16B06\nName :Khanjan Dharaiya                                             \nDivision :B\n\n");           
                   
                         System.out.println("Enter Any Number :");
                        long iNo = scanner.nextLong();
                        long iSum = 0, iReminder;
                        while (iNo > 0) {
                        iReminder = iNo % 10;
                        iSum = iSum + funFactorial(iReminder);
                        iNo = iNo / 10;
            }
            System.out.println("\nSum Of The Facto Of All Digits :" + iSum);
    }
    static long funFactorial(long iNoFromUser)
     {
                long iSum = 1;
               for (long iCount = 1; iCount <= iNoFromUser; iCount++) {
               iSum *= iCount;
            
      }
      System.out.print(iNoFromUser+"! : "+iSum+", ");
      return iSum;
}

}


/*

                                          

Enter Any Number : 145

5! : 120, 4! : 24, 1! : 1,

Sum Of The Facto Of All Digits :145



*/

0 comments:

Post a Comment