Thursday, 28 January 2016

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);
  }
}