Monday, 25 September 2017

java programs 1

===========================================================
PROGRAM  :  1
===========================================================

 n! means n × (n 1) × ... × 3 × 2 × 1
For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800,
and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.
Find the sum of the digits in the number 25!


class sumoffacto
{
            public static void main(String args[])
            {
           
                        int ifacto=1,icntr,irem=0,irev=0,isum=0;
                       
                       
                       
                        for(icntr=1;icntr<=25;icntr++)
                        {
                                    ifacto=ifacto*icntr;
                                   
                        }
                        System.out.print("The Factorial of 25 is :"+ifacto+"\n");
                        while(ifacto!=0)
                        {
                                    irem=ifacto%10;
                                    irev=(irev*10)+irem;
                                    ifacto=ifacto/10;
                                    System.out.print(irem+"+");
                                    isum=isum+irem;
                        }System.out.print("\n");
                        System.out.print("The sum of Factors Is :"+isum+"\n");

            }
}





                                                /*
                       
                                                The Factorial of 25 is :2076180480
                                                0+8+4+0+8+1+6+7+0+2+
                                                The sum of Factors Is :36
                                                                                                                                                                                                */
===========================================================
PROGRAM  :  2
===========================================================
 If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.

import java.util.*;
class dividethreefive
{
	public static void main(String args[])
	{
	
		int ifacto=1,icntr,inum,isum=0;
		Scanner sc=new Scanner(System.in);
		
		
		System.out.print("Roll No :IT16B06\nName :Khanjan Dharaiya				\nDivision :B\n\n");
		
		System.out.print("Enter number for you want to print value :");
		inum=sc.nextInt();
		
		System.out.print("This Number is Divided By 3 or 5 in between 1 		to 1000 :");
		
		for(icntr=1;icntr < inum;icntr++)


{
                                    if(icntr%3==0 || icntr%5==0)
                                    {
                                                System.out.print("\n"+icntr);
                                                isum=isum+icntr;
                                    }
                                   
                        }System.out.print("\n");System.out.print("The sum of this                            numbers                                   is :"+isum+"\n");
                       
            }
}                     
       
                                    Enter number for you want to print value :10
                                    This Number is Divided By 3 or 5 in between 1 to                                                 1000 :
                                                                                    3
                                                                                    5
                                                                                    6
                                                                                    9
                                    The sum of this numbers is :23
                                           */
===========================================================
PROGRAM : 3
===========================================================
 Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89,
By considering the terms in the Fibonacci sequence whose values do not exceed four lakhs, find the sum of the even-valued terms.
import java.util.*;
class fiboevensum
{
            public static void main(String args[])
            {
           
                        long ivalue1=0,ivalue2=1,icntr=1,temp=0,isum=0,inum;
                        Scanner sc=new Scanner(System.in);
                       
                       
                        System.out.print("Roll No :IT16B06\nName :Khanjan Dharaiya                                          \nDivision :B\n\n");
                       
                        System.out.print("Enter number for you want to print value :");
                        inum=sc.nextInt();
                        System.out.print("The even Fibonanci is :");
                        System.out.print("\n0\n");
                        while(temp<inum)
                        {
                                    temp=ivalue1+ivalue2;
                                    ivalue2=ivalue1;
                                    ivalue1=temp;
                                    if(temp%2==0)
                                    {
                                                System.out.print(temp+"\n");
                                                isum=isum+temp;
                                    }
                                    icntr++;
                        }System.out.print("The sum of even fibo is :"+isum+"\n");
            }
}
                                                                                                /*
                                                                                               
                                               
                                                Enter number for you want to print value :400000
                                                The even Fibonanci is :
                                                                                                0
                                                                                                2
                                                                                                8
                                                                                                34
                                                                                                144
                                                                                                610
                                                                                                2584
                                                                                                10946
                                                                                                46368
                                                                                                196418
                                                The sum of even fibo is   :  257114
                                                                                               
                                                                                                */
===========================================================
PROGRAM : 4
===========================================================
 The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475 ?
import java.util.*;
public class PrimeFactors
 {
public static void main(String[] args)
{       
                        System.out.print("Roll No :IT16B06\nName :Dafda K                                 B. \nDivision :B\n\n");
                        long iNo;
                        Scanner in=new Scanner(System.in);
                        System.out.println("Enter Prime Number  :");
                        iNo=in.nextLong();
                        prime(iNo);
}
public static void prime(long iNo)
{
                        int iCount=0,iCount1=0,iFlag=0,iValue=0;
                        for(iCount=2;iCount<iNo;iCount++)
                        {
                                    if(iNo%iCount==0)
                                    {
                                                if (isPrime(iCount))
                                                {
                                                System.out.println("Factor :"+iCount);
                                                iValue=iCount;
                                                }
                                    }
                        }
                        System.out.print("Your largest prime number is"+iValue+" ");
}
public static boolean isPrime(int iCount)
{
                        int iFlag=0,iCnt;
                        for(iCnt=2;iCnt<iCount;iCnt++)
                        {
                                    if(iCount%iCnt==0)
                                    {
                                                iFlag=1;
                                    }
                        }
                        if(iFlag==0)
                        {
                        return true;
                        }
                        else
                        {
                        return false;
                        }
}
}
                                                /*
                                               
Enter Prime Number  :
600851475
Factor :3
Factor :5
Factor :7
Factor :54499
Your largest prime number is54499
*/
===========================================================
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.
*/

0 comments:

Post a Comment