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 + ", ");
    }
   }
  }
}

Write a program in java input a number and check the number is armstrong number or not.

Armstrong Number in Java:

Armstrong number is a number that is equal to the sum of cubes of its digits. For example 0, 1, 153, 370, 371, 407 etc.
 
153 = (1*1*1)+(5*5*5)+(3*3*3)
where:
(1*1*1)=1
(5*5*5)=125
(3*3*3)=27
So:
1+125+27=153

Program:
import java.util.*;
class Armstrong
{
  public int arm(int n)
  {
    int rem,sum=0;
    do
    {
      rem=n % 10;
      sum = sum + (rem * rem * rem);
      n= n/10;
    }
    while(n!=0);
    return sum;
  }
  public static void main(String args[])
  {
    int n,temp,res;
    Scanner sc=new Scanner(System.in);
    Armstrong ob=new Armstrong();
    System.out.println("Enter The Number: ");
    n=sc.nextInt();
    temp=n;
    res=ob.arm(n);
    if(res==temp)
    {
      System.out.println("Armstrong Number");
    }
    else
      System.out.println("Not an Armstrong Number");
   
  }
}

Output:
Enter The Number:
153
Armstrong Number

Enter The Number:
105
Not an Armstrong Number

Write a program in java to print the composite numbers within range.

Composite Number :

A Composite Number is a positive integer that has at least one positive divisor other than one or the number itself. In other words, a Composite Number is any positive integer greater than one that is not a Prime Number. Write a Java program that Input the Number And Find Number is Composite or Prime.

Program:
import java.util.Scanner;
class Composite_Number
{
   int flag,n,i,j;
   Composite_Number()
     {
       System.out.print("Enter range to find the composite numbers: ");
       Scanner in=new Scanner(System.in);
       n=in.nextInt();
       for(i=2;i<=n;i++ )
        {
            flag=0;
           for(j=2;j<i;j++)
             {
                 if(i % j == 0)
                    flag++;
             }
           if(flag!=0)
            System.out.println("one of the composite number is:" + i);
        }
    }
public static void main(String args[])   
      {
           Composite_Number ob=new Composite_Number();
      }
}

Output:
Enter range to find the composite numbers:
20

one of the composite number is:4
one of the composite number is:6
one of the composite number is:8
one of the composite number is:9
one of the composite number is:10
one of the composite number is:12
one of the composite number is:14
one of the composite number is:15
one of the composite number is:16
one of the composite number is:18
one of the composite number is:20

Wednesday 27 January 2016

write a program in java to input a sentence and count how many word present in the sentence.

Code:
import java.util.*;
class Word_Count
{
public static void main(String args[])
{
String st; int l,i,count=0; char c;
Scanner sc=new Scanner(System.in);
System.out.print("Enter any sentence: ");
st=sc.nextLine();
st=" "+st; //adding a space infront of the inputted sentence or a name
st=st.toUpperCase(); //converting the sentence into Upper Case (Capital Letters)
l=st.length(); //finding the length of the sentence</span>
for(i=0;i<l;i++)
{
c=st.charAt(i); //taking out one character at a time from the sentence
if(c==' ') //if the character is a space, printing the next Character along with a fullstop
  count++;
}
System.out.print(count);
}
}

write a program in java to print the given name in initials.

Example:
Input Name: Sumanta Pramanick.
Output: S.P.

Code:
import java.util.*;
class Initials_Scanner
{
public static void main(String args[])
{
String st; int l,i; char c;
Scanner sc=new Scanner(System.in);
System.out.print("Enter any sentence: ");
st=sc.nextLine();
st=" "+st; //adding a space infront of the inputted sentence or a name
st=st.toUpperCase(); //converting the sentence into Upper Case (Capital Letters)
l=st.length(); //finding the length of the sentence</span>
for(i=0;i<l;i++)
{
c=st.charAt(i); //taking out one character at a time from the sentence
if(c==' ') //if the character is a space, printing the next Character along with a fullstop
System.out.print(st.charAt(i+1)+".");
}
}
}

Write a program in java to input a string (word). Convert it into lowercase letters. Count and print the frequency of each alphabet present in the string.

The output should be given as:
Sample Input: Alphabets
Sample Output:
==========================
Alphabet             Frequency
==========================
a                              2
b                              1
e                              1
h                              1
l                               1
p                              1
s                              1
t                              1

Code:
import java.util.*;
class AlphabetFreq
{
    public static void main(String args[])
    {
      String s;
      int l,count=0,j;
      char ch,i;
      Scanner sc=new Scanner(System.in);
      System.out.print("Enter any string: ");
      s = sc.nextLine();
      s=s.toLowerCase(); //converting the string into lowercase
      l=s.length(); //finding the length of the string
       /* Counting frequency of alphabets begins below */
          for(i='a'; i<='z'; i++)
            {
                count = 0;
                for(j=0; j<l; j++)
                {
                    ch=s.charAt(j); //extracting characters of the string one by one
                    if(ch==i) //first checking the whole string for 'a', then 'b' and so on
                count++; //increasing count of those aplhabets which are present in the string
                }
                if(count!=0)//printing only those alphabets whose count is not '0'
                {
                    System.out.println(i+"\t\t"+count);
                }
            }
    }

write a program in java to print only vowels present in a sentence without using build in function.

Code:
class Vowel_ASCII
{
  public static void main(String args[])
  {
    String st="THIS IS IT COMPUTER";
    int i,l,c,count=0;
    l=st.length();
    for(i=0;i<l;i++)
    {
      c=st.charAt(i);
      if(c==65 || c==69 || c==73 || c==79 || c==85 ||
         c==97 || c==101 || c==105 || c==111 || c==117)
         System.out.print((char)c+" ");
     }
  }
}

write a program in java to count how many vowels present in a sentence without using build in function.

Code:
class Vowel_ASCII
{
  public static void main(String args[])
  {
    String st="THIS IS IT";
    int i,l,c,count=0;
    l=st.length();
    for(i=0;i<l;i++)
    {
      c=st.charAt(i);
      if(c==65 || c==69 || c==73 || c==79 || c==85 ||
         c==97 || c==101 || c==105 || c==111 || c==117)
         count++;
    }
    System.out.println(count);
  }
}

write a program in java to print all letters lowercase to uppercase without using build in function.

Code:
class Lower_Upper
{
  public static void main(String args[])
  {
    String st="computer";
    int i,c;
    for(i=0;i<st.length();i++)
    {
      c=st.charAt(i);
      if(c>=97 && c<=123)
      {
        c=c-32;
       }
      System.out.print((char)c);
    }
  }
}

write a program in java to print all letters UPPERCASE to LOWERCASE without using build in function.

Code:
class Upper_Lower
{
  public static void main(String args[])
  {
    String st="COMPUTER";
    int i,c;
    for(i=0;i<st.length();i++)
    {
      c=st.charAt(i);
      if(c>=65 && c<=90)
      {
        c=c+32;
       }
      System.out.print((char)c);
    }  
  }
}

Write a program in java to print the english alphabets small a to z without using build in function.

Code:
class Atoz
{
  public static void main(String args[])
  {
    int i;
    for(i=97;i<=122;i++)
    {
      System.out.print((char)i+" ");
    }
  }
}

Write a program in java to print the english alphabets capital A to Z without using build in function.

Code:
class AtoZ
{
  public static void main(String args[])
  {
    char i;
    for(i='A';i<='Z';i++)
    {
      System.out.print(i+" ");
    }
  }
}

Write a program in java to print the english alphabets capital A to Z.

Code:
class AtoZ
{
  public static void main(String args[ ])
  {
    char i;
    for(i='A';i<='Z';i++)
    {
      System.out.print(i+" ");
    }
  }
}

Write a program in java to print the ASCII codes and equivalent letters without using build in function.

Code:
class Print_ASCII
{
  public static void main(String args[])
  {
    int i;
    for(i=32;i<=127;i++)
    {
      System.out.println("ASCII CODE : " +  i  + " LETTER : " + (char) i );
    }
  }
}

Write a program in java print all the english alphabets without using build in function.

Code:
class Print_Alphabets
{
  public static void main(String args[])
  {
    int i;
    for(i=65;i<=90;i++)
    {
      System.out.print((char)(i));
    }
  }
}

Java constructor overloading

Like other methods in java constructor can be overloaded i.e. we can create as many constructors in our class as desired. Number of constructors depends on the information about attributes of an object we have while creating objects. See constructor overloading example:

class Language

{
  String name;

  Language( )

{
    System.out.println("Constructor method called.");
}
  Language(String t)
{
    name = t;
}
  public static void main(String[ ] args)
{
    Language cpp  = new Language();
    Language java = new Language("Java");
    cpp.setName("C++");
    java.getName();
    cpp.getName();
  }
  void setName(String t)
{
    name = t;
}
  void getName( )
{
    System.out.println("Language name: " + name);
}
}

Write a program in java input two string swapping two strings without using third variable.

Code:
import java.io.*;
class Swap_Strings
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader (new InputStreamReader (System.in));
System.out.print("Enter the 1st String : ");
String s1=br.readLine();
int len1=s1.length();
System.out.print("Enter the 2nd String : ");
String s2=br.readLine();
System.out.println("-------------------------------");
System.out.println("Strings Before Swapping : ");
System.out.println("1st String = "+s1);
System.out.println("2nd String = "+s2);
/*Swapping Process Begins*/
s1=s1+s2;
s2=s1.substring(0,len1);
s1=s1.substring(len1);
/*Swapping Process Ends*/
System.out.println("-------------------------------");
System.out.println("Strings After Swapping : ");
System.out.println("1st String = "+s1);
System.out.println("2nd String = "+s2);
}
}

Output:
Enter the 1st String : Java For
Enter the 2nd String : School is Fun
——————————-
Strings Before Swapping :
1st String = Java For
2nd String = School is Fun
——————————-
Strings After Swapping :
1st String = School is Fun
2nd String = Java For

Working:
Initially s1 = “Java For” and s2 = “School is Fun”,
Now, the variable, ‘len1’ stores the length of the 1st String. Hence, in this case, ‘len1’ = 8

Step 1: s1 = s1+s2; gives s1 = “Java For”+”School is Fun”;
i.e. s1 = “Java ForSchool is Fun” [Note: There will be no space when we join the 1st and the 2nd String]

Step 2: s2=s1.substring(0,len1); gives, s2=”Java ForSchool is Fun”.substring(0,8);
i.e. s2 = “Java For”

Step 3: s1 = s1.substring(len1); gives, s1 = “Java ForSchool is Fun.substring(8);
i.e. s1 = “School is Fun”

Hence, finally we have s1 = “School is Fun” and s2 = “Java For”. [Swapping Done]

Write a program in java to input a word from the user and remove the consecutive repeated characters by replacing the sequence of repeated characters by its single occurrence.


Example:
INPUT – Jaaavvvvvvvvaaaaaaaaaaa
OUTPUT – Java
INPUT – Heeeiiiissggoiinggg
OUTPUT – Heisgoing

import java.io.*;
class RemoveRepChar
{
    public static void main(String args[])throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter any word: "); // Inputting the word
        String s = br.readLine();
        s = s + " "; // Adding a space at the end of the word
        int l=s.length(); // Finding the length of the word
        String ans=""; // Variable to store the final result
        char ch1,ch2;
        for(int i=0; i<l-1; i++)
        {
            ch1=s.charAt(i); // Extracting the first character
            ch2=s.charAt(i+1); // Extracting the next character
// Adding the first extracted character to the result if the current and the next characters are different
            if(ch1!=ch2)
            {
            ans = ans + ch1;
            }
        }
        System.out.println("Word after removing repeated characters = "+ans); // Printing the result
    }
}

Output:

Output - 1:
Enter any word: Jaaavvvvvvvvaaaaaaaaaaa
Word after removing repeated characters = Java
 
Output - 2:
Enter any word: iiiiiisssssfffffffffffffuunnnnn
Word after removing repeated characters = isfun

Write a program in java input a number and check the number is special two-digit number or not.


A special two-digit number is such that when the sum of the digits is added to the product of its digits, the result is equal to the original two-digit number.
Example:
Consider the number 59.Sum of digits = 5+9=14
Product of its digits = 5 x 9 = 45
Sum of the digits and product of digits = 14 + 45 = 59

Write a program to accept a two-digit number. Add the sum of its digits to the product of its digits. If the value is equal to the number input, output the message “special-two digit number” otherwise, output the message “Not a special two-digit number”.

import java.io.*;
class Special
    {
    public static void main(String args[ ])throws IOException
        {
            BufferedReader br=new BufferedReader (new InputStreamReader(System.in));
            System.out.print("Enter a 2 digit number : ");
            int n = Integer.parseInt(br.readLine());
             
            int first, last, sum, pro;
            if(n<10 || n>99) //Checking whether entered number is 2 digit or not
                System.out.println("Invalid Input! Number should have 2 digits only.");
            else
                {
                    first = n/10; //Finding the first digit
                    last = n%10; //Finding the last digit
                    sum = first + last; //Finding the sum of the digits
                    pro = first * last; //Finding the product of the digits
                    if((sum + pro) == n)
                    {
                      System.out.println("Output : The number "+n+" is a Special Two-Digit Number.");
                    }
                    else
                    {
                      System.out.println("Output : The number is Not a Special Two-Digit Number.");
                    }
                }
        }
    }

Output:

1. Enter a 2 digit number : 79
Output : The number 79 is a Special Two-Digit Number.

2. Enter a 2 digit number : 47
Output : The number is Not a Special Two-Digit Number.