Sunday 31 January 2016

Fibonacci Series

Write a program in java print the fibonacci series within given range.
Example: 0, 1, 1, 2, 3, 5, 8, 13..........n th terms.

Program:
import java.util.*;
class Fibonacci
{
public void func(int n)
{
int i, a=0, b=1, s;
System.out.print(a + "," + b + ",");
for(i=3;i<=n;i++)
{
s=a+b;
System.out.print(s+",");
a=b;
b=s;
}
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter The Limit: ");
int limit=sc.nextInt();
Fibonacci ob = new Fibonacci();
ob.func(limit);
}
}
Output:
Enter The Limit:
10
0,1,1,2,3,5,8,13,21,34,

Saturday 30 January 2016

Write a program in java to check the number is FizzBuzz number or not.

What is FizzBuzz?
In this question, you will work with two numbers 3 and 5. The user gives a number as input and then we'll have to get the list of all the numbers starting from 1 till the given number. Now, all we need to do is to find the numbers that are multiples of 3 and 5 and print special words called Fizz and Buzz instead of those multiples respectively. If a number is multiple of both 3 as well as 5 (say 15) FizzBuzz should be printed and if a number is neither a multiple of 3 nor 5 then the number itself is printed. Now let us see how this works.

Program:
import java.util.*;
class FizzBuzz
{
    public static void main(String args[])
    {
    Scanner s=new Scanner(System.in);
    System.out.println("Enter the number");
    int n=s.nextInt();
    for(int i=1;i<=n;i++)
        {
            if(i%5==0)
            System.out.println("Buzz");
            else if(i%3==0)
            System.out.println("Fizz");
            else if((i%3==0)&&(i%5==0))
            System.out.println("FizzBuzz");
            else System.out.println(i);
        }
    }
}

Output:

Enter the number
15
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
Buzz
 
Explanation:
To know the multiples of a number we'll have to find out the remainder and 
it should be 0. The same logic is used there.

if(i%5==0) means if a number is multiple of 5 then print Buzz.
if(i%3==0) means if a number is multiple of 3 then print Fizz.
if((i%3==0)&&(i%5==0)) means if a number is multiple of 3 as well as 5, 
then print FizzBuzz.

Write a Program in Java to check if Number is Buzz Number or not.

Buzz Number: A number is said to be Buzz Number if it ends with 7 or is divisible by 7.

Example: 1007 is a Buzz Number as it end with 7. 343 is also a Buzz Number as it is divisible by 7 and 77777 is also a Buzz Number as it ends with 7 and also it is divisible by 7.

Program:
import java.util.*;
class BUZZ
{
Scanner sc=new Scanner(System.in);
int n;
public void function( )
{
System.out.println("Enter the number:");
n=sc.nextInt();
if ( n % 10 == 7  ||  a % 7 == 0 )
System.out.println("Entered number is a Buzz number.");
else
System.out.println("Entered number is not a Buzz number.");
}
public static void main(String args[ ])
{
 BUZZ ob = new BUZZ();
 ob.function( );
}
}

Sample Output:

Enter the number:
37
Entered number is a Buzz number.

Enter the number:
21
Entered number is a Buzz number.

Enter the number:
123
Entered number is not a Buzz number.

Write a Program in Java to input a number and check whether it is a Duck Number or not.

Duck Number: A Duck number is a number which has zeroes present in it, but there should be no zero present in the beginning of the number. For example 3210, 7056, 8430709 are all duck numbers whereas 08237, 04309 are not.

Program:
import java.io.*;
class Duck_No
{
    public static void main(String args[])throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter any number : ");
        String n=br.readLine(); //inputting the number and storing it in a String
        int l=n.length(); //finding the length (number of digit) of the number
        int c=0; //variable for counting number of zero digits
        char ch;
        for(int i=1;i<l;i++)
        {
            ch=n.charAt(i); //extracting each digit and checking whether it is a '0' or not
            if(ch=='0')
                c++;
        }
         char f=n.charAt(0); //taking out the first digit of the inputted number
        if(c>0 && f!='0')
            System.out.println("It is a duck number");
        else
            System.out.println("It is not a duck number");
    }
}

Write a Program in Java to input a number and check whether it is a Harshad Number or Niven Number or not.

Harshad Number : In recreational mathematics, a Harshad number (or Niven number), is an integer (in base 10) that is divisible by the sum of its digits.

Let’s understand the concept of Harshad Number through the following example:

•    The number 18 is a Harshad number in base 10, because the sum of the digits 1 and 8 is 9 (1 + 8 = 9), and 18 is divisible by 9 (since 18 % 9 = 0)

The first few Harshad numbers in base 10 are:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 18, 20, 21, 24, 27, 30, 36, 40, 42, 45, 48, 50, 54, 60, 63, 70, 72, 80, 81, 84, 90, 100, 102, 108, 110, 111, 112, 114, 117, 120, 126, 132, 133, 135, 140, 144, 150, 152, 153, 156, 162, 171, 180, 190, 192, 195, 198, 200 etc.

Program:
import java.util.*;
class HarshadNumber
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a number : ");
        int n = sc.nextInt();
        int c = n, d, sum = 0;
        //finding sum of digits
        while(c>0)
        {
            d = c%10;
            sum = sum + d;
            c = c/10;
        }
        if(n%sum == 0)
            System.out.println(n+" is a Harshad Number.");
        else
            System.out.println(n+" is not a Harshad Number.");     
    }
}

Thursday 28 January 2016

Emirp number check

Emirp Number:
An emirp number is a number which is prime backwards and forwards.
Example: 13 and 31 are both prime numbers. Thus, 13 is an emirp number. Design a class Emirp to check if a given number is Emirp number or not.

Code:
import java.util.*;
class Emirp
{
  public static void main(String args[])
  {
    Scanner sc=new Scanner(System.in);
    char ch;
    do{
int i,c=0,r,n2=0,c1=0;
    System.out.println("Enter The Number: ");
    int n=sc.nextInt();
   
    for(i=1;i<=n;i++)
    {
      if(n % i == 0)
        c++;
    }
    if(c == 2)
    {
      r = n % 10;
      n2 = (n2 * 10) + r;
      n = n / 10;
    }
    for(i=1;i<=n2;i++)
    {
      if(n2 % i == 0)
        c1++;
    }
    if ( c ==2 && c1 ==2)
    {
          System.out.println("***Emirp Number***\n");
    }
    else
    {
          System.out.println("***Not Emirp Number***\n");
    }
    System.out.println("Do You Want To Check More Numbers ? Press y / n: ");
    ch=sc.next().charAt(0);
    }
    while(ch!='n');
    System.exit(0);
    }
}
Output:
Sample Output
Input the number
13
31
Number is Emirp

Input the number
17
71
Number is Emirp

Input the number
14
41
Number is not Emirp

Write a program in java to print all automorphic numbers with range.

Automorphic Number:
An automorphic number is a number whose square ends in the same digits as the number itself.
Examples of automorphic numbers : 5, 6 and 76
52 = 25
62 = 36
762 = 5776

Code:
import java.util.*;
class Automorphic_Range
{
  public static void main(String args[])
  {
    Scanner sc=new Scanner(System.in);
    int sqr,temp,c=0,i=1,rem;
    System.out.println("Enter The Range: ");
    int n=sc.nextInt();
    while(n!=0)
    {
    temp=i;
    sqr=i*i;
    while(i!=0)
    {
      c++;
      i=i/10;
    }
    rem=sqr % ((int)Math.pow(10,c));
    if(temp == rem)
    {
      System.out.println(i + ", ");
    }
   }
  }
}